feat: Created automation for creating instances in the chatwoot bot with the command #inbox_whatsapp:<INSTANCE_NAME>

This commit is contained in:
Davidson Gomes 2023-07-17 15:37:49 -03:00
parent 71a3e75ae2
commit 0cb87e6ed9
5 changed files with 42 additions and 4 deletions

View File

@ -4,6 +4,7 @@
* Added messages.delete event * Added messages.delete event
* Added restart instance endpoint * Added restart instance endpoint
* Created automation for creating instances in the chatwoot bot with the command #inbox_whatsapp:<INSTANCE_NAME>
### Fixed ### Fixed

View File

@ -90,7 +90,7 @@ export class ChatwootController {
logger.verbose( logger.verbose(
'requested receiveWebhook from ' + instance.instanceName + ' instance', 'requested receiveWebhook from ' + instance.instanceName + ' instance',
); );
const chatwootService = new ChatwootService(waMonitor); const chatwootService = new ChatwootService(waMonitor, this.configService);
return chatwootService.receiveWebhook(instance, data); return chatwootService.receiveWebhook(instance, data);
} }

View File

@ -12,6 +12,7 @@ import mimeTypes from 'mime-types';
import { SendAudioDto } from '../dto/sendMessage.dto'; import { SendAudioDto } from '../dto/sendMessage.dto';
import { SendMediaDto } from '../dto/sendMessage.dto'; import { SendMediaDto } from '../dto/sendMessage.dto';
import { ROOT_DIR } from '../../config/path.config'; import { ROOT_DIR } from '../../config/path.config';
import { ConfigService, HttpServer } from '../../config/env.config';
export class ChatwootService { export class ChatwootService {
private messageCacheFile: string; private messageCacheFile: string;
@ -21,7 +22,10 @@ export class ChatwootService {
private provider: any; private provider: any;
constructor(private readonly waMonitor: WAMonitoringService) { constructor(
private readonly waMonitor: WAMonitoringService,
private readonly configService: ConfigService,
) {
this.messageCache = new Set(); this.messageCache = new Set();
} }
@ -981,6 +985,39 @@ export class ChatwootService {
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();
} }
if (command.includes('#inbox_whatsapp')) {
console.log('command include #inbox_whatsapp');
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
console.log('url server: ' + urlServer);
console.log('api key: ' + apiKey);
const data = {
instanceName: command.split(':')[1],
qrcode: true,
chatwoot_account_id: this.provider.account_id,
chatwoot_token: this.provider.token,
chatwoot_url: this.provider.url,
chatwoot_sign_msg: this.provider.sign_msg,
};
const config = {
method: 'post',
maxBodyLength: Infinity,
url: `${urlServer}/instance/create`,
headers: {
'Content-Type': 'application/json',
apikey: apiKey,
},
data: data,
};
const { data: response } = await axios.request(config);
console.log(response);
}
} }
if ( if (

View File

@ -149,7 +149,7 @@ export class WAStartupService {
private endSession = false; private endSession = false;
private logBaileys = this.configService.get<Log>('LOG').BAILEYS; private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
private chatwootService = new ChatwootService(waMonitor); private chatwootService = new ChatwootService(waMonitor, this.configService);
public set instanceName(name: string) { public set instanceName(name: string) {
this.logger.verbose(`Initializing instance '${name}'`); this.logger.verbose(`Initializing instance '${name}'`);

View File

@ -72,7 +72,7 @@ const webhookService = new WebhookService(waMonitor);
export const webhookController = new WebhookController(webhookService); export const webhookController = new WebhookController(webhookService);
const chatwootService = new ChatwootService(waMonitor); const chatwootService = new ChatwootService(waMonitor, configService);
export const chatwootController = new ChatwootController(chatwootService, configService); export const chatwootController = new ChatwootController(chatwootService, configService);