-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (101 loc) · 4.33 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
import { DisconnectReason, fetchLatestBaileysVersion, useMultiFileAuthState } from "@whiskeysockets/baileys";
import { MongoClient } from "mongodb";
import 'dotenv/config'
import * as baileys from '@whiskeysockets/baileys';
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import chalk from "chalk";
import pino from "pino";
import getWeather from "./API_module/weatherAPI.js";
import { connectAuth, connectNotes } from "./Utils/mongo.js";
import useMongoDbAuthState from "./mongoDbAuthState.js";
import { handleAddNote, handleDeleteNotes, handleGetNotes } from "./API_module/notesAPI.js";
import { tagAll } from "./API_module/tagall.js";
const usePairingCode = process.argv.includes('--use-pairing-code');
const rl = readline.createInterface({input,output});
async function connectToWhatsApp () {
const Note = await connectNotes();
const authCollection = await connectAuth();
const {version, isLatest} = await fetchLatestBaileysVersion();
console.log(chalk.yellow(`using WA v${version.join('.')}, isLatest: ${isLatest}`))
const {state, saveCreds} = await useMongoDbAuthState(authCollection);
// const {state, saveCreds} = await useMultiFileAuthState("auth_info_baileys");
const sock = baileys.makeWASocket({
version,
logger: pino({level: 'silent'}),
auth: state,
browser: baileys.Browsers.windows('Firefox'),
printQRInTerminal: !usePairingCode,
generateHighQualityLinkPreview: true,
})
if(usePairingCode && !sock.authState.creds.registered){
const phoneNumber = await rl.question("Please enter your mobile phoneNumber");
const code = await sock.requestPairingCode(phoneNumber);
console.log(`pairing code : ${code}`)
}
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update;
if(connection === 'close') {
const shouldReconnect = (lastDisconnect.error)?.output?.statusCode !== DisconnectReason.loggedOut
console.log('connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
// reconnect if not logged out
if(shouldReconnect) {
connectToWhatsApp()
}
} else if(connection === 'open') {
console.log(chalk.greenBright('opened connection'))
}
})
sock.ev.on("creds.update", saveCreds);
// sock.ev.on("messages.update", (m) => {
// console.log( chalk.blue('\n its me\n'),m);
// })
console.log('listening')
sock.ev.on('messages.upsert', async (m) => {
let responseData ;
if (m.messages[0].message){
// console.log((m.messages[0].message), ' from ', JSON.stringify(m,null,2))
let message = m.messages[0]?.message?.conversation || m.messages[0]?.message?.extendedTextMessage?.text;
if (message?.toLowerCase()?.trim()?.startsWith('@gwybot')){
let chatJid = m.messages[0].key.remoteJid;
let cmdStringArray = message?.split(' ')
if( cmdStringArray.length > 1 ){
let command = cmdStringArray[1]?.toLowerCase();
console.log(command);
switch (command){
case 'weather':
getWeather(cmdStringArray[2] , sock , m.messages[0].key.remoteJid );
break;
case 'addnote':
handleAddNote(m,Note,sock);
break;
case 'getnotes':
handleGetNotes(m,Note,sock);
break;
case 'deletenote':
handleDeleteNotes(m,Note,sock);
break;
case 'tagall':
tagAll(m,sock);
break;
default :
sock.sendMessage(chatJid, {text: 'Hello!,\n Gwybot here, this might not be valid command (till now atleast :) )'});
break;
}
}else {
sock.sendMessage(chatJid, {text: 'Hello I am GwyBot, Made By Abhishek Kumar and Ranjay Singh'});
}
}
// console.log(chalk.red('\nBoomBurst\n') , 'replying to')
// await sock.sendMessage(m.messages[0].key.remoteJid, { text: 'Hello there!' })
}
else{
// console.log(chalk.red('\nBoomBurst\n') , 'replying to' , JSON.stringify(m , undefined , 2 ))
// let userjid = m.messages[0].key.remoteJid.includes('@s.whatsapp.net') ? m.messages[0].key.remoteJid : m.messages[0].key.participant ;
if(!m.messages[0].key.fromMe){
}
}
})
}
// run in main file
connectToWhatsApp()