diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cc6ee82..c4263824 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ### Fixed * Fixed Lid Messages +* Fixed sending variables to typebot +* Fixed sending variables from typebot # 1.6.0 (2023-12-12 17:24) diff --git a/Dockerfile b/Dockerfile index 10be07c4..82201346 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM node:20.7.0-alpine -LABEL version="1.6.0" description="Api to control whatsapp features through http requests." +LABEL version="1.6.1" description="Api to control whatsapp features through http requests." LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes" LABEL contact="contato@agenciadgcode.com" diff --git a/package.json b/package.json index 1987ae35..54f8e4a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "evolution-api", - "version": "1.6.0", + "version": "1.6.1", "description": "Rest api for communication with WhatsApp", "main": "./dist/src/main.js", "scripts": { @@ -56,7 +56,7 @@ "cross-env": "^7.0.3", "dayjs": "^1.11.7", "eventemitter2": "^6.4.9", - "evolution-manager": "^0.4.5", + "evolution-manager": "^0.4.6", "exiftool-vendored": "^22.0.0", "express": "^4.18.2", "express-async-errors": "^3.1.1", diff --git a/src/config/env.config.ts b/src/config/env.config.ts index da491505..69e79f54 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -169,8 +169,8 @@ export class ConfigService { this.env = !(process.env?.DOCKER_ENV === 'true') ? this.envYaml() : this.envProcess(); this.env.PRODUCTION = process.env?.NODE_ENV === 'PROD'; if (process.env?.DOCKER_ENV === 'true') { - this.env.SERVER.TYPE = 'http'; - this.env.SERVER.PORT = 8080; + this.env.SERVER.TYPE = process.env.SERVER_TYPE as 'http' | 'http'; + this.env.SERVER.PORT = Number.parseInt(process.env.SERVER_PORT) || 8080; } } diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index 589ba8dc..603aa0e0 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -25,7 +25,7 @@ info: [![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.5 + version: 1.6.1 contact: name: DavidsonGomes email: contato@agenciadgcode.com diff --git a/src/whatsapp/routers/index.router.ts b/src/whatsapp/routers/index.router.ts index 3dd4492b..781b528c 100644 --- a/src/whatsapp/routers/index.router.ts +++ b/src/whatsapp/routers/index.router.ts @@ -41,7 +41,8 @@ router status: HttpStatus.OK, message: 'Welcome to the Evolution API, it is working!', version: packageJson.version, - documentation: `${req.protocol}://${req.get('host')}/docs`, + swagger: `${req.protocol}://${req.get('host')}/docs`, + documentation: `https://doc.evolution-api.com`, manager: `${req.protocol}://${req.get('host')}/manager`, }); }) diff --git a/src/whatsapp/services/typebot.service.ts b/src/whatsapp/services/typebot.service.ts index 0da51193..6959dfc7 100644 --- a/src/whatsapp/services/typebot.service.ts +++ b/src/whatsapp/services/typebot.service.ts @@ -1,4 +1,5 @@ import axios from 'axios'; +import EventEmitter2 from 'eventemitter2'; import { ConfigService, Typebot } from '../../config/env.config'; import { Logger } from '../../config/logger.config'; @@ -9,7 +10,15 @@ import { Events } from '../types/wa.types'; import { WAMonitoringService } from './monitor.service'; export class TypebotService { - constructor(private readonly waMonitor: WAMonitoringService, private readonly configService: ConfigService) {} + constructor( + private readonly waMonitor: WAMonitoringService, + private readonly configService: ConfigService, + private readonly eventEmitter: EventEmitter2, + ) { + this.eventEmitter.on('typebot:end', async (data) => { + await this.clearSessions(data.instance, data.remoteJid); + }); + } private readonly logger = new Logger(TypebotService.name); @@ -110,6 +119,37 @@ export class TypebotService { return { typebot: { ...instance, typebot: typebotData } }; } + public async clearSessions(instance: InstanceDto, remoteJid: string) { + const findTypebot = await this.find(instance); + const sessions = (findTypebot.sessions as Session[]) ?? []; + + const sessionWithRemoteJid = sessions.filter((session) => session.remoteJid === remoteJid); + + if (sessionWithRemoteJid.length > 0) { + sessionWithRemoteJid.forEach((session) => { + sessions.splice(sessions.indexOf(session), 1); + }); + + const typebotData = { + enabled: findTypebot.enabled, + url: findTypebot.url, + typebot: findTypebot.typebot, + expire: findTypebot.expire, + keyword_finish: findTypebot.keyword_finish, + delay_message: findTypebot.delay_message, + unknown_message: findTypebot.unknown_message, + listening_from_me: findTypebot.listening_from_me, + sessions, + }; + + this.create(instance, typebotData); + + return sessions; + } + + return sessions; + } + public async startTypebot(instance: InstanceDto, data: any) { if (data.remoteJid === 'status@broadcast') return; @@ -169,20 +209,25 @@ export class TypebotService { } else { const id = Math.floor(Math.random() * 10000000000).toString(); - const reqData = { - startParams: { - publicId: data.typebot, - prefilledVariables: prefilledVariables, - }, - }; - try { const version = this.configService.get('TYPEBOT').API_VERSION; let url: string; + let reqData: {}; if (version === 'latest') { url = `${data.url}/api/v1/typebots/${data.typebot}/startChat`; + + reqData = { + prefilledVariables: prefilledVariables, + }; } else { url = `${data.url}/api/v1/sendMessage`; + + reqData = { + startParams: { + publicId: data.typebot, + prefilledVariables: prefilledVariables, + }, + }; } const request = await axios.post(url, reqData); @@ -260,25 +305,35 @@ export class TypebotService { if (data.remoteJid === 'status@broadcast') return; const id = Math.floor(Math.random() * 10000000000).toString(); - const reqData = { - startParams: { - publicId: data.typebot, - prefilledVariables: { - ...data.prefilledVariables, - remoteJid: data.remoteJid, - pushName: data.pushName || data.prefilledVariables?.pushName || '', - instanceName: instance.instanceName, - }, - }, - }; - try { const version = this.configService.get('TYPEBOT').API_VERSION; let url: string; + let reqData: {}; if (version === 'latest') { url = `${data.url}/api/v1/typebots/${data.typebot}/startChat`; + + reqData = { + prefilledVariables: { + ...data.prefilledVariables, + remoteJid: data.remoteJid, + pushName: data.pushName || data.prefilledVariables?.pushName || '', + instanceName: instance.instanceName, + }, + }; } else { url = `${data.url}/api/v1/sendMessage`; + + reqData = { + startParams: { + publicId: data.typebot, + prefilledVariables: { + ...data.prefilledVariables, + remoteJid: data.remoteJid, + pushName: data.pushName || data.prefilledVariables?.pushName || '', + instanceName: instance.instanceName, + }, + }, + }; } const request = await axios.post(url, reqData); @@ -318,37 +373,6 @@ export class TypebotService { } } - public async clearSessions(instance: InstanceDto, remoteJid: string) { - const findTypebot = await this.find(instance); - const sessions = (findTypebot.sessions as Session[]) ?? []; - - const sessionWithRemoteJid = sessions.filter((session) => session.remoteJid === remoteJid); - - if (sessionWithRemoteJid.length > 0) { - sessionWithRemoteJid.forEach((session) => { - sessions.splice(sessions.indexOf(session), 1); - }); - - const typebotData = { - enabled: findTypebot.enabled, - url: findTypebot.url, - typebot: findTypebot.typebot, - expire: findTypebot.expire, - keyword_finish: findTypebot.keyword_finish, - delay_message: findTypebot.delay_message, - unknown_message: findTypebot.unknown_message, - listening_from_me: findTypebot.listening_from_me, - sessions, - }; - - this.create(instance, typebotData); - - return sessions; - } - - return sessions; - } - public async sendWAMessage( instance: InstanceDto, remoteJid: string, @@ -356,11 +380,15 @@ export class TypebotService { input: any[], clientSideActions: any[], ) { - processMessages(this.waMonitor.waInstances[instance.instanceName], messages, input, clientSideActions).catch( - (err) => { - console.error('Erro ao processar mensagens:', err); - }, - ); + processMessages( + this.waMonitor.waInstances[instance.instanceName], + messages, + input, + clientSideActions, + this.eventEmitter, + ).catch((err) => { + console.error('Erro ao processar mensagens:', err); + }); function findItemAndGetSecondsToWait(array, targetId) { if (!array) return null; @@ -373,7 +401,7 @@ export class TypebotService { return null; } - async function processMessages(instance, messages, input, clientSideActions) { + async function processMessages(instance, messages, input, clientSideActions, eventEmitter) { for (const message of messages) { const wait = findItemAndGetSecondsToWait(clientSideActions, message.id); @@ -383,31 +411,50 @@ export class TypebotService { let linkPreview = false; for (const richText of message.content.richText) { - for (const element of richText.children) { - let text = ''; - if (element.text) { - text = element.text; + if (richText.type === 'variable') { + for (const child of richText.children) { + for (const grandChild of child.children) { + formattedText += grandChild.text; + } } + } else { + for (const element of richText.children) { + let text = ''; - if (element.bold) { - text = `*${text}*`; + if (element.type === 'inline-variable') { + for (const child of element.children) { + for (const grandChild of child.children) { + text += grandChild.text; + } + } + } else if (element.text) { + text = element.text; + } + + // if (element.text) { + // text = element.text; + // } + + if (element.bold) { + text = `*${text}*`; + } + + if (element.italic) { + text = `_${text}_`; + } + + if (element.underline) { + text = `*${text}*`; + } + + if (element.url) { + const linkText = element.children[0].text; + text = `[${linkText}](${element.url})`; + linkPreview = true; + } + + formattedText += text; } - - if (element.italic) { - text = `_${text}_`; - } - - if (element.underline) { - text = `*${text}*`; - } - - if (element.url) { - const linkText = element.children[0].text; - text = `[${linkText}](${element.url})`; - linkPreview = true; - } - - formattedText += text; } formattedText += '\n'; } @@ -494,6 +541,11 @@ export class TypebotService { }, }); } + } else { + eventEmitter.emit('typebot:end', { + instance: instance, + remoteJid: remoteJid, + }); } } } diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 618aa65b..705fa594 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -168,7 +168,7 @@ export class WAStartupService { private chatwootService = new ChatwootService(waMonitor, this.configService, this.repository); - private typebotService = new TypebotService(waMonitor, this.configService); + private typebotService = new TypebotService(waMonitor, this.configService, this.eventEmitter); private chamaaiService = new ChamaaiService(waMonitor, this.configService); diff --git a/src/whatsapp/whatsapp.module.ts b/src/whatsapp/whatsapp.module.ts index 1fb84de6..e89d4f56 100644 --- a/src/whatsapp/whatsapp.module.ts +++ b/src/whatsapp/whatsapp.module.ts @@ -101,7 +101,7 @@ export const waMonitor = new WAMonitoringService(eventEmitter, configService, re const authService = new AuthService(configService, waMonitor, repository); -const typebotService = new TypebotService(waMonitor, configService); +const typebotService = new TypebotService(waMonitor, configService, eventEmitter); export const typebotController = new TypebotController(typebotService);