|
|
|
@ -3,11 +3,11 @@ const { domain, name: domainName } = require('../../config').server;
|
|
|
|
|
const whatsappEvents = require('../emitter');
|
|
|
|
|
const { callWebhook } = require('../webhook');
|
|
|
|
|
const { addConnection, updateConnection, addCurrentConnection, resetConnection } = require('../../services/connections.service');
|
|
|
|
|
const { objectMapper } = require('../../utils/commons.util');
|
|
|
|
|
const { sessionService } = require('..');
|
|
|
|
|
const { objectMapper, pick } = require('../../utils/commons.util');
|
|
|
|
|
const { sessionStore } = require('..');
|
|
|
|
|
const { getOutboundMessage, upsertOutboundMessage } = require('../../services/outbound_messages.service');
|
|
|
|
|
|
|
|
|
|
const logger = console;
|
|
|
|
|
const logger = require('../../utils/logger.util');
|
|
|
|
|
|
|
|
|
|
const connectionEventNames = ['connection:connect', 'connection:open', 'connection:close'];
|
|
|
|
|
const messageEventNames = ['message:received', 'message:updated'];
|
|
|
|
@ -16,7 +16,8 @@ const eventTypeMapped = {
|
|
|
|
|
'message:received': 'wai.message.received',
|
|
|
|
|
'message:updated': 'wai.message.updated',
|
|
|
|
|
};
|
|
|
|
|
const timeField = { sent: 'sendTime', delivered: 'deliverTime', read: 'readTime' };
|
|
|
|
|
const timeField = { pending: 'createTime', sent: 'sendTime', delivered: 'deliverTime', read: 'readTime' };
|
|
|
|
|
const statusMapped = { pending: 'accepted', sent: 'sent', delivered: 'delivered', read: 'read' }
|
|
|
|
|
const directionField = { 'message:received': 'inbound', 'message:updated': 'outbound' };
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -30,11 +31,13 @@ const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
webhooksource: 'wai',
|
|
|
|
|
createTime: new Date(new Date().getTime() + 8 * 60 * 60 * 1000).toISOString(), // GMT +8
|
|
|
|
|
domainName,
|
|
|
|
|
conversationid: messageData?.externalId || '',
|
|
|
|
|
waiMessage: {
|
|
|
|
|
...messageData,
|
|
|
|
|
...(messageData.updateTime ? { [timeField[messageData.status]]: messageData.updateTime } : {}),
|
|
|
|
|
wamid: messageData.id,
|
|
|
|
|
direction: directionField[messageType],
|
|
|
|
|
// direction: directionField[messageType],
|
|
|
|
|
status: statusMapped?.[messageData.status] || '',
|
|
|
|
|
// externalId: '-1', // todo:
|
|
|
|
|
externalId: `-${messageData.externalId || 1}`, // debug: 测试: 是负值
|
|
|
|
|
},
|
|
|
|
@ -65,8 +68,9 @@ const setupConnectionHandler = () => {
|
|
|
|
|
{
|
|
|
|
|
...objectMapper(connectionData, { whatsAppNo: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id' }),
|
|
|
|
|
service_type: 'baileys',
|
|
|
|
|
closetime: null,
|
|
|
|
|
},
|
|
|
|
|
{ connect_domain: domain },
|
|
|
|
|
{ connect_domain: domain, connect_name: domainName },
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ connectionData, error }, 'error add connection');
|
|
|
|
@ -75,7 +79,7 @@ const setupConnectionHandler = () => {
|
|
|
|
|
logger.info(`Setting up event ${'connection:close'}`);
|
|
|
|
|
whatsappEvents.on('connection:close', async connectionData => {
|
|
|
|
|
try {
|
|
|
|
|
sessionService.removeSession(connectionData.sesson_id);
|
|
|
|
|
sessionStore.removeSession(connectionData.channelId);
|
|
|
|
|
await updateConnection(
|
|
|
|
|
{
|
|
|
|
|
...objectMapper(connectionData, { whatsAppNo: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id' }),
|
|
|
|
@ -95,13 +99,29 @@ const setupMessageHandler = () => {
|
|
|
|
|
messageEventNames.forEach(eventName => {
|
|
|
|
|
logger.info(`Setting up event ${eventName}`);
|
|
|
|
|
whatsappEvents.on(eventName, async messageData => {
|
|
|
|
|
if (messageData.status === 'pending') {
|
|
|
|
|
logger.info('message pending', messageData);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const now = new Date(new Date().getTime() + 60 * 60 * 1000).toISOString();
|
|
|
|
|
|
|
|
|
|
const m = await getOutboundMessage({ id: messageData.id });
|
|
|
|
|
const bixFields = pick(m, ['actionId', 'externalId', 'message_origin']);
|
|
|
|
|
logger.info('message evt NOT pending \n', m, bixFields);
|
|
|
|
|
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ actionId: m.actionId || '', externalId: m.externalId || '', ...messageData }, eventName);
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...messageData, ...bixFields, savedMsg: m }, eventName);
|
|
|
|
|
const { waiMessage } = webhookBody;
|
|
|
|
|
|
|
|
|
|
const upsert = await upsertOutboundMessage(m.sn || null, { ...waiMessage, msg_status: waiMessage.status });
|
|
|
|
|
const timeFields = pick(waiMessage, [...Object.values(timeField), 'createTime', 'updateTime']);
|
|
|
|
|
// timeFields.msgtime = m?.msgtime || now;
|
|
|
|
|
// timeFields.createTime = messageData?.createTime || m?.createTime || now;
|
|
|
|
|
const upsertFields = pick(waiMessage, ['direction', 'wamid', 'id', 'status']);
|
|
|
|
|
upsertFields.evt_id = webhookBody.id;
|
|
|
|
|
const pusher = { customerProfile_id: waiMessage.customerProfile?.id || '', customerProfile_name: waiMessage.customerProfile?.name || '' };
|
|
|
|
|
const record = objectMapper(waiMessage, { from: 'froms', to: 'tos', status: 'msg_status', type: 'msgtype' }, false);
|
|
|
|
|
const contentFields = waiMessage.type === 'text' ? { text_body: waiMessage.text.body } : {};
|
|
|
|
|
await upsertOutboundMessage(m.sn || null, { ...timeFields, ...upsertFields, ...pusher, ...contentFields, ...record, message_origin: bixFields?.message_origin || JSON.stringify(messageData) });
|
|
|
|
|
// console.log('upsert=========================', upsert);
|
|
|
|
|
await callWebhook(webhookBody);
|
|
|
|
|
} catch (error) {
|
|
|
|
|