|
|
|
@ -13,7 +13,7 @@ const { writeFile } = require('fs/promises');
|
|
|
|
|
const waEmitter = require('../emitter');
|
|
|
|
|
const serverConfig = require('../../config').server;
|
|
|
|
|
|
|
|
|
|
const { formatPhoneNumber, parsePhoneNumber, formatStatus, formatTimestamp } = require('./helper');
|
|
|
|
|
const { encodeJid, decodeJid, formatStatus, formatTimestamp } = require('./helper');
|
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
|
const P = require('pino');
|
|
|
|
@ -34,7 +34,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
store?.writeToFile(storeFilename);
|
|
|
|
|
}, 10_000);
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info/' + phone + '_' + channelId);
|
|
|
|
|
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info/' + phone);
|
|
|
|
|
// fetch latest version of WA Web
|
|
|
|
|
const { version, isLatest } = await fetchLatestBaileysVersion();
|
|
|
|
|
const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out');
|
|
|
|
@ -53,7 +53,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
const messageType = Object.keys(msg.message)[0];
|
|
|
|
|
console.log('messageType', messageType);
|
|
|
|
|
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
const fromWhatsAppNo = decodeJid(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
|
|
|
|
@ -76,7 +76,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
id: decodeJid(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
@ -100,7 +100,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
id: decodeJid(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
@ -115,7 +115,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
const fromWhatsAppNo = decodeJid(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
|
const externalId = externalIdCache.get(msg.key.id);
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
@ -134,7 +134,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.participant),
|
|
|
|
|
id: decodeJid(msg.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
@ -164,13 +164,13 @@ const createWhatsApp = async phone => {
|
|
|
|
|
externalId,
|
|
|
|
|
status: formatStatus(msg.update?.status),
|
|
|
|
|
direction: msg.key.fromMe ? 'outbound' : 'inbound',
|
|
|
|
|
from: msg.key.fromMe ? whatsAppNo : parsePhoneNumber(msg.key.remoteJid),
|
|
|
|
|
to: msg.key.fromMe ? parsePhoneNumber(msg.key.remoteJid) : whatsAppNo,
|
|
|
|
|
from: msg.key.fromMe ? whatsAppNo : decodeJid(msg.key.remoteJid),
|
|
|
|
|
to: msg.key.fromMe ? decodeJid(msg.key.remoteJid) : whatsAppNo,
|
|
|
|
|
conversation: {
|
|
|
|
|
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: parsePhoneNumber(msg.key.participant),
|
|
|
|
|
id: decodeJid(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
@ -199,70 +199,44 @@ const createWhatsApp = async phone => {
|
|
|
|
|
//retryRequestDelayMs: 1000*25,
|
|
|
|
|
// 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.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
|
browser: Browsers.baileys('WEAR_OS'),//Browsers.macOS('SAFARI'),//Browsers.ubuntu('IOS_PHONE'),//Browsers.baileys('WEAR_OS'),//
|
|
|
|
|
msgRetryCounterCache,
|
|
|
|
|
generateHighQualityLinkPreview: false,
|
|
|
|
|
syncFullHistory: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
|
store?.bind(waSocket.ev);
|
|
|
|
|
|
|
|
|
|
const sendTextMessage = async (number, content, externalId) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
waSocket.sendMessage(jid, { text: content })
|
|
|
|
|
.then(msg => {
|
|
|
|
|
externalIdCache.set(msg.key.id, externalId);
|
|
|
|
|
})
|
|
|
|
|
.catch(ex => {
|
|
|
|
|
console.error('sendTextMessage.error: ', ex)
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
externalId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: `发送文本消息出错 ` + ex,
|
|
|
|
|
eventSource: serverConfig.name + '.sendMessage.promise.catch',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sendImageMessage = async (number, imageUrl) => {
|
|
|
|
|
const jid = formatPhoneNumber(number);
|
|
|
|
|
try {
|
|
|
|
|
const msgInfo = await waSocket.sendMessage(jid, {
|
|
|
|
|
image: { url: imageUrl },
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
messageId: msgInfo?.key?.id ?? generateId()
|
|
|
|
|
};
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
waEmitter.emit('message.error', {
|
|
|
|
|
messge: `[${whatsAppNo}->${number}]发送图片消息出错`,
|
|
|
|
|
const sendTextMessageHandler = (event) => {
|
|
|
|
|
const { to: number, externalId, content } = event;
|
|
|
|
|
const jid = encodeJid(number);
|
|
|
|
|
console.info('number: %s; jid: %s: ', number, jid)
|
|
|
|
|
waSocket.sendMessage(
|
|
|
|
|
jid, { text: content }
|
|
|
|
|
).then(msg => {
|
|
|
|
|
externalIdCache.set(msg.key.id, externalId)
|
|
|
|
|
}).catch(ex => {
|
|
|
|
|
console.error('sendMessage.error: ', ex)
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
externalId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: ex
|
|
|
|
|
})
|
|
|
|
|
console.error(`[${whatsAppNo}->${number}]发送图片消息出错: `, ex);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getProfilePicture = async (whatsAppNo) => {
|
|
|
|
|
const number = formatPhoneNumber(whatsAppNo);
|
|
|
|
|
try {
|
|
|
|
|
const ppUrl = await waSocket.profilePictureUrl(number);
|
|
|
|
|
console.log('头像: ' + ppUrl);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.error('头像出错: ', ex);
|
|
|
|
|
}
|
|
|
|
|
error: `发送文本消息出错 ` + ex,
|
|
|
|
|
eventSource: serverConfig.name + '.sendMessage.catch',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
waSocket.ev.on('connection.update', async update => {
|
|
|
|
|
console.log('connection update: ', update);
|
|
|
|
|
const { connection, lastDisconnect, qr } = update;
|
|
|
|
|
|
|
|
|
|
if (connection === 'close') {
|
|
|
|
|
waEmitter.off('request.' + whatsAppNo + '.send.text', sendTextMessageHandler);
|
|
|
|
|
if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
start();
|
|
|
|
|
} else {
|
|
|
|
@ -277,34 +251,36 @@ const createWhatsApp = async phone => {
|
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
|
eventSource: serverConfig.name + '.connection.update.open',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.text', sendTextMessageHandler);
|
|
|
|
|
// 考虑迁移到 connection.update 事件之外,测试看是否能发送消息?
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.text', event => {
|
|
|
|
|
const {to: number, externalId, content} = event;
|
|
|
|
|
console.info('request.' + whatsAppNo + '.send.text:', event)
|
|
|
|
|
// const jid = formatPhoneNumber(event.to);
|
|
|
|
|
waSocket.sendMessage(
|
|
|
|
|
number + '@s.whatsapp.net', { text: content }
|
|
|
|
|
).then(msg => {
|
|
|
|
|
externalIdCache.set(msg.key.id, externalId)
|
|
|
|
|
}).catch(ex => {
|
|
|
|
|
console.error('sendMessage.error: ', ex)
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
externalId,
|
|
|
|
|
status: 'failed',
|
|
|
|
|
direction: 'outbound',
|
|
|
|
|
from: whatsAppNo,
|
|
|
|
|
to: number,
|
|
|
|
|
error: `发送文本消息出错 ` + ex,
|
|
|
|
|
eventSource: serverConfig.name + '.sendMessage.catch',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
// waEmitter.on('request.' + whatsAppNo + '.send.text', event => {
|
|
|
|
|
// const { to: number, externalId, content } = event;
|
|
|
|
|
// const jid = encodeJid(number);
|
|
|
|
|
// console.info('number: %s; jid: %s: ', number, jid)
|
|
|
|
|
// waSocket.sendMessage(
|
|
|
|
|
// jid, { text: content }
|
|
|
|
|
// ).then(msg => {
|
|
|
|
|
// externalIdCache.set(msg.key.id, externalId)
|
|
|
|
|
// }).catch(ex => {
|
|
|
|
|
// console.error('sendMessage.error: ', ex)
|
|
|
|
|
// waEmitter.emit('message:updated', {
|
|
|
|
|
// id: generateId(),
|
|
|
|
|
// externalId,
|
|
|
|
|
// status: 'failed',
|
|
|
|
|
// direction: 'outbound',
|
|
|
|
|
// from: whatsAppNo,
|
|
|
|
|
// to: number,
|
|
|
|
|
// error: `发送文本消息出错 ` + ex,
|
|
|
|
|
// eventSource: serverConfig.name + '.sendMessage.catch',
|
|
|
|
|
// updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.image', event => {
|
|
|
|
|
const {to: number, externalId, imageUrl} = event;
|
|
|
|
|
const jid = formatPhoneNumber(event.to);
|
|
|
|
|
const jid = encodeJid(number);
|
|
|
|
|
waSocket.sendMessage(
|
|
|
|
|
jid, {image: { url: imageUrl }}
|
|
|
|
|
).then(msg => {
|
|
|
|
|