|
|
|
@ -28,6 +28,7 @@ const directionPrefix = { inbound: 'in_', outbound: 'out_' };
|
|
|
|
|
* @returns {Object} webhookBody
|
|
|
|
|
*/
|
|
|
|
|
const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
const defaultContent = { id: '', from: '', to: '', externalId: '', type: '', direction: '', status: '', };
|
|
|
|
|
const message = {
|
|
|
|
|
id: `evt_${generateId().replace(/-/g, '')}`,
|
|
|
|
|
type: eventTypeMapped[messageType],
|
|
|
|
@ -38,6 +39,7 @@ const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
conversationid: messageData?.externalId || '',
|
|
|
|
|
whatsAppNo: messageData?.whatsAppNo || '',
|
|
|
|
|
waiMessage: {
|
|
|
|
|
...defaultContent,
|
|
|
|
|
...messageData,
|
|
|
|
|
...(messageData.updateTime ? { [timeField[messageData.status]]: messageData.updateTime } : {}),
|
|
|
|
|
id: messageData.id && messageData.direction ? `${directionPrefix[messageData.direction]}${messageData.id}` : (messageData.id || generateId()),
|
|
|
|
@ -88,7 +90,7 @@ const setupConnectionHandler = () => {
|
|
|
|
|
},
|
|
|
|
|
{ connect_domain: domain, connect_name: domainName },
|
|
|
|
|
);
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...connectionData, connection: 'open' }, 'creds:update');
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...connectionData, to: connectionData.whatsAppNo, connection: 'open' }, 'creds:update');
|
|
|
|
|
await callWebhook(webhookBody);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ connectionData, error }, 'error add connection');
|
|
|
|
@ -107,7 +109,7 @@ const setupConnectionHandler = () => {
|
|
|
|
|
{ connect_domain: domain, connect_name: domainName, channel_id: connectionData.channelId },
|
|
|
|
|
);
|
|
|
|
|
// todo: 通知前端: 重新扫码
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...connectionData, connection: 'offline' }, 'creds:update');
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...connectionData, to: connectionData.whatsAppNo, connection: 'offline' }, 'creds:update');
|
|
|
|
|
await callWebhook(webhookBody);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ connectionData, error }, 'error add connection');
|
|
|
|
@ -123,7 +125,7 @@ const setupCredsHandler = () => {
|
|
|
|
|
whatsappEvents.on('creds:update', async creds => {
|
|
|
|
|
logger.info('creds:update', creds);
|
|
|
|
|
try {
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...creds, connection: '' }, 'creds:update');
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...creds, to: creds.whatsAppNo, connection: '' }, 'creds:update');
|
|
|
|
|
await callWebhook(webhookBody);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ creds, error }, 'error update creds');
|
|
|
|
|