-
Notifications
You must be signed in to change notification settings - Fork 1
/
NotMe.js
103 lines (81 loc) · 2.95 KB
/
NotMe.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require('dotenv').config();
const qrcode = require('qrcode-terminal');
const {Client, LocalAuth} = require('whatsapp-web.js');
const OpenAIApi = require('openai');
const openai = new OpenAIApi({apiKey: process.env.OPENAI_API_KEY});
const client = new Client({
authStrategy: new LocalAuth(),
});
let conversationHistory = [];
let messageBuffer = [];
let replyTimeout = null;
const filteredPhrases = [
/**
* You can add phrases here that will be filtered out and not replied to
*/
];
client.on('qr', (qr) => {
qrcode.generate(qr, {small: true});
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message', async (message) => {
console.log(`Received message: ${message.body}`);
const allowedNumber = '[email protected]'; // Adjust the allowed number as needed
if (message.from !== allowedNumber || isMessageFiltered(message.body)) {
console.log(`Message from ${message.from} ignored or filtered out.`);
return;
}
messageBuffer.push(message.body);
if (replyTimeout) {
return;
}
/*
Here you can set up a timer, from message receive to reply sent.
Messages that are received within the timeout will be accumulated for the response.
*/
const minDelay = 3 * 60 * 1000;
const maxDelay = 30 * 60 * 1000;
const delay = Math.random() * (maxDelay - minDelay) + minDelay;
replyTimeout = setTimeout(async () => {
const combinedMessage = messageBuffer.join(' ');
const response = await generateReply(combinedMessage);
message.getChat().then(chat => {
chat.sendMessage(response);
});
messageBuffer = [];
replyTimeout = null;
}, delay);
});
function isMessageFiltered(message) {
if (!message.trim() || filteredPhrases.includes(message.toLowerCase().trim())) {
return true;
}
return false;
}
async function generateReply(message) {
try {
const systemMessage = {
role: "system",
content: "You are a helpful assistant" // !! Important !! Adjust the ChatGPT Prompt here. You can tell him to be whatever you want.
};
const messagesForThisSession = [systemMessage, ...conversationHistory, {role: "user", content: message}];
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: messagesForThisSession,
});
if (completion.choices[0] && completion.choices[0].message) {
const responseContent = completion.choices[0].message.content.trim();
conversationHistory.push({role: "user", content: message});
conversationHistory.push({role: "assistant", content: responseContent});
return responseContent;
} else {
return "Error generating reply";
}
} catch (error) {
console.error('Error generating reply:', error);
return 'Error generating reply';
}
}
client.initialize();