fix: test duplicate message media in groups chatwoot

This commit is contained in:
Davidson Gomes 2023-07-13 19:23:59 -03:00
parent ef03292e35
commit 7e88a9084d
3 changed files with 71 additions and 15 deletions

View File

@ -16,6 +16,7 @@
* Fits the format on return from the fetchAllGroups endpoint * Fits the format on return from the fetchAllGroups endpoint
* Adjust in send document with caption from chatwoot * Adjust in send document with caption from chatwoot
* Fixed message with undefind in chatwoot * Fixed message with undefind in chatwoot
* Changed message in path /
# 1.1.5 (2023-07-12 07:17) # 1.1.5 (2023-07-12 07:17)

View File

@ -25,6 +25,12 @@ const authType = configService.get<Auth>('AUTHENTICATION').TYPE;
const guards = [instanceExistsGuard, instanceLoggedGuard, authGuard[authType]]; const guards = [instanceExistsGuard, instanceLoggedGuard, authGuard[authType]];
router router
.get('/', (req, res) => {
res.status(HttpStatus.OK).json({
status: HttpStatus.OK,
message: 'Welcome to the Evolution API, it is working!',
});
})
.use( .use(
'/instance', '/instance',
new InstanceRouter(configService, ...guards).router, new InstanceRouter(configService, ...guards).router,

View File

@ -389,6 +389,7 @@ export class ChatwootService {
conversationId: number, conversationId: number,
content: string, content: string,
messageType: 'incoming' | 'outgoing' | undefined, messageType: 'incoming' | 'outgoing' | undefined,
privateMessage?: boolean,
attachments?: { attachments?: {
content: unknown; content: unknown;
encoding: string; encoding: string;
@ -404,6 +405,7 @@ export class ChatwootService {
content: content, content: content,
message_type: messageType, message_type: messageType,
attachments: attachments, attachments: attachments,
private: privateMessage,
}, },
}); });
@ -414,6 +416,7 @@ export class ChatwootService {
instance: InstanceDto, instance: InstanceDto,
content: string, content: string,
messageType: 'incoming' | 'outgoing' | undefined, messageType: 'incoming' | 'outgoing' | undefined,
privateMessage?: boolean,
attachments?: { attachments?: {
content: unknown; content: unknown;
encoding: string; encoding: string;
@ -436,6 +439,10 @@ export class ChatwootService {
conversation?.meta?.sender?.id === contact.id && conversation.status === 'open', conversation?.meta?.sender?.id === contact.id && conversation.status === 'open',
); );
if (!conversation) {
return;
}
const message = await client.messages.create({ const message = await client.messages.create({
accountId: this.provider.account_id, accountId: this.provider.account_id,
conversationId: conversation.id, conversationId: conversation.id,
@ -443,6 +450,7 @@ export class ChatwootService {
content: content, content: content,
message_type: messageType, message_type: messageType,
attachments: attachments, attachments: attachments,
private: privateMessage,
}, },
}); });
@ -453,6 +461,7 @@ export class ChatwootService {
conversationId: number, conversationId: number,
file: string, file: string,
messageType: 'incoming' | 'outgoing' | undefined, messageType: 'incoming' | 'outgoing' | undefined,
privateMessage: boolean,
content?: string, content?: string,
) { ) {
const data = new FormData(); const data = new FormData();
@ -463,6 +472,8 @@ export class ChatwootService {
data.append('message_type', messageType); data.append('message_type', messageType);
data.append('private', privateMessage);
data.append('attachments[]', createReadStream(file)); data.append('attachments[]', createReadStream(file));
const config = { const config = {
@ -478,10 +489,12 @@ export class ChatwootService {
try { try {
const { data } = await axios.request(config); const { data } = await axios.request(config);
unlinkSync(file); unlinkSync(file);
return data; return data;
} catch (error) { } catch (error) {
console.log(error); console.log(error);
unlinkSync(file);
} }
} }
@ -635,6 +648,7 @@ export class ChatwootService {
instance, instance,
`🚨 Instância ${body.inbox.name} já está conectada.`, `🚨 Instância ${body.inbox.name} já está conectada.`,
'incoming', 'incoming',
false,
); );
} }
} }
@ -647,6 +661,7 @@ export class ChatwootService {
instance, instance,
`⚠️ Instância ${body.inbox.name} não existe.`, `⚠️ Instância ${body.inbox.name} não existe.`,
'incoming', 'incoming',
false,
); );
} }
@ -655,6 +670,7 @@ export class ChatwootService {
instance, instance,
`⚠️ Status da instância ${body.inbox.name}: *${state}*`, `⚠️ Status da instância ${body.inbox.name}: *${state}*`,
'incoming', 'incoming',
false,
); );
} }
} }
@ -662,7 +678,7 @@ export class ChatwootService {
if (command === 'desconectar') { if (command === 'desconectar') {
const msgLogout = `🚨 Desconectando Whatsapp da caixa de entrada *${body.inbox.name}*: `; const msgLogout = `🚨 Desconectando Whatsapp da caixa de entrada *${body.inbox.name}*: `;
await this.createBotMessage(instance, msgLogout, 'incoming'); await this.createBotMessage(instance, msgLogout, 'incoming', false);
await waInstance?.client?.logout('Log out instance: ' + instance.instanceName); await waInstance?.client?.logout('Log out instance: ' + instance.instanceName);
await waInstance?.client?.ws?.close(); await waInstance?.client?.ws?.close();
} }
@ -819,7 +835,7 @@ export class ChatwootService {
const bodyMessage = await this.getConversationMessage(body.message); const bodyMessage = await this.getConversationMessage(body.message);
if (!bodyMessage) { if (!bodyMessage && !isMedia) {
return; return;
} }
@ -835,25 +851,57 @@ export class ChatwootService {
const fileData = Buffer.from(downloadBase64.base64, 'base64'); const fileData = Buffer.from(downloadBase64.base64, 'base64');
const fileName = `${path.join( const fileName = `${path.join(waInstance?.storePath, 'temp', `${nameFile}`)}`;
waInstance?.storePath,
'chatwoot',
`${nameFile}`,
)}`;
writeFileSync(fileName, fileData, 'utf8'); writeFileSync(fileName, fileData);
if (body.key.remoteJid.includes('@g.us') && !body.key.fromMe) { if (body.key.remoteJid.includes('@g.us') && !body.key.fromMe) {
const participantName = body.pushName; const participantName = body.pushName;
const content = `**${participantName}**\n\n${bodyMessage}`; const content = `**${participantName}**\n\n${bodyMessage}`;
return await this.sendData(getConversion, fileName, messageType, content); const send = await this.sendData(
getConversion,
fileName,
messageType,
false,
content,
);
if (!send) {
return;
}
this.messageCache = this.loadMessageCache();
this.messageCache.add(send.id.toString());
this.saveMessageCache();
return send;
} else { } else {
return await this.sendData(getConversion, fileName, messageType, bodyMessage); const send = await this.sendData(
getConversion,
fileName,
messageType,
false,
bodyMessage,
);
if (!send) {
return;
}
this.messageCache = this.loadMessageCache();
this.messageCache.add(send.id.toString());
this.saveMessageCache();
return send;
} }
} }
if (body.key.remoteJid.includes('@g.us')) { if (body.key.remoteJid.includes('@g.us') && !body.key.fromMe) {
const participantName = body.pushName; const participantName = body.pushName;
const content = `**${participantName}**\n\n${bodyMessage}`; const content = `**${participantName}**\n\n${bodyMessage}`;
@ -863,6 +911,7 @@ export class ChatwootService {
getConversion, getConversion,
content, content,
messageType, messageType,
false,
); );
this.messageCacheFile = path.join( this.messageCacheFile = path.join(
@ -885,6 +934,7 @@ export class ChatwootService {
getConversion, getConversion,
bodyMessage, bodyMessage,
messageType, messageType,
false,
); );
this.messageCacheFile = path.join( this.messageCacheFile = path.join(
@ -899,7 +949,6 @@ export class ChatwootService {
this.messageCache.add(send.id.toString()); this.messageCache.add(send.id.toString());
this.saveMessageCache(); this.saveMessageCache();
return send; return send;
} }
} }
@ -913,13 +962,13 @@ export class ChatwootService {
} }
const msgStatus = `⚡️ Status da instância ${inbox.name}: ${data.status}`; const msgStatus = `⚡️ Status da instância ${inbox.name}: ${data.status}`;
await this.createBotMessage(instance, msgStatus, 'incoming'); await this.createBotMessage(instance, msgStatus, 'incoming', false);
} }
if (event === 'connection.update') { if (event === 'connection.update') {
if (body.state === 'open') { if (body.state === 'open') {
const msgConnection = `🚀 Conexão realizada com sucesso!`; const msgConnection = `🚀 Conexão realizada com sucesso!`;
await this.createBotMessage(instance, msgConnection, 'incoming'); await this.createBotMessage(instance, msgConnection, 'incoming', false);
} }
} }
@ -944,7 +993,7 @@ export class ChatwootService {
if (event === 'qrcode.updated') { if (event === 'qrcode.updated') {
if (body.statusCode === 500) { if (body.statusCode === 500) {
const erroQRcode = `🚨 Limite de geração de QRCode atingido, para gerar um novo QRCode, envie a mensagem /iniciar novamente.`; const erroQRcode = `🚨 Limite de geração de QRCode atingido, para gerar um novo QRCode, envie a mensagem /iniciar novamente.`;
return await this.createBotMessage(instance, erroQRcode, 'incoming'); return await this.createBotMessage(instance, erroQRcode, 'incoming', false);
} else { } else {
const fileData = Buffer.from( const fileData = Buffer.from(
body?.qrcode.base64.replace('data:image/png;base64,', ''), body?.qrcode.base64.replace('data:image/png;base64,', ''),