|
|
|
@ -1,12 +1,14 @@
|
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const { domain } = require('../../config').server;
|
|
|
|
|
const whatsappEvents = require('../emitter');
|
|
|
|
|
const { callWebhook } = require('../webhook');
|
|
|
|
|
const { addConnection, updateConnection } = require('../../services/connections.service');
|
|
|
|
|
const { addConnection, updateConnection, addCurrentConnection } = require('../../services/connections.service');
|
|
|
|
|
const { objectMapper } = require('../../utils/commons.util');
|
|
|
|
|
const { sessionService } = require('..');
|
|
|
|
|
|
|
|
|
|
const logger = console;
|
|
|
|
|
|
|
|
|
|
const connectionEventNames = ['connection:open', 'connection:update', 'connection:close'];
|
|
|
|
|
const connectionEventNames = ['connection:connect', 'connection:open', 'connection:close'];
|
|
|
|
|
const messageEventNames = ['message:received', 'message:updated'];
|
|
|
|
|
|
|
|
|
|
const eeventTypeMapped = {
|
|
|
|
@ -28,10 +30,11 @@ const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
|
|
|
|
|
const setupConnectionHandler = () => {
|
|
|
|
|
// connectionEventNames.forEach(eventName => {
|
|
|
|
|
whatsappEvents.on('connection:open', async connectionData => {
|
|
|
|
|
logger.info(`Setting up event ${'connection:open'}`);
|
|
|
|
|
logger.info(`Setting up event ${'connection:connect'}`);
|
|
|
|
|
whatsappEvents.on('connection:connect', async connectionData => {
|
|
|
|
|
try {
|
|
|
|
|
await addConnection({
|
|
|
|
|
// find Or create
|
|
|
|
|
await addCurrentConnection({
|
|
|
|
|
...objectMapper(connectionData, { phone: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id', createTimestamp: 'createtime' }),
|
|
|
|
|
service_type: 'baileys',
|
|
|
|
|
status: 'connecting',
|
|
|
|
@ -40,13 +43,30 @@ const setupConnectionHandler = () => {
|
|
|
|
|
logger.error({ connectionData, error }, 'error add connection');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
whatsappEvents.on('connection:update', async connectionData => {
|
|
|
|
|
logger.info(`Setting up event ${'connection:updated'}`);
|
|
|
|
|
logger.info(`Setting up event ${'connection:open'}`);
|
|
|
|
|
whatsappEvents.on('connection:open', async connectionData => {
|
|
|
|
|
logger.info(`event ${'connection:open'}`, connectionData);
|
|
|
|
|
try {
|
|
|
|
|
await updateConnection(
|
|
|
|
|
{
|
|
|
|
|
...objectMapper(connectionData, { phone: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id' }),
|
|
|
|
|
service_type: 'baileys',
|
|
|
|
|
},
|
|
|
|
|
{ connect_domain: domain },
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ connectionData, error }, 'error add connection');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
logger.info(`Setting up event ${'connection:close'}`);
|
|
|
|
|
whatsappEvents.on('connection:close', async connectionData => {
|
|
|
|
|
try {
|
|
|
|
|
sessionService.removeSession(connectionData.sesson_id);
|
|
|
|
|
await updateConnection({
|
|
|
|
|
...objectMapper(connectionData, { phone: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id' }),
|
|
|
|
|
service_type: 'baileys',
|
|
|
|
|
});
|
|
|
|
|
// todo: 通知前端: 重新扫码
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ connectionData, error }, 'error add connection');
|
|
|
|
|
}
|
|
|
|
|