-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
339 lines (309 loc) · 11.2 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
const { Client, RemoteAuth, MessageMedia } = require("whatsapp-web.js");
const dotenv = require("dotenv");
dotenv.config();
const axios = require("axios");
const qrcode = require("qrcode-terminal");
const mime = require("mime-types");
const path = require("path");
const fs = require("fs");
// Require database
const { MongoStore } = require("wwebjs-mongo");
const mongoose = require("mongoose");
dotenv.config();
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.API_KEY,
});
const openai = new OpenAIApi(configuration);
// Meme Function
const sendMeme = async (chat) => {
try {
const response = await axios.get("https://meme-api.com/gimme");
const memeUrl = response.data.url;
const memeCaption = response.data.title;
const caption = memeCaption || "";
const media = await MessageMedia.fromUrl(memeUrl);
await chat.sendMessage(media, { caption });
} catch (error) {
console.error(error);
}
};
// Load the session data
mongoose.connect(process.env.MONGODB_URI).then(() => {
const store = new MongoStore({ mongoose: mongoose });
const client = new Client({
authStrategy: new RemoteAuth({
store: store,
backupSyncIntervalMs: 1000000,
}),
});
// QR Code
client.on("qr", (qr) => {
qrcode.generate(qr, { small: true });
});
client.on("ready", () => {
console.log("Client is ready!");
});
client.on("remote_session_saved", () => {
console.log("Remote session saved");
});
client.on("message", async (message) => {
let chat = await message.getChat();
// Group Chat
if (chat.isGroup) {
let grpid = chat.id._serialized;
console.log("Group ID: " + grpid);
if (message.body === "-sticker") {
if (message.hasMedia) {
message.downloadMedia().then((media) => {
if (media) {
const mediaPath = "./downloaded-media/";
if (!fs.existsSync(mediaPath)) {
fs.mkdirSync(mediaPath);
}
const extension = mime.extension(media.mimetype);
const filename = new Date().getTime();
const fullFilename = mediaPath + filename + "." + extension;
// Save to file
try {
fs.writeFileSync(fullFilename, media.data, {
encoding: "base64",
});
console.log("File downloaded successfully!", fullFilename);
console.log(fullFilename);
MessageMedia.fromFilePath((filePath = fullFilename));
client.sendMessage(
message.from,
new MessageMedia(media.mimetype, media.data, filename),
{
sendMediaAsSticker: true,
}
);
fs.unlinkSync(fullFilename);
console.log(`File Deleted successfully!`);
} catch (err) {
console.log("Failed to save the file:", err);
console.log(`File Deleted successfully!`);
}
}
});
} else {
message.reply(`send image with caption *-sticker* `);
}
} else if (message.body === "-quote") {
const apiData = await axios.get("https://type.fit/api/quotes");
const randomNumber = Math.floor(Math.random() * apiData.data.length);
message.reply(`*${apiData.data[randomNumber].text}*`);
} else if (message.body === "-ping") {
return message.reply("pong");
} else if (message.body === "-meme") {
try {
const chat = await client.getChatById(message.from);
await sendMeme(chat);
} catch (err) {
console.log(err);
}
} else if (message.body.startsWith("-search")) {
try {
const prompt = message.body.substring(8);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `tell me ${prompt}`,
temperature: 0.7,
max_tokens: 3000,
top_p: 1.0,
frequency_penalty: 0.2,
presence_penalty: 0,
});
return await message.reply(
response.data.choices[0].text.substring(2)
);
} catch (err) {
await message.reply("Something went wrong");
console.log(err);
}
} else if (message.body === "-ping") {
return message.reply("pong");
} else if (message.body === "-help") {
return message.reply(
"Hi, I am a bot that can help you search the internet. To use me, just type -search and then your question. For example, -search what is the capital of India?"
);
} else if (message.body === "-about") {
return message.reply(
"Hi, I am a bot that can help you search the internet. To use me, just type -search and then your question. For example, -search what is the capital of India?"
);
} else if (message.body === "-test") {
return message.reply(
"Hi, I am a bot that can help you search the internet. To use me, just type -search and then your question. For example, -search what is the capital of India?"
);
} else if (message.body === "-commands") {
return message.reply(
`-search <input>
-meme
-quote
-help
-about
-sticker <in image caption>
-commands
-image <input>`
);
} else if (message.body.startsWith("-imagine")) {
try {
const prompt = message.body.substring(7);
const response = await openai.createImage({
prompt: prompt,
n: 1,
size: "1024x1024",
});
image_url = response.data.data[0].url;
const media = await MessageMedia.fromUrl(image_url);
return await client.sendMessage(message.from, message.reply(media));
} catch (error) {
await message.reply("Something went wrong");
console.log(error);
}
}
}
// Personal Chat
if (!chat.isGroup) {
if (message.hasMedia) {
message.downloadMedia().then((media) => {
if (media) {
try {
const mediaPath = "./downloaded-media/";
if (!fs.existsSync(mediaPath)) {
fs.mkdirSync(mediaPath);
}
const extension = mime.extension(media.mimetype);
const filename = new Date().getTime();
const fullFilename = mediaPath + filename + "." + extension;
// Save to file
try {
fs.writeFileSync(fullFilename, media.data, {
encoding: "base64",
});
console.log("File downloaded successfully!", fullFilename);
console.log(fullFilename);
MessageMedia.fromFilePath((filePath = fullFilename));
client.sendMessage(
message.from,
new MessageMedia(media.mimetype, media.data, filename),
{
sendMediaAsSticker: true,
}
);
fs.unlinkSync(fullFilename);
console.log(`File Deleted successfully!`);
} catch (err) {
console.log("Failed to save the file:", err.message);
console.log(`File Deleted successfully!`);
}
} catch (err) {
console.log(err.message);
}
}
});
} else if (message.body === "-quote") {
const apiData = await axios.get("https://type.fit/api/quotes");
const randomNumber = Math.floor(Math.random() * apiData.data.length);
message.reply(`*${apiData.data[randomNumber].text}*`);
} else if (message.body === "-ping") {
return message.reply("pong");
} else if (message.body === "-meme") {
try {
const chat = await client.getChatById(message.from);
await sendMeme(chat);
} catch (err) {
console.log(err);
}
} else if (message.body.startsWith("-search")) {
try {
const prompt = message.body.substring(8);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `tell me ${prompt}`,
temperature: 0.7,
max_tokens: 3000,
top_p: 1.0,
frequency_penalty: 0.2,
presence_penalty: 0,
});
return await message.reply(response.data.choices[0]);
} catch (err) {
console.log(err);
}
} else if (message.body === "-ping") {
return message.reply("pong");
} else if (message.body === "-help") {
return message.reply(
"Hi, I am a bot that can help you search the internet. To use me, just type -search and then your question. For example, -search what is the capital of India?"
);
} else if (message.body === "-about") {
return message.reply(
"Hi, I am a bot that can help you search the internet. To use me, just type -search and then your question. For example, -search what is the capital of India?"
);
} else if (message.body === "-test") {
return message.reply(
"Hi, I am a bot that can help you search the internet. To use me, just type -search and then your question. For example, -search what is the capital of India?"
);
} else if (message.body === "-commands") {
return message.reply(
`-search <input>
-meme
-quote
-help
-about
-sticker <in image caption>
-commands
-imagine <input>`
);
} else if (message.body.startsWith("-imagine")) {
try {
const prompt = message.body.substring(7);
const response = await openai.createImage({
prompt: prompt,
n: 1,
size: "1024x1024",
});
image_url = response.data.data[0].url;
const media = await MessageMedia.fromUrl(image_url);
return await client.sendMessage(message.from, message.reply(media));
} catch (error) {
await message.reply("Something went wrong");
console.log(error);
}
} else if (
!message.body === "-sticker" ||
!message.body === "-quote" ||
!message.body === "-ping" ||
!message.body === "-help" ||
!message.body === "-about" ||
!message.body === "-test" ||
!message.body === "-commands" ||
!message.body.startsWith("-imagine")
) {
try {
const prompt = message.body;
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: prompt,
temperature: 0.7,
max_tokens: 3000,
top_p: 1.0,
frequency_penalty: 0.2,
presence_penalty: 0,
});
return await message.reply(response.data.choices[0].text);
} catch (err) {
await message.reply("Something went wrong");
console.log(err);
}
}
}
});
client.initialize();
});
const connection = mongoose.connection;
connection.once("open", () => {
console.log("MongoDB database connection established successfully");
});