|
|
|
@ -1,10 +1,11 @@
|
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const { domain } = require('../../config').server;
|
|
|
|
|
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 { getOutboundMessage, upsertOutboundMessage } = require('../../services/outbound_messages.service');
|
|
|
|
|
|
|
|
|
|
const logger = console;
|
|
|
|
|
|
|
|
|
@ -17,6 +18,10 @@ const eventTypeMapped = {
|
|
|
|
|
};
|
|
|
|
|
const timeField = { sent: 'sendTime', delivered: 'deliverTime', read: 'readTime' };
|
|
|
|
|
const directionField = { 'message:received': 'inbound', 'message:updated': 'outbound' };
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @returns {Object} webhookBody
|
|
|
|
|
*/
|
|
|
|
|
const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
const message = {
|
|
|
|
|
id: `evt_${generateId().replace(/-/g, '')}`,
|
|
|
|
@ -24,12 +29,13 @@ const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
apiVersion: 'v2',
|
|
|
|
|
webhooksource: 'wai',
|
|
|
|
|
createTime: new Date(new Date().getTime() + 8 * 60 * 60 * 1000).toISOString(), // GMT +8
|
|
|
|
|
domainName,
|
|
|
|
|
waiMessage: {
|
|
|
|
|
...messageData,
|
|
|
|
|
...(messageData.updateTime ? { [timeField[messageData.status]]: messageData.updateTime } : {}),
|
|
|
|
|
wamid: messageData.id,
|
|
|
|
|
direction: directionField[messageType],
|
|
|
|
|
externalId: '-1', // todo:
|
|
|
|
|
// externalId: '-1', // todo:
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
return message;
|
|
|
|
@ -42,7 +48,7 @@ const setupConnectionHandler = () => {
|
|
|
|
|
try {
|
|
|
|
|
// find Or create
|
|
|
|
|
await addCurrentConnection({
|
|
|
|
|
...objectMapper(connectionData, { phone: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id', createTimestamp: 'createtime' }),
|
|
|
|
|
...objectMapper(connectionData, { phone: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id', createTimestamp: 'createtime' }, false),
|
|
|
|
|
service_type: 'baileys',
|
|
|
|
|
status: 'connecting',
|
|
|
|
|
});
|
|
|
|
@ -69,10 +75,13 @@ const setupConnectionHandler = () => {
|
|
|
|
|
whatsappEvents.on('connection:close', async connectionData => {
|
|
|
|
|
try {
|
|
|
|
|
sessionService.removeSession(connectionData.sesson_id);
|
|
|
|
|
await updateConnection({
|
|
|
|
|
...objectMapper(connectionData, { whatsAppNo: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id' }),
|
|
|
|
|
service_type: 'baileys',
|
|
|
|
|
});
|
|
|
|
|
await updateConnection(
|
|
|
|
|
{
|
|
|
|
|
...objectMapper(connectionData, { whatsAppNo: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id' }),
|
|
|
|
|
service_type: 'baileys',
|
|
|
|
|
},
|
|
|
|
|
{ connect_domain: domain },
|
|
|
|
|
);
|
|
|
|
|
// todo: 通知前端: 重新扫码
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ connectionData, error }, 'error add connection');
|
|
|
|
@ -86,8 +95,14 @@ const setupMessageHandler = () => {
|
|
|
|
|
logger.info(`Setting up event ${eventName}`);
|
|
|
|
|
whatsappEvents.on(eventName, async messageData => {
|
|
|
|
|
try {
|
|
|
|
|
const x = webhookBodyBuilder(messageData, eventName);
|
|
|
|
|
await callWebhook(x);
|
|
|
|
|
const m = await getOutboundMessage({ id: messageData.id });
|
|
|
|
|
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...messageData, actionId: m.actionId, externalId: m.externalId }, eventName);
|
|
|
|
|
const { waiMessage } = webhookBody;
|
|
|
|
|
|
|
|
|
|
const upsert = await upsertOutboundMessage(m.sn, { ...waiMessage, msg_status: waiMessage.status });
|
|
|
|
|
// console.log('upsert=========================', upsert);
|
|
|
|
|
await callWebhook(webhookBody);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ messageData, error }, 'error call webhook');
|
|
|
|
|
}
|
|
|
|
|