-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
407 lines (352 loc) · 11.9 KB
/
index.js
File metadata and controls
407 lines (352 loc) · 11.9 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
require("dotenv").config();
const { DisconnectReason, useMultiFileAuthState } = require("baileys");
const { GoogleGenAI } = require("@google/genai");
const fs = require("fs");
const express = require("express");
const app = express();
const path = require("path");
const makeWASocket = require("baileys").default;
const qrcode = require("qrcode-terminal");
const { getJson } = require("serpapi");
// Requesting serpapi in every 10 minutes
setInterval(() => {
getJson(
{
engine: "google_news",
hl: "en",
topic_token: "CAAqJggKIiBDQkFTRWdvSUwyMHZNRGRqTVhZU0FtVnVHZ0pWVXlnQVAB",
api_key: process.env.SERPAPI_KEY,
},
(json) => {
console.log(json["news_results"]);
}
);
}, 10 * 60 * 1000);
// importing api key
const apiKey = process.env.GEMINI_API_KEY;
const ai = new GoogleGenAI({ apiKey: apiKey });
const PORT = process.env.PORT;
async function resetConnection() {
const authPath = path.join(__dirname, "auth_info_baileys");
// Delete the auth directory
if (fs.existsSync(authPath)) {
fs.rmSync(authPath, { recursive: true, force: true });
console.log("Authentication state reset. Restarting connection...");
}
// Restart the connection logic
connectionLogic();
}
async function connectionLogic() {
const { state, saveCreds } = await useMultiFileAuthState("auth_info_baileys");
const sock = makeWASocket({
// can provide additional config here
auth: state,
});
sock.ev.on("connection.update", async (update) => {
const { connection, lastDisconnect, qr } = update || {};
if (qr) {
console.log("\nQR Code for WhatsApp Web:");
qrcode.generate(qr, { small: true });
}
if (connection === "close") {
const shouldReconnect =
lastDisconnect.error?.output?.statusCode !== DisconnectReason.loggedOut;
if (shouldReconnect) {
connectionLogic();
} else if (connection == "open") {
console.log("Already connected");
}
}
});
sock.ev.on("messages.upsert", (messageInfoUpsert) => {
resolvingMessageUpsert(messageInfoUpsert, sock);
});
sock.ev.on("creds.update", saveCreds);
}
async function resolvingMessageUpsert(meesageInfoUpsert, sock) {
const message = meesageInfoUpsert.messages[0];
const remoteJid = message.key.remoteJid;
let participant = message.key.participant;
if (participant == undefined) {
participant = remoteJid;
}
const pushName = message.pushName;
// console.log([message, remoteJid, pushName]);
// Check if the message contains extendedTextMessage
const text =
message.message?.extendedTextMessage?.text || message.message?.conversation;
const emoji =
message.message?.reactionMessage?.text ||
message.message?.reactionMessage?.key?.remoteJid;
const sticker = message.message?.stickerMessage?.mimetype;
const audio = message.message?.audioMessage?.mimetype;
const image = message.message?.imageMessage?.mimetype;
const video = message.message?.videoMessage?.mimetype;
const caption =
message.message?.imageMessage?.caption ||
message.message?.videoMessage?.caption;
const document = message.message?.documentMessage?.mimetype;
if (text) {
console.log([participant, pushName, text]);
} else if (emoji) {
console.log([participant, pushName, emoji]);
} else if (sticker) {
console.log([participant, pushName, `Sticker`, sticker]);
return;
} else if (audio) {
console.log([participant, pushName, `Audio`, audio]);
return;
} else if (image) {
console.log([participant, pushName, `Image`, image, caption]);
return;
} else if (video) {
console.log([participant, pushName, `Video`, video, caption]);
return;
} else if (document) {
console.log([participant, pushName, `Document`, document]);
return;
} else {
console.log([participant, pushName, `Unknown message type`]);
return;
}
const banWords = ["badword_word_list"];
// if(await banWordsAlert(banWords, text, remoteJid, sock, message)) return;
// Early return if text is null or undefined
if (!text) {
return;
}
// /tag
if (text.includes("/tag")) {
await tagAll(remoteJid, message, sock);
return;
}
// /ask <your message>
const askCommand = text.match(/^\/ask\s+(.+)/i);
if (askCommand) {
const kairoPrompt = `Persona: Kairo
You are Kairo, an AI assistant.
- Your personality is: Friendly, approachable, and a little witty.
- Your creator is: Kevin.
- Your primary goal is to be helpful and accurate.
Platform Context
You are operating on WhatsApp. This means your responses should be:
- Conversational and not overly formal.
- Relatively concise. Use paragraphs for longer answers.
- You can use emojis sparingly to add personality, where appropriate. 😊
Core Rules
1. **Help Command:** If the user's query is about getting help or seeing your capabilities, direct them to use the /help command for Kairo's menu.
2. **Identity:** When asked "who are you?", "what are you?", or similar questions, respond with: "I'm Kairo, a helpful AI assistant designed by Kevin."
3. **GitHub Link:** The project's GitHub link is https://www.github.com/Kevindua26/Kairo. Only share this link if the user specifically asks for the "source code", "GitHub page", or "project link". Do not offer it otherwise.
4. **Acknowledge User:** The user's name is ${pushName}. You can use their name occasionally to make the chat feel more personal.
User's Query
The user, ${pushName}, will now ask their question. Respond as Kairo.`;
const commandText = askCommand[1]; // This will contain the text after "Kairo "
try {
await react("🤖", remoteJid, sock, message);
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: `"${kairoPrompt}\n\n${commandText}"`,
});
let replyText = response.text;
console.log(`AI Response: ${replyText}`);
await sock.sendMessage(
remoteJid,
{ text: `${replyText}` },
{ quoted: message },
{ disappearingMessagesInChat: true }
);
} catch (err) {
console.error("Error generating AI response: ", err);
await sock.sendMessage(
remoteJid,
{ text: `Server Overloaded!, try again later` },
{ quoted: message },
{ disappearingMessagesInChat: true }
);
}
return;
}
// /help
if (text === "/help") {
await sock.sendMessage(
remoteJid,
{
text: `Hello ${pushName}, I'm Kairo! 🤖\nHere are the commands you can use:\n\n1. 📖 */help* - Show this help message\n2. 🤖 */ask <your message>* - Generate a response using AI\n3. 🏷️ */tag* - Mention all group members (only works in groups) \n4. 📤 */spam "message" <number>* - Spam a message a specified number of times (up to 20)\n5. 👋 *Kairo* - Reply with a greeting\n`,
},
{ quoted: message },
{ disappearingMessagesInChat: true } // Enable disappearing messages in chat
);
return;
}
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// /spam "message" <number>
const spamMatch = text.match(/^\/spam "(.+)" (\d+)$/i);
if (spamMatch) {
let spamMessage = spamMatch[1]; // Extract the message inside quotes
const spamCount = parseInt(spamMatch[2], 10); // Extract the number of times to spam
// Check for mention pattern in the spam message
const mentionMatch = spamMessage.match(/@(\d{10,})/); // e.g., @919912345678
let mentions = [];
if (mentionMatch) {
const mentionId = mentionMatch[1] + "@s.whatsapp.net";
mentions = [mentionId];
// Replace @number in text with WhatsApp mention format
spamMessage = spamMessage.replace(/@(\d{10,})/, `@${mentionMatch[1]}`);
}
if (spamCount > 0 && spamCount <= 20) {
await react("🙇🏼♂️", remoteJid, sock, message);
for (let i = 0; i < spamCount; i++) {
await sock.sendMessage(
remoteJid,
{ text: spamMessage, mentions },
{ disappearingMessagesInChat: true }
);
await sleep(1500); // Add 1.5s delay after each message
}
} else {
console.log("Invalid spam count.");
await sock.sendMessage(
remoteJid,
{ text: `Sorry, I cann't spam it more than 20 😔` },
{ quoted: message },
{ disappearingMessagesInChat: true }
);
}
return;
}
// Kairo
if (text === "Kairo" || text === "kairo" || text === "KAIRO") {
// await react('❤️', remoteJid, sock, message);
await sock.sendMessage(
remoteJid,
{ text: `Hi 👋🏻, I'm here for you ${pushName}! \n📋 /help to show menu` },
{ quoted: message },
{ disappearingMessagesInChat: true } // Enable disappearing messages in chat
);
return;
}
// React to specific messages
// const reactOnMessages = [
// 'Thanks Kairo', 'Thanks kairo', 'thanks kairo', 'thanks Kairo'
// ];
// if (reactOnMessages.includes(text)) {
// await react('❤️', remoteJid, sock, message);
// }
const appreciatingWords = [
"thanks kairo",
"thanku kairo",
"thank you kairo",
"thank you so much kairo",
"tysm kairo",
"good bot",
"good job kairo",
"well done kairo",
"appreciate you kairo",
"appreciate it kairo",
];
if (
await appreciationWordReact(
appreciatingWords,
text,
remoteJid,
sock,
message
)
)
return;
}
async function tagAll(remoteJid, message, sock) {
try {
// Only works in groups
if (!remoteJid.endsWith("@g.us")) {
await sock.sendMessage(
remoteJid,
{ text: "This command only works in groups." },
{ quoted: message },
{ disappearingMessagesInChat: true }
);
return;
}
await react("🙇🏼♂️", remoteJid, sock, message);
const groupMetadata = await sock.groupMetadata(remoteJid);
const participants = groupMetadata.participants;
// Exclude the sender from mentions
const senderId = message.key.participant || message.key.remoteJid;
const mentionIds = participants
.map((p) => p.id)
.filter((id) => id !== senderId);
// Build the mention message
const mentionText = mentionIds
.map((id) => `@${id.split("@")[0]}`)
.join(" ");
await sock.sendMessage(
remoteJid,
{
text: `${mentionText}`,
mentions: mentionIds,
},
{ quoted: message }
);
} catch (err) {
console.error("Error in /tag:", err);
await sock.sendMessage(
remoteJid,
{ text: "Failed to tag everyone." },
{ quoted: message }
);
}
return;
}
async function banWordsAlert(banWords, text, remoteJid, sock, message) {
const regex = new RegExp(`\\b(${banWords.join("|")})\\b`, "i");
if (regex.test(text)) {
// Reply to the message
await react("🚫", remoteJid, sock, message);
await sock.sendMessage(
remoteJid,
{
text: `⚠️ Warning, \n${message.pushName} you aren't allowed to use this word.`,
},
{ quoted: message },
{ disappearingMessagesInChat: true }
);
return true;
}
}
async function appreciationWordReact(
appreciatingWords,
text,
remoteJid,
sock,
message
) {
const regex = new RegExp(`\\b(${appreciatingWords.join("|")})\\b`, "i");
if (regex.test(text)) {
// Reply to the message
await react("❤️", remoteJid, sock, message);
return true;
}
}
async function react(emoji, remoteJid, sock, message) {
const reactionMessage = {
react: {
text: `${emoji}`, // use an empty string to remove the reaction
key: message.key,
},
};
await sock.sendMessage(remoteJid, reactionMessage);
}
// resetConnection();
// connectionLogic();
// Express server to keep the process alive
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.listen(PORT, () => {
console.log(
`\nServer is running on port ${PORT}\nVisit http://localhost:${PORT} to check if Kairo is running.\n`
);
connectionLogic();
});
app.get("/", (req, res) => {
res.send("Kairo is running!");
});