|
|
|
@ -4,11 +4,11 @@ const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const { domain, name: domainName } = require('../../config').server;
|
|
|
|
|
const whatsappEvents = require('../emitter');
|
|
|
|
|
const { callWebhook } = require('../webhook');
|
|
|
|
|
const { updateConnection, addCurrentConnection, resetConnection } = require('../../services/connections.service');
|
|
|
|
|
const { objectMapper, pick } = require('../../utils/commons.util');
|
|
|
|
|
const { sessionStore } = require('..');
|
|
|
|
|
const { createWhatsApp } = require('../../core/baileys');
|
|
|
|
|
const { updateConnection, addCurrentConnection, resetConnection, getConnection } = require('../../services/connections.service');
|
|
|
|
|
const { getOutboundMessage, upsertOutboundMessage } = require('../../services/outbound_messages.service');
|
|
|
|
|
|
|
|
|
|
const { objectMapper, pick } = require('../../utils/commons.util');
|
|
|
|
|
const { logger, getUserLogger } = require('../../utils/logger.util');
|
|
|
|
|
const { DbData } = require('../../helper/wai.msg.helper');
|
|
|
|
|
|
|
|
|
@ -45,6 +45,7 @@ const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
...defaultContent,
|
|
|
|
|
...messageData,
|
|
|
|
|
...(messageData.updateTime ? { [timeField[messageData.status]]: messageData.updateTime } : {}),
|
|
|
|
|
type: messageData.type || messageData.msgtype || '',
|
|
|
|
|
id: uniqueMsgId(messageData) || messageData.id || generateId(),
|
|
|
|
|
wamid: messageData.id || '',
|
|
|
|
|
// direction: directionField[messageType],
|
|
|
|
@ -63,9 +64,11 @@ const webhookBodyFill = (webhookBody, messageData) => {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WhatsApp 连接事件
|
|
|
|
|
* * connection:connect
|
|
|
|
|
* * connection:open
|
|
|
|
|
* * connection:close
|
|
|
|
|
*/
|
|
|
|
|
const setupConnectionHandler = () => {
|
|
|
|
|
// connectionEventNames.forEach(eventName => {
|
|
|
|
|
whatsappEvents.on('connection:connect', async connectionData => {
|
|
|
|
|
try {
|
|
|
|
|
getUserLogger(connectionData.phone).info({ msg: `连接https://web.whatsapp.com/`, connectionData });
|
|
|
|
@ -114,11 +117,11 @@ const setupConnectionHandler = () => {
|
|
|
|
|
logger.error({ connectionData, error }, 'error close connection');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 监听 Creds 更新事件
|
|
|
|
|
* * creds:update
|
|
|
|
|
*/
|
|
|
|
|
const setupCredsHandler = () => {
|
|
|
|
|
whatsappEvents.on('creds:update', async creds => {
|
|
|
|
@ -134,8 +137,8 @@ const setupCredsHandler = () => {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WhatsApp 消息事件
|
|
|
|
|
* pending ⏰ -> saved ⏰ -> sent(*) ✔ -> delivered ✔✔ -> read ✅
|
|
|
|
|
* saved -> pending -> sent(*) -> delivered -> read
|
|
|
|
|
* * pending ⏰ -> saved ⏰ -> sent(*) ✔ -> delivered ✔✔ -> read ✅
|
|
|
|
|
* * saved -> pending -> sent(*) -> delivered -> read
|
|
|
|
|
*/
|
|
|
|
|
const setupMessageHandler = () => {
|
|
|
|
|
messageEventNames.forEach(eventName => {
|
|
|
|
@ -175,7 +178,7 @@ const setupMessageHandler = () => {
|
|
|
|
|
targetUpsert,
|
|
|
|
|
);
|
|
|
|
|
// console.log('upsert=========================', upsert);
|
|
|
|
|
// todo: 把内容加上, 否则前端没显示
|
|
|
|
|
// 把内容加上, 否则前端没显示
|
|
|
|
|
await callWebhook(webhookBodyFill(webhookBody, msgRow));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error({ messageData, error }, 'error call webhook');
|
|
|
|
@ -193,8 +196,22 @@ function setupWhatsappHandler() {
|
|
|
|
|
/**
|
|
|
|
|
* 登出: 当前服务的所有连接
|
|
|
|
|
*/
|
|
|
|
|
async function resetCurrentConnection() {
|
|
|
|
|
async function resetCurrentConnections() {
|
|
|
|
|
await resetConnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { setupWhatsappHandler, resetCurrentConnection };
|
|
|
|
|
/**
|
|
|
|
|
* 登录: 当前服务的所有连接
|
|
|
|
|
*/
|
|
|
|
|
async function loginCurrentConnections() {
|
|
|
|
|
const currents = await getConnection({ connect_domain: domain, connect_name: domainName });
|
|
|
|
|
for (const user of currents) {
|
|
|
|
|
const { wa_id: waId } = user;
|
|
|
|
|
const phone = waId.replace('+', '');
|
|
|
|
|
const whatsApp1 = await createWhatsApp(phone);
|
|
|
|
|
whatsApp1.start();
|
|
|
|
|
sessionStore.createSession(phone, whatsApp1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { setupWhatsappHandler, resetCurrentConnections, loginCurrentConnections };
|
|
|
|
|