diff --git a/src/config/env.config.ts b/src/config/env.config.ts index d13eec07..1849ceef 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -4,7 +4,7 @@ import { join } from 'path'; import { SRC_DIR } from './path.config'; import { isBooleanString } from 'class-validator'; -export type HttpServer = { TYPE: 'http' | 'https'; PORT: number }; +export type HttpServer = { TYPE: 'http' | 'https'; PORT: number; URL: string }; export type HttpMethods = 'POST' | 'GET' | 'PUT' | 'DELETE'; export type Cors = { @@ -98,9 +98,9 @@ export type Instance = { NAME: string; WEBHOOK_URL: string; MODE: string; - CHATWOOT_ACCOUNT_ID: string; - CHATWOOT_TOKEN: string; - CHATWOOT_URL: string; + CHATWOOT_ACCOUNT_ID?: string; + CHATWOOT_TOKEN?: string; + CHATWOOT_URL?: string; }; export type Auth = { API_KEY: ApiKey; @@ -159,6 +159,7 @@ export class ConfigService { if (process.env?.DOCKER_ENV === 'true') { this.env.SERVER.TYPE = 'http'; this.env.SERVER.PORT = 8080; + this.env.SERVER.URL = `http://localhost:${this.env.SERVER.PORT}`; } } @@ -173,6 +174,7 @@ export class ConfigService { SERVER: { TYPE: process.env.SERVER_TYPE as 'http' | 'https', PORT: Number.parseInt(process.env.SERVER_PORT), + URL: process.env.SERVER_URL, }, CORS: { ORIGIN: process.env.CORS_ORIGIN.split(','), @@ -278,9 +280,10 @@ export class ConfigService { NAME: process.env.AUTHENTICATION_INSTANCE_NAME, WEBHOOK_URL: process.env.AUTHENTICATION_INSTANCE_WEBHOOK_URL, MODE: process.env.AUTHENTICATION_INSTANCE_MODE, - CHATWOOT_ACCOUNT_ID: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID, - CHATWOOT_TOKEN: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN, - CHATWOOT_URL: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_URL, + CHATWOOT_ACCOUNT_ID: + process.env.AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID || '', + CHATWOOT_TOKEN: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN || '', + CHATWOOT_URL: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_URL || '', }, }, }; diff --git a/src/dev-env.yml b/src/dev-env.yml index 55273971..244ff73d 100644 --- a/src/dev-env.yml +++ b/src/dev-env.yml @@ -8,6 +8,7 @@ SERVER: TYPE: http # https PORT: 8080 # 443 + URL: localhost CORS: ORIGIN: diff --git a/src/whatsapp/controllers/chatwoot.controller.ts b/src/whatsapp/controllers/chatwoot.controller.ts index 71519c39..a4367833 100644 --- a/src/whatsapp/controllers/chatwoot.controller.ts +++ b/src/whatsapp/controllers/chatwoot.controller.ts @@ -5,11 +5,15 @@ import { ChatwootDto } from '../dto/chatwoot.dto'; import { ChatwootService } from '../services/chatwoot.service'; import { Logger } from '../../config/logger.config'; import { waMonitor } from '../whatsapp.module'; +import { ConfigService, HttpServer } from '../../config/env.config'; const logger = new Logger('ChatwootController'); export class ChatwootController { - constructor(private readonly chatwootService: ChatwootService) {} + constructor( + private readonly chatwootService: ChatwootService, + private readonly configService: ConfigService, + ) {} public async createChatwoot(instance: InstanceDto, data: ChatwootDto) { logger.verbose( @@ -46,9 +50,11 @@ export class ChatwootController { const result = this.chatwootService.create(instance, data); + const urlServer = this.configService.get('SERVER').URL; + const response = { ...result, - webhook_url: `/chatwoot/webhook/${instance.instanceName}`, + webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`, }; return response; @@ -56,11 +62,13 @@ export class ChatwootController { public async findChatwoot(instance: InstanceDto) { logger.verbose('requested findChatwoot from ' + instance.instanceName + ' instance'); - const result = this.chatwootService.find(instance); + const result = await this.chatwootService.find(instance); + + const urlServer = this.configService.get('SERVER').URL; const response = { ...result, - webhook_url: `/chatwoot/webhook/${instance.instanceName}`, + webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`, }; return response; diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 51ad9b37..b04933db 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -1,6 +1,6 @@ import { delay } from '@whiskeysockets/baileys'; import EventEmitter2 from 'eventemitter2'; -import { Auth, ConfigService } from '../../config/env.config'; +import { Auth, ConfigService, HttpServer } from '../../config/env.config'; import { BadRequestException, InternalServerErrorException } from '../../exceptions'; import { InstanceDto } from '../dto/instance.dto'; import { RepositoryBroker } from '../repository/repository.manager'; @@ -154,6 +154,8 @@ export class InstanceController { this.logger.log(error); } + const urlServer = this.configService.get('SERVER').URL; + return { instance: { instanceName: instance.instanceName, @@ -167,7 +169,7 @@ export class InstanceController { url: chatwoot_url, sign_msg: chatwoot_sign_msg, name_inbox: instance.instanceName, - webhook_url: `/chatwoot/webhook/${instance.instanceName}`, + webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`, }, }; } else { @@ -288,6 +290,8 @@ export class InstanceController { this.logger.log(error); } + const urlServer = this.configService.get('SERVER').URL; + return { instance: { instanceName: instance.instanceName, @@ -301,7 +305,7 @@ export class InstanceController { url: chatwoot_url, sign_msg: chatwoot_sign_msg, name_inbox: instance.instanceName, - webhook_url: `/chatwoot/webhook/${instance.instanceName}`, + webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`, }, }; } diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 24fbcb01..802ea353 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -42,22 +42,26 @@ export class ChatwootService { } private async getProvider(instance: InstanceDto) { - const provider = await this.waMonitor.waInstances[ - instance.instanceName - ].findChatwoot(); + try { + const provider = await this.waMonitor.waInstances[ + instance.instanceName + ].findChatwoot(); - if (!provider) { + if (!provider) { + return null; + } + + return provider; + } catch (error) { return null; } - - return provider; } private async clientCw(instance: InstanceDto) { const provider = await this.getProvider(instance); if (!provider) { - throw new Error('provider not found'); + this.logger.error('provider not found'); } this.provider = provider; @@ -78,7 +82,7 @@ export class ChatwootService { this.logger.verbose('create chatwoot: ' + instance.instanceName); this.waMonitor.waInstances[instance.instanceName].setChatwoot(data); - return { chatwoot: { ...instance, chatwoot: data } }; + return data; } public async find(instance: InstanceDto): Promise { @@ -583,6 +587,21 @@ export class ChatwootService { } } + if (body.message_type === 'template' && body.content_type === 'input_csat') { + const data: SendTextDto = { + number: chatId, + textMessage: { + text: body.content, + }, + options: { + delay: 1200, + presence: 'composing', + }, + }; + + await waInstance?.textMessage(data); + } + return { message: 'bot' }; } catch (error) { console.log(error); diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index 0f4efcac..60058254 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -9,6 +9,7 @@ import { ConfigService, Database, DelInstance, + HttpServer, Redis, } from '../../config/env.config'; import { RepositoryBroker } from '../repository/repository.manager'; @@ -83,6 +84,19 @@ export class WAMonitoringService { for await (const [key, value] of Object.entries(this.waInstances)) { if (value) { this.logger.verbose('get instance info: ' + key); + let chatwoot: any; + + const urlServer = this.configService.get('SERVER').URL; + + const findChatwoot = await this.waInstances[key].findChatwoot(); + + if (findChatwoot.enabled) { + chatwoot = { + ...findChatwoot, + webhook_url: `${urlServer}/chatwoot/webhook/${key}`, + }; + } + if (value.connectionStatus.state === 'open') { this.logger.verbose('instance: ' + key + ' - connectionStatus: open'); let apikey: string; @@ -101,6 +115,7 @@ export class WAMonitoringService { profilePictureUrl: value.profilePictureUrl, status: (await value.getProfileStatus()) || '', apikey, + chatwoot, }, }); } else { @@ -114,6 +129,7 @@ export class WAMonitoringService { profileName: (await value.getProfileName()) || 'not loaded', profilePictureUrl: value.profilePictureUrl, status: (await value.getProfileStatus()) || '', + chatwoot, }, }); } @@ -134,6 +150,7 @@ export class WAMonitoringService { instanceName: key, status: value.connectionStatus.state, apikey, + chatwoot, }, }); } else { @@ -144,6 +161,7 @@ export class WAMonitoringService { instance: { instanceName: key, status: value.connectionStatus.state, + chatwoot, }, }); } diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 9d56730c..068a61be 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -30,7 +30,6 @@ import makeWASocket, { WAMessageUpdate, WASocket, getAggregateVotesInPollMessage, - Browsers, } from '@whiskeysockets/baileys'; import { Auth, @@ -38,6 +37,7 @@ import { ConfigService, ConfigSessionPhone, Database, + HttpServer, QrCode, Redis, Webhook, @@ -343,6 +343,7 @@ export class WAStartupService { public async sendDataWebhook(event: Events, data: T, local = true) { const webhookGlobal = this.configService.get('WEBHOOK'); + const urlServer = this.configService.get('SERVER').URL; const webhookLocal = this.localWebhook.events; const we = event.replace(/[\.-]/gm, '_').toUpperCase(); const transformedWe = we.replace(/_/gm, '-').toLowerCase(); @@ -367,6 +368,7 @@ export class WAStartupService { instance: this.instance.name, data, destination: this.localWebhook.url, + urlServer, }); } @@ -378,6 +380,7 @@ export class WAStartupService { instance: this.instance.name, data, destination: this.localWebhook.url, + urlServer, }); } } catch (error) { @@ -425,6 +428,7 @@ export class WAStartupService { instance: this.instance.name, data, destination: localUrl, + urlServer, }); } @@ -436,6 +440,7 @@ export class WAStartupService { instance: this.instance.name, data, destination: localUrl, + urlServer, }); } } catch (error) { @@ -719,8 +724,7 @@ export class WAStartupService { const { version } = await fetchLatestBaileysVersion(); this.logger.verbose('Baileys version: ' + version); const session = this.configService.get('CONFIG_SESSION_PHONE'); - // const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()]; - const browser: WABrowserDescription = Browsers.appropriate(session.CLIENT); + const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()]; this.logger.verbose('Browser: ' + JSON.stringify(browser)); const socketConfig: UserFacingSocketConfig = { diff --git a/src/whatsapp/whatsapp.module.ts b/src/whatsapp/whatsapp.module.ts index a03ca18d..64d463d2 100644 --- a/src/whatsapp/whatsapp.module.ts +++ b/src/whatsapp/whatsapp.module.ts @@ -74,7 +74,7 @@ export const webhookController = new WebhookController(webhookService); const chatwootService = new ChatwootService(waMonitor); -export const chatwootController = new ChatwootController(chatwootService); +export const chatwootController = new ChatwootController(chatwootService, configService); export const instanceController = new InstanceController( waMonitor,