|
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'; |
11 | 7 |
|
12 | 8 | 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"); |
14 | 21 | const client = makeWASocket({ |
15 | 22 | version: (await fetchLatestBaileysVersion()).version, |
16 | 23 | auth: state, |
17 | 24 | logger: P({ level: 'silent' }), |
18 | 25 | 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 | + }; |
20 | 33 |
|
21 | | - //connection updates |
22 | 34 | 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!!`); |
25 | 37 | 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); |
28 | 40 | 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); |
33 | 45 | } |
34 | 46 | } |
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 | + |
39 | 51 | 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`; |
49 | 59 | } |
50 | 60 |
|
| 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 | + }); |
51 | 75 |
|
52 | | - }) |
53 | | - client.ev.on('creds.update', saveCreds) |
54 | | -} |
| 76 | + client.ev.on('creds.update', saveCreds); |
| 77 | +}; |
55 | 78 |
|
56 | | -start() |
| 79 | +start(); |
0 commit comments