|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const { domain, name: domainName } = require('../../config').server;
|
|
|
|
|
const whatsappEvents = require('../emitter');
|
|
|
|
@ -15,11 +17,12 @@ const messageEventNames = ['message:received', 'message:updated'];
|
|
|
|
|
const eventTypeMapped = {
|
|
|
|
|
'message:received': 'wai.message.received',
|
|
|
|
|
'message:updated': 'wai.message.updated',
|
|
|
|
|
'creds:update': 'wai.creds.update',
|
|
|
|
|
};
|
|
|
|
|
const timeField = { saved: 'createTime', pending: 'createTime', sent: 'sendTime', delivered: 'deliverTime', read: 'readTime', };
|
|
|
|
|
const statusMapped = { saved: 'accepted', pending: 'accepted', sent: 'sent', delivered: 'delivered', read: 'read', failed: 'failed' }
|
|
|
|
|
const timeField = { saved: 'createTime', pending: 'createTime', sent: 'sendTime', delivered: 'deliverTime', read: 'readTime' };
|
|
|
|
|
const statusMapped = { saved: 'accepted', pending: 'accepted', sent: 'sent', delivered: 'delivered', read: 'read', failed: 'failed' };
|
|
|
|
|
const directionField = { 'message:received': 'inbound', 'message:updated': 'outbound' };
|
|
|
|
|
const directionPrefix = { 'inbound': 'in_', 'outbound': 'out_' };
|
|
|
|
|
const directionPrefix = { inbound: 'in_', outbound: 'out_' };
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns {Object} webhookBody
|
|
|
|
@ -36,8 +39,8 @@ const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
waiMessage: {
|
|
|
|
|
...messageData,
|
|
|
|
|
...(messageData.updateTime ? { [timeField[messageData.status]]: messageData.updateTime } : {}),
|
|
|
|
|
id: `${directionPrefix[messageData.direction]}${messageData.id}`,
|
|
|
|
|
wamid: messageData.id,
|
|
|
|
|
id: messageData.id && messageData.direction ? `${directionPrefix[messageData.direction]}${messageData.id}` : (messageData.id || generateId()),
|
|
|
|
|
wamid: messageData.id || null,
|
|
|
|
|
// direction: directionField[messageType],
|
|
|
|
|
status: statusMapped?.[messageData.status] || messageData.status || '',
|
|
|
|
|
externalId: messageData?.externalId || '',
|
|
|
|
@ -46,6 +49,16 @@ const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
return message;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const webhookBodyFill = (webhookBody, messageData) => {
|
|
|
|
|
const { type } = messageData;
|
|
|
|
|
const messageObj = { [type]: messageData };
|
|
|
|
|
Object.assign(webhookBody.waiMessage, messageObj);
|
|
|
|
|
return webhookBody;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WhatsApp 连接事件
|
|
|
|
|
*/
|
|
|
|
|
const setupConnectionHandler = () => {
|
|
|
|
|
// connectionEventNames.forEach(eventName => {
|
|
|
|
|
logger.info(`Setting up event ${'connection:connect'}`);
|
|
|
|
@ -98,6 +111,21 @@ const setupConnectionHandler = () => {
|
|
|
|
|
// });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 监听 Creds 更新事件
|
|
|
|
|
*/
|
|
|
|
|
const setupCredsHandler = () => {
|
|
|
|
|
whatsappEvents.on('creds:update', async creds => {
|
|
|
|
|
logger.info('creds:update', creds);
|
|
|
|
|
try {
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...creds }, eventName);
|
|
|
|
|
await callWebhook(webhookBody);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ creds, error }, 'error update creds');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WhatsApp 消息事件
|
|
|
|
|
* pending ⏰ -> saved ⏰ -> sent(*) ✔ -> delivered ✔✔ -> read ✅
|
|
|
|
@ -130,9 +158,13 @@ const setupMessageHandler = () => {
|
|
|
|
|
const record = objectMapper(waiMessage, { from: 'from', to: 'to', status: 'msg_status', type: 'msgtype' }, false);
|
|
|
|
|
const contentFields = waiMessage.type === 'text' ? { text_body: waiMessage.text.body } : {};
|
|
|
|
|
|
|
|
|
|
await upsertOutboundMessage({ ...timeFields, ...upsertFields, ...pusher, ...contentFields, ...record, message_origin: savedMsg?.message_origin || JSON.stringify(messageData) }, targetUpsert);
|
|
|
|
|
const msgRow = await upsertOutboundMessage(
|
|
|
|
|
{ ...timeFields, ...upsertFields, ...pusher, ...contentFields, ...record, message_origin: savedMsg?.message_origin || JSON.stringify(messageData) },
|
|
|
|
|
targetUpsert,
|
|
|
|
|
);
|
|
|
|
|
// console.log('upsert=========================', upsert);
|
|
|
|
|
await callWebhook(webhookBody);
|
|
|
|
|
// todo: 把内容加上, 否则前端没显示
|
|
|
|
|
await callWebhook(webhookBodyFill(webhookBody, msgRow));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ messageData, error }, 'error call webhook');
|
|
|
|
|
}
|
|
|
|
@ -142,6 +174,7 @@ const setupMessageHandler = () => {
|
|
|
|
|
|
|
|
|
|
function setupWhatsappHandler() {
|
|
|
|
|
setupConnectionHandler();
|
|
|
|
|
setupCredsHandler();
|
|
|
|
|
setupMessageHandler();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|