-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
247 lines (232 loc) · 9.27 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
const {
default: connectToWhatsApp,
useMultiFileAuthState,
makeInMemoryStore,
jidDecode,
proto,
getContentType,
DisconnectReason
} = require("@whiskeysockets/baileys");
const {
Boom
} = require('@hapi/boom');
const pino = require('pino');
const qrCodeTerminal = require('qrcode-terminal');
const chalk = require('chalk');
const config = require('./config/mainConfig.json');
const store = makeInMemoryStore({
logger: pino().child({
level: 'silent',
stream: 'store'
})
});
const smsg = (conn, m, store) => {
if (!m) return m;
let M = proto.WebMessageInfo;
if (m.key) {
m.id = m.key.id;
m.isBaileys = m.id.startsWith('BAE5') && m.id.length === 16;
m.chat = m.key.remoteJid;
m.fromMe = m.key.fromMe;
m.isGroup = m.chat.endsWith('@g.us');
m.sender = conn.decodeJid(m.fromMe && conn.user.id || m.participant || m.key.participant || m.chat || '');
if (m.isGroup) m.participant = conn.decodeJid(m.key.participant) || '';
}
if (m.message) {
m.mtype = getContentType(m.message);
m.msg = (m.mtype == 'viewOnceMessage' ? m.message[m.mtype].message[getContentType(m.message[m.mtype].message)] : m.message[m.mtype]);
m.body = m.message.conversation || m.msg.caption || m.msg.text || (m.mtype == 'listResponseMessage') && m.msg.singleSelectReply.selectedRowId || (m.mtype == 'buttonsResponseMessage') && m.msg.selectedButtonId || (m.mtype == 'viewOnceMessage') && m.msg.caption || m.text;
let quoted = m.quoted = m.msg.contextInfo ? m.msg.contextInfo.quotedMessage : null;
m.mentionedJid = m.msg.contextInfo ? m.msg.contextInfo.mentionedJid : [];
if (m.quoted) {
let type = Object.keys(m.quoted)[0];
m.quoted = m.quoted[type];
if (['productMessage'].includes(type)) {
type = Object.keys(m.quoted)[0];
m.quoted = m.quoted[type];
}
if (typeof m.quoted === 'string') m.quoted = {
text: m.quoted
};
m.quoted.mtype = type;
m.quoted.id = m.msg.contextInfo.stanzaId;
m.quoted.chat = m.msg.contextInfo.remoteJid || m.chat;
m.quoted.isBaileys = m.quoted.id ? m.quoted.id.startsWith('BAE5') && m.quoted.id.length === 16 : false;
m.quoted.sender = conn.decodeJid(m.msg.contextInfo.participant);
m.quoted.fromMe = m.quoted.sender === conn.decodeJid(conn.user.id);
m.quoted.text = m.quoted.text || m.quoted.caption || m.quoted.conversation || m.quoted.contentText || m.quoted.selectedDisplayText || m.quoted.title || '';
m.quoted.mentionedJid = m.msg.contextInfo ? m.msg.contextInfo.mentionedJid : [];
m.getQuotedObj = m.getQuotedMessage = async () => {
if (!m.quoted.id) return false;
let q = await store.loadMessage(m.chat, m.quoted.id, conn);
return exports.smsg(conn, q, store);
};
let vM = m.quoted.fakeObj = M.fromObject({
key: {
remoteJid: m.quoted.chat,
fromMe: m.quoted.fromMe,
id: m.quoted.id
},
message: quoted,
...(m.isGroup ? {
participant: m.quoted.sender
} : {})
});
m.quoted.delete = () => conn.sendMessage(m.quoted.chat, {
delete: vM.key
});
m.quoted.copyNForward = (jid, forceForward = false, options = {}) => conn.copyNForward(jid, vM, forceForward, options);
m.quoted.download = () => conn.downloadMediaMessage(m.quoted);
}
}
if (m.msg.url) m.download = () => conn.downloadMediaMessage(m.msg);
m.text = m.msg.text || m.msg.caption || m.message.conversation || m.msg.contentText || m.msg.selectedDisplayText || m.msg.title || '';
m.reply = (text, chatId = m.chat, options = {}) => Buffer.isBuffer(text) ? conn.sendMedia(chatId, text, 'file', '', m, {
...options
}) : conn.sendText(chatId, text, m, {
...options
});
m.copy = () => exports.smsg(conn, M.fromObject(M.toObject(m)));
m.copyNForward = (jid = m.chat, forceForward = false, options = {}) => conn.copyNForward(jid, m, forceForward, options);
conn.appenTextMessage = async (text, chatUpdate) => {
let messages = await generateWAMessage(m.chat, {
text: text,
mentions: m.mentionedJid
}, {
userJid: conn.user.id,
quoted: m.quoted && m.quoted.fakeObj
});
messages.key.fromMe = areJidsSameUser(m.sender, conn.user.id);
messages.key.id = m.key.id;
messages.pushName = m.pushName;
if (m.isGroup) messages.participant = m.sender;
let msg = {
...chatUpdate,
messages: [proto.WebMessageInfo.fromObject(messages)],
type: 'append'
};
conn.ev.emit('messages.upsert', msg);
};
return m;
};
async function startServer() {
const {
state,
saveCreds
} = await useMultiFileAuthState(`./${config.session_folder_name}`);
const sock = connectToWhatsApp({
logger: pino({
level: 'silent'
}),
printQRInTerminal: true,
browser: ["WhatsApp Bot", "Chrome", "1.0.0"],
auth: state
});
store.bind(sock.ev);
sock.ev.on('messages.upsert', async chatUpdate => {
try {
mek = chatUpdate.messages[0];
if (!mek.message) return;
mek.message = (Object.keys(mek.message)[0] === 'ephemeralMessage') ? mek.message.ephemeralMessage.message : mek.message;
if (mek.key && mek.key.remoteJid === 'status@broadcast') return;
if (!sock.public && !mek.key.fromMe && chatUpdate.type === 'notify') return;
if (mek.key.id.startsWith('BAE5') && mek.key.id.length === 16) return;
m = smsg(sock, mek, store);
require('./response/client.js')(sock, m, chatUpdate, store);
} catch (err) {
console.log(err);
}
});
sock.decodeJid = (jid) => {
if (!jid) return jid;
if (/:\d+@/gi.test(jid)) {
let decode = jidDecode(jid) || {};
return decode.user && decode.server && decode.user + '@' + decode.server || jid;
} else return jid;
};
sock.public = config.public_mode;
sock.serializeM = (m) => smsg(sock, m, store);
sock.ev.on('connection.update', async (update) => {
const {
connection,
lastDisconnect
} = update;
if (connection === 'close') {
let reason = new Boom(lastDisconnect?.error)?.output.statusCode;
if (reason === DisconnectReason.badSession) {
console.log(`Bad Session File, Please Delete Session and Scan Again`);
sock.logout();
} else if (reason === DisconnectReason.connectionClosed) {
console.log("Connection closed, reconnecting....");
startServer();
} else if (reason === DisconnectReason.connectionLost) {
console.log("Connection Lost from Server, reconnecting...");
startServer();
} else if (reason === DisconnectReason.connectionReplaced) {
console.log("Connection Replaced, Another New Session Opened, Please Close Current Session First");
sock.logout();
} else if (reason === DisconnectReason.loggedOut) {
console.log(`Device Logged Out, Please Scan Again And Run.`);
sock.logout();
} else if (reason === DisconnectReason.restartRequired) {
console.log("Restart Required, Restarting...");
startServer();
} else if (reason === DisconnectReason.timedOut) {
console.log("Connection TimedOut, Reconnecting...");
startServer();
} else if (reason === DisconnectReason.Multidevicemismatch) {
console.log("Multi device mismatch, please scan again");
sock.logout();
} else sock.end(`Unknown DisconnectReason: ${reason}|${connection}`);
} else if (connection === "open") {
console.log(chalk.bold(chalk.cyan.blue('• User Info')));
console.log(chalk.cyan(`- Name : ${sock.user.name}`));
console.log(chalk.cyan(`- Number : ${sock.user.id.split(':')[0]}`));
console.log(chalk.cyan(`- Status : Connected`));
}
});
sock.sendText = (jid, teks, quoted = '', options) => {
return sock.sendMessage(jid, {
text: teks,
...options
}, {
quoted,
...options
})
};
sock.sendImage = async (jid, path, caption = '', quoted = '', options) => {
let buffer = Buffer.isBuffer(path) ? path : /^data:.*?\/.*?;base64,/i.test(path) ? Buffer.from(path.split`,` [1], 'base64') : /^https?:\/\//.test(path) ? await (await getBuffer(path)) : fs.existsSync(path) ? fs.readFileSync(path) : Buffer.alloc(0);
return await sock.sendMessage(jid, {
image: buffer,
caption: caption,
jpegThumbnail: '',
...options
}, {
quoted
});
};
sock.sendVideo = async (jid, path, caption = '', quoted = '', gif = false, options) => {
let buffer = Buffer.isBuffer(path) ? path : /^data:.*?\/.*?;base64,/i.test(path) ? Buffer.from(path.split`,` [1], 'base64') : /^https?:\/\//.test(path) ? await (await getBuffer(path)) : fs.existsSync(path) ? fs.readFileSync(path) : Buffer.alloc(0);
return await sock.sendMessage(jid, {
video: buffer,
caption: caption,
gifPlayback: gif,
jpegThumbnail: '',
...options
}, {
quoted
});
};
sock.sendAudio = async (jid, path, quoted = '', ptt = false, options) => {
let buffer = Buffer.isBuffer(path) ? path : /^data:.*?\/.*?;base64,/i.test(path) ? Buffer.from(path.split`,` [1], 'base64') : /^https?:\/\//.test(path) ? await (await getBuffer(path)) : fs.existsSync(path) ? fs.readFileSync(path) : Buffer.alloc(0);
return await sock.sendMessage(jid, {
audio: buffer,
ptt: ptt,
...options
}, {
quoted
});
};
return sock;
}
startServer();