Skip to content

Commit a446461

Browse files
committed
fix: variables in typebot
1 parent 87027ea commit a446461

9 files changed

Lines changed: 144 additions & 89 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
### Fixed
44

55
* Fixed Lid Messages
6+
* Fixed sending variables to typebot
7+
* Fixed sending variables from typebot
68

79
# 1.6.0 (2023-12-12 17:24)
810

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM node:20.7.0-alpine
22

3-
LABEL version="1.6.0" description="Api to control whatsapp features through http requests."
3+
LABEL version="1.6.1" description="Api to control whatsapp features through http requests."
44
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
55
LABEL contact="contato@agenciadgcode.com"
66

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "evolution-api",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/src/main.js",
66
"scripts": {
@@ -56,7 +56,7 @@
5656
"cross-env": "^7.0.3",
5757
"dayjs": "^1.11.7",
5858
"eventemitter2": "^6.4.9",
59-
"evolution-manager": "^0.4.5",
59+
"evolution-manager": "^0.4.6",
6060
"exiftool-vendored": "^22.0.0",
6161
"express": "^4.18.2",
6262
"express-async-errors": "^3.1.1",

src/config/env.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ export class ConfigService {
169169
this.env = !(process.env?.DOCKER_ENV === 'true') ? this.envYaml() : this.envProcess();
170170
this.env.PRODUCTION = process.env?.NODE_ENV === 'PROD';
171171
if (process.env?.DOCKER_ENV === 'true') {
172-
this.env.SERVER.TYPE = 'http';
173-
this.env.SERVER.PORT = 8080;
172+
this.env.SERVER.TYPE = process.env.SERVER_TYPE as 'http' | 'http';
173+
this.env.SERVER.PORT = Number.parseInt(process.env.SERVER_PORT) || 8080;
174174
}
175175
}
176176

src/docs/swagger.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ info:
2525
</font>
2626
2727
[![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)
28-
version: 1.5.5
28+
version: 1.6.1
2929
contact:
3030
name: DavidsonGomes
3131
email: contato@agenciadgcode.com

src/whatsapp/routers/index.router.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ router
4141
status: HttpStatus.OK,
4242
message: 'Welcome to the Evolution API, it is working!',
4343
version: packageJson.version,
44-
documentation: `${req.protocol}://${req.get('host')}/docs`,
44+
swagger: `${req.protocol}://${req.get('host')}/docs`,
45+
documentation: `https://doc.evolution-api.com`,
4546
manager: `${req.protocol}://${req.get('host')}/manager`,
4647
});
4748
})

src/whatsapp/services/typebot.service.ts

Lines changed: 132 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import EventEmitter2 from 'eventemitter2';
23

34
import { ConfigService, Typebot } from '../../config/env.config';
45
import { Logger } from '../../config/logger.config';
@@ -9,7 +10,15 @@ import { Events } from '../types/wa.types';
910
import { WAMonitoringService } from './monitor.service';
1011

