fix: Solved problem with duplicate messages in chatwoot

This commit is contained in:
Davidson Gomes
2023-10-09 18:00:14 -03:00
parent 29fd448998
commit 62c74deac3
9 changed files with 151 additions and 17 deletions

View File

@@ -25,7 +25,7 @@ info:
</font>
[![Run in Postman](https://run.pstmn.io/button.svg)](https://god.gw.postman.com/run-collection/26869335-5546d063-156b-4529-915f-909dd628c090?action=collection%2Ffork&source=rip_markdown&collection-url=entityId%3D26869335-5546d063-156b-4529-915f-909dd628c090%26entityType%3Dcollection%26workspaceId%3D339a4ee7-378b-45c9-b5b8-fd2c0a9c2442)
version: 1.5.2
version: 1.5.4
contact:
name: DavidsonGomes
email: contato@agenciadgcode.com

View File

@@ -1,15 +1,21 @@
import { Request, Response } from 'express';
import { ConfigService } from '../../config/env.config';
import { Auth, ConfigService, HttpServer } from '../../config/env.config';
import { HttpStatus } from '../routers/index.router';
import { WAMonitoringService } from '../services/monitor.service';
export class ViewsController {
constructor(private readonly waMonit: WAMonitoringService, private readonly configService: ConfigService) {}
constructor(private readonly waMonitor: WAMonitoringService, private readonly configService: ConfigService) {}
public async manager(request: Request, response: Response) {
try {
return response.status(HttpStatus.OK).render('manager');
const token = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
const port = this.configService.get<HttpServer>('SERVER').PORT;
const instances = await this.waMonitor.instanceInfo();
console.log('INSTANCES: ', instances);
return response.status(HttpStatus.OK).render('manager', { token, port, instances });
} catch (error) {
console.log('ERROR: ', error);
}

View File

@@ -914,7 +914,7 @@ export class ChatwootService {
},
};
await waInstance?.audioWhatsapp(data);
await waInstance?.audioWhatsapp(data, true);
this.logger.verbose('audio sent');
return;
@@ -939,7 +939,7 @@ export class ChatwootService {
data.mediaMessage.caption = caption;
}
await waInstance?.mediaMessage(data);
await waInstance?.mediaMessage(data, true);
this.logger.verbose('media sent');
return;
@@ -1074,7 +1074,7 @@ export class ChatwootService {
},
};
await waInstance?.textMessage(data);
await waInstance?.textMessage(data, true);
}
}
}

View File

@@ -33,6 +33,7 @@ import makeWASocket, {
WAMessageUpdate,
WASocket,
} from '@whiskeysockets/baileys';
import MAIN_LOGGER from '@whiskeysockets/baileys/lib/Utils/logger';
import axios from 'axios';
import { exec, execSync } from 'child_process';
import { arrayUnique, isBase64, isURL } from 'class-validator';
@@ -131,6 +132,8 @@ import { ChatwootService } from './chatwoot.service';
//import { SocksProxyAgent } from './socks-proxy-agent';
import { TypebotService } from './typebot.service';
const logger = MAIN_LOGGER.child({});
export class WAStartupService {
constructor(
private readonly configService: ConfigService,
@@ -1542,7 +1545,7 @@ export class WAStartupService {
'buffer',
{},
{
logger: P({ level: 'error' }),
logger: logger,
reuploadRequest: this.client.updateMediaMessage,
},
);
@@ -2061,7 +2064,12 @@ export class WAStartupService {
}
}
private async sendMessageWithTyping<T = proto.IMessage>(number: string, message: T, options?: Options) {
private async sendMessageWithTyping<T = proto.IMessage>(
number: string,
message: T,
options?: Options,
isChatwoot = false,
) {
this.logger.verbose('Sending message with typing');
this.logger.verbose(`Check if number "${number}" is WhatsApp`);
@@ -2219,7 +2227,7 @@ export class WAStartupService {
this.logger.verbose('Sending data to webhook in event SEND_MESSAGE');
await this.sendDataWebhook(Events.SEND_MESSAGE, messageRaw);
if (this.localChatwoot.enabled) {
if (this.localChatwoot.enabled && !isChatwoot) {
this.chatwootService.eventWhatsapp(Events.SEND_MESSAGE, { instanceName: this.instance.name }, messageRaw);
}
@@ -2244,7 +2252,7 @@ export class WAStartupService {
}
// Send Message Controller
public async textMessage(data: SendTextDto) {
public async textMessage(data: SendTextDto, isChatwoot = false) {
this.logger.verbose('Sending text message');
return await this.sendMessageWithTyping(
data.number,
@@ -2252,6 +2260,7 @@ export class WAStartupService {
conversation: data.textMessage.text,
},
data?.options,
isChatwoot,
);
}
@@ -2528,11 +2537,11 @@ export class WAStartupService {
return result;
}
public async mediaMessage(data: SendMediaDto) {
public async mediaMessage(data: SendMediaDto, isChatwoot = false) {
this.logger.verbose('Sending media message');
const generate = await this.prepareMediaMessage(data.mediaMessage);
return await this.sendMessageWithTyping(data.number, { ...generate.message }, data?.options);
return await this.sendMessageWithTyping(data.number, { ...generate.message }, data?.options, isChatwoot);
}
public async processAudio(audio: string, number: string) {
@@ -2589,7 +2598,7 @@ export class WAStartupService {
});
}
public async audioWhatsapp(data: SendAudioDto) {
public async audioWhatsapp(data: SendAudioDto, isChatwoot = false) {
this.logger.verbose('Sending audio whatsapp');
if (!data.options?.encoding && data.options?.encoding !== false) {
@@ -2608,6 +2617,7 @@ export class WAStartupService {
mimetype: 'audio/mp4',
},
{ presence: 'recording', delay: data?.options?.delay },
isChatwoot,
);
fs.unlinkSync(convert);
@@ -2629,6 +2639,7 @@ export class WAStartupService {
mimetype: 'audio/ogg; codecs=opus',
},
{ presence: 'recording', delay: data?.options?.delay },
isChatwoot,
);
}