Skip to content

Commit 9a44b1a

Browse files
authored
Add files via upload
1 parent ab5e57c commit 9a44b1a

File tree

1 file changed

+61
-38
lines changed

1 file changed

+61
-38
lines changed

index.js

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,79 @@
1-
import ws from '@whiskeysockets/baileys'
2-
const { default: makeWASocket, DisconnectReason, useMultiFileAuthState, fetchLatestBaileysVersion } = ws
3-
import { remove } from 'fs-extra'
4-
import { serialize } from './lib/waClient.js'
5-
import * as dotenv from 'dotenv'
6-
import axios from 'axios'
7-
import P from 'pino'
8-
import { Boom } from '@hapi/boom'
9-
10-
dotenv.config()
1+
import ws from '@whiskeysockets/baileys';
2+
import { remove } from 'fs-extra';
3+
import { serialize } from './lib/waClient.js';
4+
import axios from 'axios';
5+
import P from 'pino';
6+
import { Boom } from '@hapi/boom';
117

128
const start = async () => {
13-
const { state, saveCreds } = await useMultiFileAuthState("session")
9+
console.log('\x1b[36m%s\x1b[0m', '╭───────────────────────────────────────╮');
10+
console.log('\x1b[36m%s\x1b[0m', '│ 🤖 \x1b[35mWahGpt-Bot\x1b[0m 🤖 │');
11+
console.log('\x1b[36m%s\x1b[0m', '│ │');
12+
console.log('\x1b[36m%s\x1b[0m', '│ \x1b[35mWhatsApp Chat Bot with ChatGPT \x1b[0m │');
13+
console.log('\x1b[36m%s\x1b[0m', '│ \x1b[35mIntegration\x1b[0m │');
14+
console.log('\x1b[36m%s\x1b[0m', '│ │');
15+
console.log('\x1b[36m%s\x1b[0m', '╰───────────────────────────────────────╯');
16+
17+
const { useMultiFileAuthState, fetchLatestBaileysVersion } = ws;
18+
const { default: makeWASocket, DisconnectReason } = ws;
19+
20+
const { state, saveCreds } = await useMultiFileAuthState("session");
1421
const client = makeWASocket({
1522
version: (await fetchLatestBaileysVersion()).version,
1623
auth: state,
1724
logger: P({ level: 'silent' }),
1825
printQRInTerminal: true
19-
})
26+
});
27+
28+
const colors = {
29+
user: '\x1b[36m', // Cyan color for user messages
30+
bot: '\x1b[35m', // Magenta color for bot messages
31+
reset: '\x1b[0m' // Reset color
32+
};
2033

21-
//connection updates
2234
client.ev.on('connection.update', async (update) => {
23-
const { connection, lastDisconnect } = update
24-
if (update.qr) console.log(`Scan the QR code!!`)
35+
const { connection, lastDisconnect } = update;
36+
if (update.qr) console.log(`📱 Scan the QR code!!`);
2537
if (connection === 'close') {
26-
const { statusCode } = new Boom(lastDisconnect?.error).output
27-
if (statusCode !== DisconnectReason.loggedOut) setTimeout(() => start(), 3000)
38+
const { statusCode } = new Boom(lastDisconnect?.error).output;
39+
if (statusCode !== DisconnectReason.loggedOut) setTimeout(() => start(), 3000);
2840
else {
29-
console.log('Disconnected :"(')
30-
await remove("session")
31-
console.log('Starting...')
32-
setTimeout(() => start(), 3000)
41+
console.log('Disconnected :("');
42+
await remove("session");
43+
console.log('🔄 Restarting...');
44+
setTimeout(() => start(), 3000);
3345
}
3446
}
35-
if (connection === 'connecting') console.log('Connecting to WhatsApp!!')
36-
if (connection === 'open') console.log('Connected to WhatsApp')
37-
38-
})
47+
if (connection === 'connecting') console.log('🔗 Connecting to WhatsApp!!');
48+
if (connection === 'open') console.log('Connected to WhatsApp');
49+
});
50+
3951
client.ev.on('messages.upsert', async ({ messages, type }) => {
40-
if (type !== 'notify') return
41-
const M = serialize(JSON.parse(JSON.stringify(messages[0])), client)
42-
if (M.quoted?.participant) M.mentions.push(M.quoted.participant)
43-
console.log(M.body)
44-
if (
45-
M.mentions.includes(client.user.id.split(':')[0] + '@s.whatsapp.net')
46-
) {
47-
const text = await axios.get(`https://oni-chan-unique-api.vercel.app/gpt4?text=${M.body}`)
48-
M.reply(text.data.result)
52+
if (type !== 'notify') return;
53+
54+
const M = serialize(JSON.parse(JSON.stringify(messages[0])), client);
55+
56+
let response = '';
57+
if (M.quoted) {
58+
response += `Quoted message: ${M.quoted.body}\n\n`;
4959
}
5060

61+
try {
62+
const text = await axios.get(`https://oni-chan-unique-api.vercel.app/gpt4?text=${M.body}`);
63+
const botResponse = text.data.result;
64+
response += `*🤖Bot:* ${botResponse}`;;
65+
66+
console.log(`${colors.user}👤 User: ${M.body}${colors.reset}`);
67+
console.log(`${colors.bot}🤖 Bot: ${botResponse}${colors.reset}`);
68+
69+
M.reply(response);
70+
} catch (error) {
71+
console.error('❌ Error:', error.message);
72+
M.reply('Sorry, an error occurred while processing your request.');
73+
}
74+
});
5175

52-
})
53-
client.ev.on('creds.update', saveCreds)
54-
}
76+
client.ev.on('creds.update', saveCreds);
77+
};
5578

56-
start()
79+
start();

0 commit comments

Comments
 (0)