-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
151 lines (139 loc) · 5 KB
/
app.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const qrcode = require('qrcode-terminal');
const { Client, LocalAuth } = require('whatsapp-web.js');
const axios = require('axios');
require('dotenv').config();
const apiKey = process.env.OPENAI_API_KEY;
const openaiClient = axios.create({
headers: { 'Authorization': 'Bearer ' + apiKey }
});
const whatsappClient = new Client({
authStrategy: new LocalAuth(),
});
whatsappClient.initialize();
whatsappClient.on('qr', (qr) => {
qrcode.generate(qr, { small: true });
});
whatsappClient.on('authenticated', () => {
console.log('AUTHENTICATED');
});
//Sends the message to every user's first message every hour.
let repliedUsers = [];
whatsappClient.on('message', msg => {
if (msg.body && !repliedUsers.includes(msg.from) && !msg.isGroupMsg) {
msg.reply('Thank you for reaching out to me, I will be responding soon!');
repliedUsers.push(msg.from);
}
});
setInterval(() => {
repliedUsers = [];
}, 60 * 60 * 1000);
// whatsappClient.on('ready', async () => {
// console.log('Client is ready!');
// try {
// const chats = await whatsappClient.getChats();
// const TargetName = chats.find(chat => chat.name === 'Shruti');
// if (TargetName) {
// setInterval(async () => {
// const prompt = "Something for shruti.";
// const params = {
// "model": "gpt-3.5-turbo",
// "messages": [
// {"role": "system", "content": "You are a helpful assistant, who is responding to shruti, my sister, on my behalf. Send her messages about her law degree,how foody she is, a joke type which is short."},
// {"role": "user", "content": prompt}
// ]
// };
// try {
// const result = await openaiClient.post('https://api.openai.com/v1/chat/completions', params);
// const openAIResponse = result.data.choices[0].message.content.trim();
// whatsappClient.sendMessage(TargetName.id._serialized, openAIResponse);
// } catch (err) {
// console.log(err);
// whatsappClient.sendMessage(TargetName.id._serialized, 'Sorry, I am unable to generate a message.');
// }
// }, 9000);
// } else {
// console.log("Chat not found");
// }
// } catch (err) {
// console.log("Error fetching chats:", err);
// }
// });
let TargetName;
whatsappClient.on('ready', async () => {
console.log('Client is ready!');
const chats = await whatsappClient.getChats();
TargetName = chats.find(chat => chat.name === 'Mumma2');
});
//Replies to your questions.
whatsappClient.on('message', async msg => {
if (msg.body && msg.from === process.env.MUMMA_ID) {
const prompt = msg.body;
const params = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant, which talks to a person, taking text messages as prompts"},
{"role": "user", "content": prompt}
]
};
try {
const result = await openaiClient.post('https://api.openai.com/v1/chat/completions', params);
const openAIResponse = result.data.choices[0].message.content.trim();
if (TargetName) {
whatsappClient.sendMessage(TargetName.id._serialized, openAIResponse);
} else {
console.log('Chat not found');
}
} catch (err) {
console.log(err);
if (TargetName) {
whatsappClient.sendMessage(TargetName.id._serialized, 'Sorry, I am unable to generate a message.');
}
}
}
});
let TargetName2;
whatsappClient.on('ready', async () => {
console.log('Client is ready!');
const chats = await whatsappClient.getChats();
TargetName2 = chats.find(chat => chat.name === 'Deveshi');
});
//Respond to my freind, deveshi as i am busy.
whatsappClient.on('message', async msg => {
if (msg.body && msg.from === process.env.DEVESHI_ID) {
const prompt = msg.body;
const params = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant, talks to my freind, deveshi as i am busy. Keep her entertained"},
{"role": "user", "content": prompt}
]
};
try {
const result = await openaiClient.post('https://api.openai.com/v1/chat/completions', params);
const openAIResponse = result.data.choices[0].message.content.trim();
if (TargetName2) {
whatsappClient.sendMessage(TargetName2.id._serialized, openAIResponse);
} else {
console.log('Chat not found');
}
} catch (err) {
console.log(err);
if (TargetName2) {
whatsappClient.sendMessage(TargetName2.id._serialized, 'Sorry, I am unable to generate a message.');
}
}
}
});
//suggest more functionalities
//weather
whatsappClient.on('message', async msg => {
if (msg.body == '!weather') {
let weatherData = await getWeatherData(); // function to fetch weather data from API
msg.reply(`
Weather Update:
Location: ${weatherData.location}
Temperature: ${weatherData.temperature}°C
Condition: ${weatherData.condition}
`);
}
});