|
|
|
@ -172,6 +172,11 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
for (const msg of messageUpdate) {
|
|
|
|
|
|
|
|
|
|
// 没有明确标识状态的更新,忽略
|
|
|
|
|
const ignore = msg.update === undefined || msg.update.status === undefined;
|
|
|
|
|
|
|
|
|
|
if (ignore) continue;
|
|
|
|
|
|
|
|
|
|
const externalId = msgIdMap.get(msg.key.id);
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
@ -280,7 +285,6 @@ const createWhatsApp = async phone => {
|
|
|
|
|
if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
|
|
|
|
start();
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('connection:close', {
|
|
|
|
|
whatsAppNo, channelId,
|
|
|
|
|
eventSource: serverConfig.name + '.connection.update.close',
|
|
|
|
@ -288,18 +292,16 @@ const createWhatsApp = async phone => {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if (connection === 'open') {
|
|
|
|
|
connectionStatus = 'open';
|
|
|
|
|
waEmitter.emit('connection:open', {
|
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
|
eventSource: serverConfig.name + '.connection.update.open',
|
|
|
|
|
});
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.message', event => {
|
|
|
|
|
// const jid = formatPhoneNumber(event.to);
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.message', event => {
|
|
|
|
|
const {to: number, externalId, content} = event;
|
|
|
|
|
console.info('request.' + whatsAppNo + '.send.message:', event)
|
|
|
|
|
// const jid = formatPhoneNumber(event.to);
|
|
|
|
|
waSocket.sendMessage(
|
|
|
|
|
number + '@s.whatsapp.net',
|
|
|
|
|
{ text: content}
|
|
|
|
|
number + '@s.whatsapp.net', { text: content }
|
|
|
|
|
).then(msg => {
|
|
|
|
|
msgIdMap.set(msg.key.id, externalId);
|
|
|
|
|
}).catch(ex => {
|
|
|
|
@ -318,6 +320,29 @@ const createWhatsApp = async phone => {
|
|
|
|
|
});
|
|
|
|
|
//sendTextMessage(number, content, externalId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
waEmitter.on('req.' + whatsAppNo + '.send.image', event => {
|
|
|
|
|
const {to: number, externalId, content} = event;
|
|
|
|
|
const jid = formatPhoneNumber(event.to);
|
|
|
|
|
waSocket.sendMessage(
|
|
|
|
|
jid, {image: { url: imageUrl }}
|
|
|
|
|
).then(msg => {
|
|
|
|
|
msgIdMap.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),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else if (qr !== undefined) {
|
|
|
|
|
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
|
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
|