-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathanalyseAndReply.js
61 lines (51 loc) · 1.43 KB
/
analyseAndReply.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const generatePrompt = require("./promptGenerator");
const fs = require("fs");
const openAI = require("./openAI");
async function analyseAndReply(
page,
orderedMessages,
OUR_TINDER_USER_ID,
theirName,
conversationId
) {
// Last message is the first in the messages array
const isLastMessageByThem =
orderedMessages[orderedMessages.length - 1] &&
orderedMessages[orderedMessages.length - 1].from !== OUR_TINDER_USER_ID;
console.log(
"Last message by",
isLastMessageByThem ? "them 😏" : "us ...going to next conversation"
);
if (isLastMessageByThem) {
const generatedPrompt = generatePrompt(
orderedMessages,
OUR_TINDER_USER_ID,
"Anthony",
theirName
);
const completion = await openAI.getNextUserMessage(
generatedPrompt,
theirName
);
const text = completion.data.choices[0].text
.replace(/(\r\n|\n|\r)/gm, "")
.replace("Anthony:", "");
console.log("🤖 says:", text);
// Save the conversation in a JSON file
fs.writeFile(
`conversations/${theirName}-${conversationId}.txt`,
generatedPrompt,
function (err) {
if (err) {
console.log(err);
}
}
);
// Focus and type the message like a user would (slowly)
await page.focus("textarea");
await page.keyboard.type(text);
// Send the message
await page.keyboard.press("Enter");
}
}
module.exports = analyseAndReply;