diff --git a/CHANGELOG.md b/CHANGELOG.md index 20c8772a..a51323a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Added messages.delete event * Added restart instance endpoint +* Created automation for creating instances in the chatwoot bot with the command #inbox_whatsapp: ### Fixed diff --git a/src/whatsapp/controllers/chatwoot.controller.ts b/src/whatsapp/controllers/chatwoot.controller.ts index d66901ef..de0aef7a 100644 --- a/src/whatsapp/controllers/chatwoot.controller.ts +++ b/src/whatsapp/controllers/chatwoot.controller.ts @@ -90,7 +90,7 @@ export class ChatwootController { logger.verbose( 'requested receiveWebhook from ' + instance.instanceName + ' instance', ); - const chatwootService = new ChatwootService(waMonitor); + const chatwootService = new ChatwootService(waMonitor, this.configService); return chatwootService.receiveWebhook(instance, data); } diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 384cedd8..2493e334 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -12,6 +12,7 @@ import mimeTypes from 'mime-types'; import { SendAudioDto } from '../dto/sendMessage.dto'; import { SendMediaDto } from '../dto/sendMessage.dto'; import { ROOT_DIR } from '../../config/path.config'; +import { ConfigService, HttpServer } from '../../config/env.config'; export class ChatwootService { private messageCacheFile: string; @@ -21,7 +22,10 @@ export class ChatwootService { private provider: any; - constructor(private readonly waMonitor: WAMonitoringService) { + constructor( + private readonly waMonitor: WAMonitoringService, + private readonly configService: ConfigService, + ) { this.messageCache = new Set(); } @@ -981,6 +985,39 @@ export class ChatwootService { await waInstance?.client?.logout('Log out instance: ' + instance.instanceName); await waInstance?.client?.ws?.close(); } + + if (command.includes('#inbox_whatsapp')) { + console.log('command include #inbox_whatsapp'); + + const urlServer = this.configService.get('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 ( diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index fbe5f100..fc5ae8b9 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -149,7 +149,7 @@ export class WAStartupService { private endSession = false; private logBaileys = this.configService.get('LOG').BAILEYS; - private chatwootService = new ChatwootService(waMonitor); + private chatwootService = new ChatwootService(waMonitor, this.configService); public set instanceName(name: string) { this.logger.verbose(`Initializing instance '${name}'`); diff --git a/src/whatsapp/whatsapp.module.ts b/src/whatsapp/whatsapp.module.ts index c57b9bf8..46f8ecd1 100644 --- a/src/whatsapp/whatsapp.module.ts +++ b/src/whatsapp/whatsapp.module.ts @@ -72,7 +72,7 @@ const webhookService = new WebhookService(waMonitor); export const webhookController = new WebhookController(webhookService); -const chatwootService = new ChatwootService(waMonitor); +const chatwootService = new ChatwootService(waMonitor, configService); export const chatwootController = new ChatwootController(chatwootService, configService);