Added support for messaging with ads on chatwoot

This commit is contained in:
Davidson Gomes 2023-09-17 13:48:27 -03:00
parent 32da15fa8a
commit 707aa22a6d
4 changed files with 26 additions and 149 deletions

View File

@ -26,6 +26,7 @@
* Added Typebot integration
* Added proxy endpoint
* Added send and date_time in webhook data
* Added support for messaging with ads on chatwoot
### Fixed

View File

@ -6,7 +6,7 @@ import cors from 'cors';
import express, { json, NextFunction, Request, Response, urlencoded } from 'express';
import { join } from 'path';
import { configService, Cors, HttpServer, Rabbitmq, Webhook } from './config/env.config';
import { Auth, configService, Cors, HttpServer, Rabbitmq, Webhook } from './config/env.config';
import { onUnexpectedError } from './config/error.config';
import { Logger } from './config/logger.config';
import { ROOT_DIR } from './config/path.config';
@ -61,6 +61,8 @@ function bootstrap() {
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
const localISOTime = new Date(Date.now() - tzoffset).toISOString();
const now = localISOTime;
const globalApiKey = configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
const serverUrl = configService.get<HttpServer>('SERVER').URL;
const errorData = {
event: 'error',
@ -73,6 +75,8 @@ function bootstrap() {
},
},
date_time: now,
api_key: globalApiKey,
server_url: serverUrl,
};
logger.error(errorData);
@ -83,11 +87,6 @@ function bootstrap() {
httpService.post('', errorData);
}
if (err['message'].includes('No sessions')) {
console.log(err['message']);
process.exit(1);
}
return res.status(err['status'] || 500).json({
status: err['status'] || 500,
error: err['error'] || 'Internal Server Error',

View File

@ -6,7 +6,7 @@ import Jimp from 'jimp';
import mimeTypes from 'mime-types';
import path from 'path';
import { Chatwoot, ConfigService } from '../../config/env.config';
import { ConfigService } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { ROOT_DIR } from '../../config/path.config';
import { ChatwootDto } from '../dto/chatwoot.dto';
@ -613,8 +613,6 @@ export class ChatwootService {
conversationId: number,
content: string,
messageType: 'incoming' | 'outgoing' | undefined,
source_id?: string,
source_reply_id?: string,
privateMessage?: boolean,
attachments?: {
content: unknown;
@ -640,8 +638,6 @@ export class ChatwootService {
message_type: messageType,
attachments: attachments,
private: privateMessage || false,
source_id: source_id,
source_reply_id: source_reply_id,
},
});
@ -737,8 +733,6 @@ export class ChatwootService {
file: string,
messageType: 'incoming' | 'outgoing' | undefined,
content?: string,
source_id?: string,
source_reply_id?: string,
) {
this.logger.verbose('send data to chatwoot');
@ -755,16 +749,6 @@ export class ChatwootService {
this.logger.verbose('temp file found');
data.append('attachments[]', createReadStream(file));
if (source_id) {
this.logger.verbose('source_id found');
data.append('source_id', source_id);
}
if (source_reply_id) {
this.logger.verbose('source_reply_id found');
data.append('source_reply_id', source_reply_id);
}
this.logger.verbose('get client to instance: ' + this.provider.instanceName);
const config = {
method: 'post',
@ -930,10 +914,10 @@ export class ChatwootService {
},
};
const audioWhatsapp = await waInstance?.audioWhatsapp(data);
await waInstance?.audioWhatsapp(data);
this.logger.verbose('audio sent');
return audioWhatsapp;
return;
}
this.logger.verbose('send media to instance: ' + waInstance.instanceName);
@ -955,51 +939,17 @@ export class ChatwootService {
data.mediaMessage.caption = caption;
}
const mediaMessage = await waInstance?.mediaMessage(data);
await waInstance?.mediaMessage(data);
this.logger.verbose('media sent');
return mediaMessage;
return;
} catch (error) {
this.logger.error(error);
}
}
public async updateMessage(
instance: InstanceDto,
accountId: number,
conversationId: number,
messageId: number,
sourceId: string | null,
) {
// const useReplyId = this.configService.get<DelInstance>('DEL_INSTANCE');
const useReplyId = this.configService.get<Chatwoot>('CHATWOOT')?.USE_REPLY_ID;
if (useReplyId === true) {
this.logger.verbose('update message to chatwoot instance: ' + instance.instanceName);
const client = await this.clientCw(instance);
if (!client) {
this.logger.warn('client not found');
return null;
}
this.logger.verbose('check if sourceId to update');
if (sourceId) {
this.logger.verbose('update message to chatwoot');
const dataUpdated = {
source_id: sourceId,
};
await client.messages.update({
accountId,
conversationId,
data: dataUpdated,
messageId,
});
}
}
}
public async receiveWebhook(instance: InstanceDto, body: any) {
try {
// espera 500ms para evitar duplicidade de mensagens
await new Promise((resolve) => setTimeout(resolve, 500));
this.logger.verbose('receive webhook to chatwoot instance: ' + instance.instanceName);
@ -1097,9 +1047,6 @@ export class ChatwootService {
}
for (const message of body.conversation.messages) {
const messageId = message?.id;
const conversationId = message?.conversation_id;
const accountId = message?.account_id;
this.logger.verbose('check if message is media');
if (message.attachments && message.attachments.length > 0) {
this.logger.verbose('message is media');
@ -1110,8 +1057,7 @@ export class ChatwootService {
formatText = null;
}
const mediaMessage = await this.sendAttachment(waInstance, chatId, attachment.data_url, formatText);
await this.updateMessage(instance, accountId, conversationId, messageId, mediaMessage?.key?.id);
await this.sendAttachment(waInstance, chatId, attachment.data_url, formatText);
}
} else {
this.logger.verbose('message is text');
@ -1128,8 +1074,7 @@ export class ChatwootService {
},
};
const message = await waInstance?.textMessage(data);
await this.updateMessage(instance, accountId, conversationId, messageId, message?.key?.id);
await waInstance?.textMessage(data);
}
}
}
@ -1187,7 +1132,7 @@ export class ChatwootService {
thumbnailUrl: string;
sourceUrl: string;
}
let adsMessage: AdsMessage | undefined = msg.extendedTextMessage?.contextInfo.externalAdReply;
const adsMessage: AdsMessage | undefined = msg.extendedTextMessage?.contextInfo.externalAdReply;
this.logger.verbose('Get ads message if it exist');
adsMessage && this.logger.verbose('Ads message: ' + adsMessage);
@ -1218,38 +1163,6 @@ export class ChatwootService {
return types;
}
private getContextIdTypeMessage(msg: any) {
this.logger.verbose('get type message');
const types = {
conversation: msg.conversation?.contextInfo?.stanzaId,
imageMessage: msg.imageMessage?.contextInfo?.stanzaId,
videoMessage: msg.videoMessage?.contextInfo?.stanzaId,
extendedTextMessage: msg.extendedTextMessage?.contextInfo?.stanzaId,
messageContextInfo: msg.messageContextInfo?.stanzaId,
stickerMessage: undefined,
documentMessage: msg.documentMessage?.contextInfo?.stanzaId,
documentWithCaptionMessage: msg.documentWithCaptionMessage?.message?.documentMessage?.contextInfo?.stanzaId,
audioMessage: msg.audioMessage?.contextInfo?.stanzaId,
contactMessage: msg.contactMessage?.contextInfo?.stanzaId,
contactsArrayMessage: msg.contactsArrayMessage?.contextInfo?.stanzaId,
locationMessage: msg.locationMessage?.contextInfo?.stanzaId,
liveLocationMessage: msg.liveLocationMessage?.contextInfo?.stanzaId,
};
this.logger.verbose('type message: ' + types);
return types;
}
private getContextMessageContent(types: any) {
this.logger.verbose('get message context content');
const typeKey = Object.keys(types).find((key) => types[key] !== undefined && types[key] !== '');
const result = typeKey ? types[typeKey] : undefined;
return result;
}
private getMessageContent(types: any) {
this.logger.verbose('get message content');
const typeKey = Object.keys(types).find((key) => types[key] !== undefined);
@ -1349,18 +1262,6 @@ export class ChatwootService {
return messageContent;
}
private getContextConversationMessage(msg: any) {
this.logger.verbose('get context conversation message');
const types = this.getContextIdTypeMessage(msg);
const messageContext = this.getContextMessageContent(types);
this.logger.verbose('context conversation message: ' + messageContext);
return messageContext;
}
public async eventWhatsapp(event: string, instance: InstanceDto, body: any) {
this.logger.verbose('event whatsapp to instance: ' + instance.instanceName);
try {
@ -1389,8 +1290,6 @@ export class ChatwootService {
this.logger.verbose('get conversation message');
const bodyMessage = await this.getConversationMessage(body.message);
const source_reply_id = this.getContextConversationMessage(body.message);
const isMedia = this.isMediaMessage(body.message);
const adsMessage = this.getAdsMessage(body.message);
@ -1410,8 +1309,6 @@ export class ChatwootService {
const messageType = body.key.fromMe ? 'outgoing' : 'incoming';
const source_id = body.key?.id;
this.logger.verbose('message type: ' + messageType);
this.logger.verbose('is media: ' + isMedia);
@ -1456,7 +1353,7 @@ export class ChatwootService {
}
this.logger.verbose('send data to chatwoot');
const send = await this.sendData(getConversation, fileName, messageType, content, source_id, source_reply_id);
const send = await this.sendData(getConversation, fileName, messageType, content);
if (!send) {
this.logger.warn('message not sent');
@ -1477,14 +1374,7 @@ export class ChatwootService {
this.logger.verbose('message is not group');
this.logger.verbose('send data to chatwoot');
const send = await this.sendData(
getConversation,
fileName,
messageType,
bodyMessage,
source_id,
source_reply_id,
);
const send = await this.sendData(getConversation, fileName, messageType, bodyMessage);
if (!send) {
this.logger.warn('message not sent');
@ -1581,14 +1471,7 @@ export class ChatwootService {
}
this.logger.verbose('send data to chatwoot');
const send = await this.createMessage(
instance,
getConversation,
content,
messageType,
source_id,
source_reply_id,
);
const send = await this.createMessage(instance, getConversation, content, messageType);
if (!send) {
this.logger.warn('message not sent');
@ -1609,14 +1492,7 @@ export class ChatwootService {
this.logger.verbose('message is not group');
this.logger.verbose('send data to chatwoot');
const send = await this.createMessage(
instance,
getConversation,
bodyMessage,
messageType,
source_id,
source_reply_id,
);
const send = await this.createMessage(instance, getConversation, bodyMessage, messageType);
if (!send) {
this.logger.warn('message not sent');
@ -1653,14 +1529,14 @@ export class ChatwootService {
}
// if (event === 'connection.update') {
// this.logger.verbose('event connection.update');
// this.logger.verbose('event connection.update');
// if (body.status === 'open') {
// const msgConnection = `🚀 Connection successfully established!`;
// if (body.status === 'open') {
// const msgConnection = `🚀 Connection successfully established!`;
// this.logger.verbose('send message to chatwoot');
// await this.createBotMessage(instance, msgConnection, 'incoming');
// }
// this.logger.verbose('send message to chatwoot');
// await this.createBotMessage(instance, msgConnection, 'incoming');
// }
// }
if (event === 'qrcode.updated') {

View File

@ -1434,8 +1434,9 @@ export class WAStartupService {
if (
type !== 'notify' ||
!received.message ||
!received?.message ||
received.message?.protocolMessage ||
received.message.senderKeyDistributionMessage ||
received.message?.pollUpdateMessage
) {
this.logger.verbose('message rejected');