|
|
|
@ -9,7 +9,7 @@ const { objectMapper, pick } = require('../../utils/commons.util');
|
|
|
|
|
const { sessionStore } = require('..');
|
|
|
|
|
const { getOutboundMessage, upsertOutboundMessage } = require('../../services/outbound_messages.service');
|
|
|
|
|
|
|
|
|
|
const logger = require('../../utils/logger.util');
|
|
|
|
|
const { logger, getUserLogger } = require('../../utils/logger.util');
|
|
|
|
|
const { DbData } = require('../../helper/wai.msg.helper');
|
|
|
|
|
|
|
|
|
|
const connectionEventNames = ['connection:connect', 'connection:open', 'connection:close'];
|
|
|
|
@ -40,7 +40,7 @@ const webhookBodyBuilder = (messageData, messageType) => {
|
|
|
|
|
createTime: new Date(new Date().getTime() + 8 * 60 * 60 * 1000).toISOString(), // GMT +8
|
|
|
|
|
domainName,
|
|
|
|
|
conversationid: messageData?.externalId || '',
|
|
|
|
|
whatsAppNo: messageData?.whatsAppNo || '',
|
|
|
|
|
whatsAppNo: messageData?.whatsAppNo || messageData.from || messageData.to || '',
|
|
|
|
|
waiMessage: {
|
|
|
|
|
...defaultContent,
|
|
|
|
|
...messageData,
|
|
|
|
@ -68,6 +68,7 @@ const setupConnectionHandler = () => {
|
|
|
|
|
// connectionEventNames.forEach(eventName => {
|
|
|
|
|
whatsappEvents.on('connection:connect', async connectionData => {
|
|
|
|
|
try {
|
|
|
|
|
getUserLogger(connectionData.phone).info({ msg: `连接https://web.whatsapp.com/`, connectionData });
|
|
|
|
|
// find Or create
|
|
|
|
|
await addCurrentConnection({
|
|
|
|
|
...objectMapper(connectionData, { phone: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id', createTimestamp: 'createtime', version: 'version' }, false),
|
|
|
|
@ -79,9 +80,9 @@ const setupConnectionHandler = () => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
whatsappEvents.on('connection:open', async connectionData => {
|
|
|
|
|
logger.info(`event ${'connection:open'}`, connectionData);
|
|
|
|
|
// todo: 更新实例
|
|
|
|
|
try {
|
|
|
|
|
getUserLogger(connectionData.whatsAppNo).info({ msg: `已登录`, connectionData });
|
|
|
|
|
await updateConnection(
|
|
|
|
|
{
|
|
|
|
|
...objectMapper(connectionData, { whatsAppNo: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id' }),
|
|
|
|
@ -97,8 +98,8 @@ const setupConnectionHandler = () => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
whatsappEvents.on('connection:close', async connectionData => {
|
|
|
|
|
logger.info(`event ${'connection:close'}`, connectionData);
|
|
|
|
|
try {
|
|
|
|
|
getUserLogger(connectionData.whatsAppNo).warn({ msg: `断开连接`, connectionData });
|
|
|
|
|
sessionStore.removeSession(connectionData.channelId);
|
|
|
|
|
await updateConnection(
|
|
|
|
|
{
|
|
|
|
@ -121,8 +122,8 @@ const setupConnectionHandler = () => {
|
|
|
|
|
*/
|
|
|
|
|
const setupCredsHandler = () => {
|
|
|
|
|
whatsappEvents.on('creds:update', async creds => {
|
|
|
|
|
logger.info('creds:update', creds);
|
|
|
|
|
try {
|
|
|
|
|
getUserLogger(creds.whatsAppNo).info({ msg: `二维码`, creds });
|
|
|
|
|
const webhookBody = webhookBodyBuilder({ ...creds, to: creds.whatsAppNo, connection: '' }, 'creds:update');
|
|
|
|
|
await callWebhook(webhookBody);
|
|
|
|
|
} catch (error) {
|
|
|
|
@ -143,6 +144,9 @@ const setupMessageHandler = () => {
|
|
|
|
|
// logger.info('message pending', messageData);
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
const { from, to, whatsAppNo } = messageData;
|
|
|
|
|
const _whatsAppNo = whatsAppNo || from || to;
|
|
|
|
|
getUserLogger(_whatsAppNo).info({ eventName, messageData });
|
|
|
|
|
try {
|
|
|
|
|
const now = new Date(new Date().getTime() + 60 * 60 * 1000).toISOString();
|
|
|
|
|
|
|
|
|
|