|
|
|
@ -17,9 +17,6 @@ const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
|
const P = require('pino');
|
|
|
|
|
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs.txt'));
|
|
|
|
|
logger.level = 'trace';
|
|
|
|
|
|
|
|
|
|
const msgRetryCounterCache = new NodeCache();
|
|
|
|
|
|
|
|
|
|
waEmitter.on('message:updated', event => {
|
|
|
|
@ -40,7 +37,8 @@ const createWhatsApp = async phone => {
|
|
|
|
|
let qrCode = null;
|
|
|
|
|
const channelId = generateId();
|
|
|
|
|
const whatsAppNo = phone;
|
|
|
|
|
|
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs-' + phone + '.txt'));
|
|
|
|
|
logger.level = 'trace';
|
|
|
|
|
const storeFilename = './baileys_auth_info/baileys_store_' + phone + '_' + channelId + '.json'
|
|
|
|
|
const store = makeInMemoryStore({ logger });
|
|
|
|
|
store?.readFromFile(storeFilename);
|
|
|
|
@ -53,14 +51,32 @@ const createWhatsApp = async phone => {
|
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
|
|
|
|
const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out');
|
|
|
|
|
|
|
|
|
|
const sendTextMessage = async (number, content) => {
|
|
|
|
|
const sendTextMessage = async (number, content, externalId) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
|
|
|
|
|
if (number === whatsAppNo) {
|
|
|
|
|
// whatsAppNo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const msgInfo = await waSocket.sendMessage(jid, { text: content });
|
|
|
|
|
waSocket.sendMessage(jid, { text: content })
|
|
|
|
|
return {
|
|
|
|
|
messageId: msgInfo?.key.id
|
|
|
|
|
};
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
waSocket.sendMessage(jid, { text: content })
|
|
|
|
|
.then(msgInfo => {
|
|
|
|
|
resolve({
|
|
|
|
|
messageId: msgInfo?.key?.id ?? generateId(),
|
|
|
|
|
externalId
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(ex => {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: `[${whatsAppNo}->${number}]发送文本消息出错`,
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: ex
|
|
|
|
|
})
|
|
|
|
|
console.error(`[${whatsAppNo}->${number}]发送文本消息出错: `, ex);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: `[${whatsAppNo}->${number}]发送文本消息出错`,
|
|
|
|
@ -79,7 +95,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
image: { url: imageUrl },
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
messageId: msgInfo?.key.id
|
|
|
|
|
messageId: msgInfo?.key?.id ?? generateId()
|
|
|
|
|
};
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
@ -115,34 +131,26 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
const messageType = Object.keys(msg.message)[0];
|
|
|
|
|
console.log('messageType', messageType);
|
|
|
|
|
if (messageType === 'imageMessage') {
|
|
|
|
|
// download the message
|
|
|
|
|
const buffer = await downloadMediaMessage(
|
|
|
|
|
msg,
|
|
|
|
|
'buffer',
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
logger,
|
|
|
|
|
// pass this so that baileys can request a reupload of media
|
|
|
|
|
// that has been deleted
|
|
|
|
|
reuploadRequest: waSocket.updateMediaMessage,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
// save to file
|
|
|
|
|
await writeFile('d:/my-download.jpeg', buffer);
|
|
|
|
|
}
|
|
|
|
|
// if (messageType === 'imageMessage') {
|
|
|
|
|
// const buffer = await downloadMediaMessage(
|
|
|
|
|
// msg,
|
|
|
|
|
// 'buffer',
|
|
|
|
|
// {},
|
|
|
|
|
// {
|
|
|
|
|
// logger,
|
|
|
|
|
// reuploadRequest: waSocket.updateMediaMessage,
|
|
|
|
|
// },
|
|
|
|
|
// );
|
|
|
|
|
// await writeFile('d:/my-download.jpeg', buffer);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
|
|
|
|
|
|
if (text.indexOf('【发图】') > -1) {
|
|
|
|
|
sendImageMessage(fromWhatsAppNo, 'https://images.asiahighlights.com/allpicture/2022/07/8a7d9ced5936463bb904c82a_cut_750x850_349.webp');
|
|
|
|
|
} else if (text.indexOf('【发文】') > -1) {
|
|
|
|
|
sendTextMessage(fromWhatsAppNo, '这是文本消息:' + new Date().toString());
|
|
|
|
|
} else if (text.indexOf('【发群】') > -1) {
|
|
|
|
|
sendTextMessage('120363363417115199@g.us', '这是群消息:' + new Date().toString());
|
|
|
|
|
if (text.indexOf('「自~测」') > -1) {
|
|
|
|
|
sendTextMessage(fromWhatsAppNo, '这是自测消息:' + new Date().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
@ -258,6 +266,10 @@ const createWhatsApp = async phone => {
|
|
|
|
|
creds: state.creds,
|
|
|
|
|
keys: makeCacheableSignalKeyStore(state.keys, logger),
|
|
|
|
|
},
|
|
|
|
|
connectTimeoutMs: 1000*10,
|
|
|
|
|
defaultQueryTimeoutMs: 1000*10,
|
|
|
|
|
keepAliveIntervalMs: 1000*10,
|
|
|
|
|
retryRequestDelayMs: 1000*10,
|
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21
|
|
|
|
|
// https://github.com/WhiskeySockets/Baileys/blob/31bc8ab4e2c825c0d774875701ed07e20d05bdb6/WAProto/WAProto.proto
|
|
|
|
|
browser: Browsers.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
@ -279,7 +291,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
console.log('Connection closed: ', lastDisconnect);
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
whatsAppNo, channelId,
|
|
|
|
|
eventSource: 'connection.update.close',
|
|
|
|
|
status: 'offline',
|
|
|
|
|
});
|
|
|
|
@ -288,7 +300,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
// 扫码成功后向这个群发消息,后续就能使用 API 发送了。
|
|
|
|
|
sendTextMessage('120363363417115199@g.us', whatsAppNo + ' 登录成功:' + new Date().toString());
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
status: 'open', whatsAppNo,
|
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
|
eventSource: 'connection.update.open',
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|