1112
export class TypebotService {
12-
constructor(private readonly waMonitor: WAMonitoringService, private readonly configService: ConfigService) {}
13+
constructor(
14+
private readonly waMonitor: WAMonitoringService,
15+
private readonly configService: ConfigService,
16+
private readonly eventEmitter: EventEmitter2,
17+
) {
18+
this.eventEmitter.on('typebot:end', async (data) => {
19+
await this.clearSessions(data.instance, data.remoteJid);
20+
});
21+
}
1322

1423
private readonly logger = new Logger(TypebotService.name);
1524

@@ -110,6 +119,37 @@ export class TypebotService {
110119
return { typebot: { ...instance, typebot: typebotData } };
111120
}
112121

122+
public async clearSessions(instance: InstanceDto, remoteJid: string) {
123+
const findTypebot = await this.find(instance);
124+
const sessions = (findTypebot.sessions as Session[]) ?? [];
125+
126+
const sessionWithRemoteJid = sessions.filter((session) => session.remoteJid === remoteJid);
127+
128+
if (sessionWithRemoteJid.length > 0) {
129+
sessionWithRemoteJid.forEach((session) => {
130+
sessions.splice(sessions.indexOf(session), 1);
131+
});
132+
133+
const typebotData = {
134+
enabled: findTypebot.enabled,
135+
url: findTypebot.url,
136+
typebot: findTypebot.typebot,
137+
expire: findTypebot.expire,
138+
keyword_finish: findTypebot.keyword_finish,
139+
delay_message: findTypebot.delay_message,
140+
unknown_message: findTypebot.unknown_message,
141+
listening_from_me: findTypebot.listening_from_me,
142+
sessions,
143+
};
144+
145+
this.create(instance, typebotData);
146+
147+
return sessions;
148+
}
149+
150+
return sessions;
151+
}
152+
113153
public async startTypebot(instance: InstanceDto, data: any) {
114154
if (data.remoteJid === 'status@broadcast') return;
115155

@@ -169,20 +209,25 @@ export class TypebotService {
169209
} else {
170210
const id = Math.floor(Math.random() * 10000000000).toString();
171211

172-
const reqData = {
173-
startParams: {
174-
publicId: data.typebot,
175-
prefilledVariables: prefilledVariables,
176-
},
177-
};
178-
179212
try {
180213
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
181214
let url: string;
215+
let reqData: {};
182216
if (version === 'latest') {
183217
url = `${data.url}/api/v1/typebots/${data.typebot}/startChat`;
218+
219+
reqData = {
220+
prefilledVariables: prefilledVariables,
221+
};
184222
} else {
185223
url = `${data.url}/api/v1/sendMessage`;
224+
225+
reqData = {
226+
startParams: {
227+
publicId: data.typebot,
228+
prefilledVariables: prefilledVariables,
229+
},
230+
};
186231
}
187232
const request = await axios.post(url, reqData);
188233

@@ -260,25 +305,35 @@ export class TypebotService {
260305
if (data.remoteJid === 'status@broadcast') return;
261306
const id = Math.floor(Math.random() * 10000000000).toString();
262307

263-
const reqData = {
264-
startParams: {
265-
publicId: data.typebot,
266-
prefilledVariables: {
267-
...data.prefilledVariables,
268-
remoteJid: data.remoteJid,
269-
pushName: data.pushName || data.prefilledVariables?.pushName || '',
270-
instanceName: instance.instanceName,
271-
},
272-
},
273-
};
274-
275308
try {
276309
const version = this.configService.get<Typebot>('TYPEBOT').API_VERSION;
277310
let url: string;
311+
let reqData: {};
278312
if (version === 'latest') {
279313
url = `${data.url}/api/v1/typebots/${data.typebot}/startChat`;
314+
315+
reqData = {
316+
prefilledVariables: {
317+
...data.prefilledVariables,
318+
remoteJid: data.remoteJid,
319+
pushName: data.pushName || data.prefilledVariables?.pushName || '',
320+
instanceName: instance.instanceName,
321+
},
322+
};
280323
} else {
281324
url = `${data.url}/api/v1/sendMessage`;
325+
326+
reqData = {
327+
startParams: {
328+
publicId: data.typebot,
329+
prefilledVariables: {
330+
...data.prefilledVariables,
331+
remoteJid: data.remoteJid,
332+
pushName: data.pushName || data.prefilledVariables?.pushName || '',
333+
instanceName: instance.instanceName,
334+
},
335+
},
336+
};
282337
}
283338
const request = await axios.post(url, reqData);
284339

@@ -318,49 +373,22 @@ export class TypebotService {
318373
}
319374
}
320375

321-
public async clearSessions(instance: InstanceDto, remoteJid: string) {
322-
const findTypebot = await this.find(instance);
323-
const sessions = (findTypebot.sessions as Session[]) ?? [];
324-
325-
const sessionWithRemoteJid = sessions.filter((session) => session.remoteJid === remoteJid);
326-
327-
if (sessionWithRemoteJid.length > 0) {
328-
sessionWithRemoteJid.forEach((session) => {
329-
sessions.splice(sessions.indexOf(session), 1);
330-
});
331-
332-
const typebotData = {
333-
enabled: findTypebot.enabled,
334-
url: findTypebot.url,
335-
typebot: findTypebot.typebot,
336-
expire: findTypebot.expire,
337-
keyword_finish: findTypebot.keyword_finish,
338-
delay_message: findTypebot.delay_message,
339-
unknown_message: findTypebot.unknown_message,
340-
listening_from_me: findTypebot.listening_from_me,
341-
sessions,
342-
};
343-
344-
this.create(instance, typebotData);
345-
346-
return sessions;
347-
}
348-
349-
return sessions;
350-
}
351-
352376
public async sendWAMessage(
353377
instance: InstanceDto,
354378
remoteJid: string,
355379
messages: any[],
356380
input: any[],
357381
clientSideActions: any[],
358382
) {
359-
processMessages(this.waMonitor.waInstances[instance.instanceName], messages, input, clientSideActions).catch(
360-
(err) => {
361-
console.error('Erro ao processar mensagens:', err);
362-
},
363-
);
383+
processMessages(
384+
this.waMonitor.waInstances[instance.instanceName],
385+
messages,
386+
input,
387+
clientSideActions,
388+
this.eventEmitter,
389+
).catch((err) => {
390+
console.error('Erro ao processar mensagens:', err);
391+
});
364392

365393
function findItemAndGetSecondsToWait(array, targetId) {
366394
if (!array) return null;
@@ -373,7 +401,7 @@ export class TypebotService {
373401
return null;
374402
}
375403

376-
async function processMessages(instance, messages, input, clientSideActions) {
404+
async function processMessages(instance, messages, input, clientSideActions, eventEmitter) {
377405
for (const message of messages) {
378406
const wait = findItemAndGetSecondsToWait(clientSideActions, message.id);
379407

@@ -383,31 +411,50 @@ export class TypebotService {
383411
let linkPreview = false;
384412

385413
for (const richText of message.content.richText) {
386-
for (const element of richText.children) {
387-
let text = '';
388-
if (element.text) {
389-
text = element.text;
390-
}
391-
392-
if (element.bold) {
393-
text = `*${text}*`;
394-
}
395-
396-
if (element.italic) {
397-
text = `_${text}_`;
398-
}
399-
400-
if (element.underline) {
401-
text = `*${text}*`;
414+
if (richText.type === 'variable') {
415+
for (const child of richText.children) {
416+
for (const grandChild of child.children) {
417+
formattedText += grandChild.text;
418+
}
402419
}
403-
404-
if (element.url) {
405-
const linkText = element.children[0].text;
406-
text = `[${linkText}](${element.url})`;
407-
linkPreview = true;
420+
} else {
421+
for (const element of richText.children) {
422+
let text = '';
423+
424+
if (element.type === 'inline-variable') {
425+
for (const child of element.children) {
426+
for (const grandChild of child.children) {
427+
text += grandChild.text;
428+
}
429+
}
430+
} else if (element.text) {
431+
text = element.text;
432+
}
433+
434+
// if (element.text) {
435+
// text = element.text;
436+
// }
437+
438+
if (element.bold) {
439+
text = `*${text}*`;
440+
}
441+
442+
if (element.italic) {
443+
text = `_${text}_`;
444+
}
445+
446+
if (element.underline) {
447+
text = `*${text}*`;
448+
}
449+
450+
if (element.url) {
451+
const linkText = element.children[0].text;
452+
text = `[${linkText}](${element.url})`;
453+
linkPreview = true;
454+
}
455+
456+
formattedText += text;
408457
}
409-
410-
formattedText += text;
411458
}
412459
formattedText += '\n';
413460
}
@@ -494,6 +541,11 @@ export class TypebotService {
494541
},
495542
});
496543
}
544+
} else {
545+
eventEmitter.emit('typebot:end', {
546+
instance: instance,
547+
remoteJid: remoteJid,
548+
});
497549
}
498550
}
499551
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class WAStartupService {
168168

169169
private chatwootService = new ChatwootService(waMonitor, this.configService, this.repository);
170170

171-
private typebotService = new TypebotService(waMonitor, this.configService);
171+
private typebotService = new TypebotService(waMonitor, this.configService, this.eventEmitter);
172172

173173
private chamaaiService = new ChamaaiService(waMonitor, this.configService);
174174

src/whatsapp/whatsapp.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const waMonitor = new WAMonitoringService(eventEmitter, configService, re
101101

102102
const authService = new AuthService(configService, waMonitor, repository);
103103

104-
const typebotService = new TypebotService(waMonitor, configService);
104+
const typebotService = new TypebotService(waMonitor, configService, eventEmitter);
105105

106106
export const typebotController = new TypebotController(typebotService);
107107

0 commit comments

Comments
 (0)