|
|
|
@ -11,6 +11,7 @@ const {
|
|
|
|
|
const { writeFile } = require('fs/promises');
|
|
|
|
|
const waEmitter = require('../emitter');
|
|
|
|
|
|
|
|
|
|
const { formatPhoneNumber, parsePhoneNumber, formatStatus, formatTimestamp } = require('./common');
|
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
|
const P = require('pino');
|
|
|
|
@ -18,12 +19,8 @@ const P = require('pino');
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs.txt'));
|
|
|
|
|
logger.level = 'trace';
|
|
|
|
|
|
|
|
|
|
// external map to store retry counts of messages when decryption/encryption fails
|
|
|
|
|
// keep this out of the socket itself, so as to prevent a message decryption/encryption loop across socket restarts
|
|
|
|
|
const msgRetryCounterCache = new NodeCache();
|
|
|
|
|
|
|
|
|
|
// the store maintains the data of the WA connection in memory
|
|
|
|
|
// can be written out to a file & read from it
|
|
|
|
|
const store = makeInMemoryStore({ logger });
|
|
|
|
|
store?.readFromFile('./baileys_store_multi.json');
|
|
|
|
|
// save every 10s
|
|
|
|
@ -37,8 +34,10 @@ waEmitter.on('message:updated', event => {
|
|
|
|
|
waEmitter.on('message:received', event => {
|
|
|
|
|
console.info('msg:event', event);
|
|
|
|
|
});
|
|
|
|
|
waEmitter.on('connection.open', event => {
|
|
|
|
|
console.info('msg:event', event);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// start a connection
|
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
|
const channelId = generateId();
|
|
|
|
|
const whatsAppNo = phone;
|
|
|
|
@ -47,64 +46,38 @@ const createWhatsApp = async phone => {
|
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
|
|
|
|
const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out');
|
|
|
|
|
|
|
|
|
|
const formatPhoneNumber = number => {
|
|
|
|
|
if (number === null || number === undefined) return '';
|
|
|
|
|
|
|
|
|
|
if (number.indexOf('@g.us') > -1) {
|
|
|
|
|
return number;
|
|
|
|
|
} else if (number.indexOf('@broadcast') > -1) {
|
|
|
|
|
return number;
|
|
|
|
|
} else {
|
|
|
|
|
return number + '@s.whatsapp.net';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const parsePhoneNumber = number => {
|
|
|
|
|
if (number === null || number === undefined) return '';
|
|
|
|
|
|
|
|
|
|
if (number.indexOf('@s.whatsapp.net') > -1) {
|
|
|
|
|
return number.split('@')[0];
|
|
|
|
|
} else {
|
|
|
|
|
return number;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// status: sent read delivered failed
|
|
|
|
|
// 2 sent, 3 delivered, 4 read, 0 error
|
|
|
|
|
// Time: 2008-07-07 15:37:07
|
|
|
|
|
|
|
|
|
|
const formatStatus = number => {
|
|
|
|
|
if (number === 2) return 'sent';
|
|
|
|
|
else if (number === 3) return 'delivered';
|
|
|
|
|
else if (number === 4) return 'read';
|
|
|
|
|
else if (number === 0) return 'error';
|
|
|
|
|
else return 'played';
|
|
|
|
|
};
|
|
|
|
|
const formatTimestamp = timestamp => {
|
|
|
|
|
if (timestamp === null) return '';
|
|
|
|
|
|
|
|
|
|
const datetime = new Date(timestamp * 1000);
|
|
|
|
|
|
|
|
|
|
return datetime.getFullYear() + '-' + (datetime.getMonth() + 1) + '-' + datetime.getDay() + ' ' + datetime.getHours() + ':' + datetime.getMinutes() + ':' + datetime.getSeconds();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sendTextMessage = (whatsAppNo, content) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
console.info('formatPhoneNumber: ', number);
|
|
|
|
|
waSocket.sendMessage(number, { text: content });
|
|
|
|
|
try {
|
|
|
|
|
waSocket.sendMessage(number, { text: content });
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: '发送文本消息出错',
|
|
|
|
|
error: ex
|
|
|
|
|
})
|
|
|
|
|
console.error('发送文本消息出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sendImageMessage = (whatsAppNo, imageUrl) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
waSocket.sendMessage(number, {
|
|
|
|
|
image: { url: imageUrl },
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
waSocket.sendMessage(number, {
|
|
|
|
|
image: { url: imageUrl },
|
|
|
|
|
});
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: '发送图片消息出错',
|
|
|
|
|
error: ex
|
|
|
|
|
})
|
|
|
|
|
console.error('发送图片消息出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let waSocket = null;
|
|
|
|
|
|
|
|
|
|
const start = () => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
waSocket = makeWASocket({
|
|
|
|
|
version,
|
|
|
|
|
logger,
|
|
|
|
@ -128,6 +101,8 @@ const createWhatsApp = async phone => {
|
|
|
|
|
if (connection === 'close') {
|
|
|
|
|
console.log('链接断开:', lastDisconnect);
|
|
|
|
|
if (lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
|
|
|
|
|
console.log('正在重连:');
|
|
|
|
|
start();
|
|
|
|
|
} else {
|
|
|
|
|
waSocket.end(error => console.error('end.error: ', error));
|
|
|
|
@ -135,8 +110,9 @@ const createWhatsApp = async phone => {
|
|
|
|
|
console.log('Connection closed. You are logged out.');
|
|
|
|
|
}
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
|
console.info('链接成功');
|
|
|
|
|
waEmitter.emit('connection.open', waSocket);
|
|
|
|
|
waEmitter.emit('connection.open', {
|
|
|
|
|
status: 'online',
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
resolve(qr);
|
|
|
|
@ -172,24 +148,11 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
|
if (msg.key.remoteJid.indexOf('@g.us') > -1) {
|
|
|
|
|
const metadata = await waSocket.groupMetadata(msg.key.remoteJid);
|
|
|
|
|
console.log('群: ' + metadata.subject + ', description: ' + metadata.desc);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(msg.key.remoteJid);
|
|
|
|
|
console.log('群头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('群头像出错了: ', ex);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.log('remoteJid: ', msg.key.remoteJid);
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(msg.key.remoteJid);
|
|
|
|
|
console.log('个人头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('个人头像出错了: ', ex);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(msg.key.remoteJid);
|
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
@ -200,7 +163,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
} else if (text.indexOf('文本') > -1) {
|
|
|
|
|
sendTextMessage(fromWhatsAppNo, '这是文本消息:' + new Date().toString());
|
|
|
|
|
} else if (text.indexOf('发群') > -1) {
|
|
|
|
|
sendTextMessage('120363335516526642@g.us', '这是群消息:' + new Date().toString());
|
|
|
|
|
sendTextMessage('120363363417115199@g.us', '这是群消息:' + new Date().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
@ -223,6 +186,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
waEmitter.emit('message:received', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
status: '',
|
|
|
|
|
direction: 'inbound',
|
|
|
|
|
from: fromWhatsAppNo,
|
|
|
|
|
to: whatsAppNo,
|
|
|
|
|
type: 'text',
|
|
|
|
|