fix: test duplicate message media in groups chatwoot

This commit is contained in:
Davidson Gomes 2023-07-13 19:55:23 -03:00
parent 7e88a9084d
commit 2565b934a5

View File

@ -389,7 +389,6 @@ 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;
@ -405,7 +404,6 @@ export class ChatwootService {
content: content, content: content,
message_type: messageType, message_type: messageType,
attachments: attachments, attachments: attachments,
private: privateMessage,
}, },
}); });
@ -416,7 +414,6 @@ 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;
@ -450,7 +447,6 @@ export class ChatwootService {
content: content, content: content,
message_type: messageType, message_type: messageType,
attachments: attachments, attachments: attachments,
private: privateMessage,
}, },
}); });
@ -461,7 +457,6 @@ 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();
@ -472,8 +467,6 @@ 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 = {
@ -489,7 +482,6 @@ 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) {
@ -648,7 +640,6 @@ export class ChatwootService {
instance, instance,
`🚨 Instância ${body.inbox.name} já está conectada.`, `🚨 Instância ${body.inbox.name} já está conectada.`,
'incoming', 'incoming',
false,
); );
} }
} }
@ -661,7 +652,6 @@ export class ChatwootService {
instance, instance,
`⚠️ Instância ${body.inbox.name} não existe.`, `⚠️ Instância ${body.inbox.name} não existe.`,
'incoming', 'incoming',
false,
); );
} }
@ -670,7 +660,6 @@ export class ChatwootService {
instance, instance,
`⚠️ Status da instância ${body.inbox.name}: *${state}*`, `⚠️ Status da instância ${body.inbox.name}: *${state}*`,
'incoming', 'incoming',
false,
); );
} }
} }
@ -678,7 +667,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', false); await this.createBotMessage(instance, msgLogout, 'incoming');
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();
} }
@ -853,17 +842,23 @@ export class ChatwootService {
const fileName = `${path.join(waInstance?.storePath, 'temp', `${nameFile}`)}`; const fileName = `${path.join(waInstance?.storePath, 'temp', `${nameFile}`)}`;
writeFileSync(fileName, fileData); writeFileSync(fileName, fileData, 'utf8');
if (body.key.remoteJid.includes('@g.us') && !body.key.fromMe) { if (body.key.remoteJid.includes('@g.us')) {
const participantName = body.pushName; const participantName = body.pushName;
const content = `**${participantName}**\n\n${bodyMessage}`; let content: string;
if (!body.key.fromMe) {
content = `**${participantName}**\n\n${bodyMessage}`;
} else {
content = `${bodyMessage}`;
}
const send = await this.sendData( const send = await this.sendData(
getConversion, getConversion,
fileName, fileName,
messageType, messageType,
false,
content, content,
); );
@ -871,6 +866,13 @@ export class ChatwootService {
return; return;
} }
this.messageCacheFile = path.join(
ROOT_DIR,
'store',
'chatwoot',
`${instance.instanceName}_cache.txt`,
);
this.messageCache = this.loadMessageCache(); this.messageCache = this.loadMessageCache();
this.messageCache.add(send.id.toString()); this.messageCache.add(send.id.toString());
@ -883,7 +885,6 @@ export class ChatwootService {
getConversion, getConversion,
fileName, fileName,
messageType, messageType,
false,
bodyMessage, bodyMessage,
); );
@ -891,6 +892,13 @@ export class ChatwootService {
return; return;
} }
this.messageCacheFile = path.join(
ROOT_DIR,
'store',
'chatwoot',
`${instance.instanceName}_cache.txt`,
);
this.messageCache = this.loadMessageCache(); this.messageCache = this.loadMessageCache();
this.messageCache.add(send.id.toString()); this.messageCache.add(send.id.toString());
@ -901,19 +909,28 @@ export class ChatwootService {
} }
} }
if (body.key.remoteJid.includes('@g.us') && !body.key.fromMe) { if (body.key.remoteJid.includes('@g.us')) {
const participantName = body.pushName; const participantName = body.pushName;
const content = `**${participantName}**\n\n${bodyMessage}`; let content: string;
if (!body.key.fromMe) {
content = `**${participantName}**\n\n${bodyMessage}`;
} else {
content = `${bodyMessage}`;
}
const send = await this.createMessage( const send = await this.createMessage(
instance, instance,
getConversion, getConversion,
content, content,
messageType, messageType,
false,
); );
if (!send) {
return;
}
this.messageCacheFile = path.join( this.messageCacheFile = path.join(
ROOT_DIR, ROOT_DIR,
'store', 'store',
@ -934,9 +951,12 @@ export class ChatwootService {
getConversion, getConversion,
bodyMessage, bodyMessage,
messageType, messageType,
false,
); );
if (!send) {
return;
}
this.messageCacheFile = path.join( this.messageCacheFile = path.join(
ROOT_DIR, ROOT_DIR,
'store', 'store',
@ -949,6 +969,7 @@ export class ChatwootService {
this.messageCache.add(send.id.toString()); this.messageCache.add(send.id.toString());
this.saveMessageCache(); this.saveMessageCache();
return send; return send;
} }
} }
@ -962,13 +983,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', false); await this.createBotMessage(instance, msgStatus, 'incoming');
} }
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', false); await this.createBotMessage(instance, msgConnection, 'incoming');
} }
} }
@ -993,7 +1014,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', false); return await this.createBotMessage(instance, erroQRcode, 'incoming');
} 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,', ''),