Skip to content

Commit 6f99784

Browse files
committed
Added Typebot integration
1 parent 201e6f7 commit 6f99784

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ function bootstrap() {
2727
cors({
2828
origin(requestOrigin, callback) {
2929
const { ORIGIN } = configService.get<Cors>('CORS');
30-
!requestOrigin ? (requestOrigin = '*') : undefined;
30+
if (ORIGIN.includes('*')) {
31+
return callback(null, true);
32+
}
3133
if (ORIGIN.indexOf(requestOrigin) !== -1) {
3234
return callback(null, true);
3335
}

src/whatsapp/services/typebot.service.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { delay } from '@whiskeysockets/baileys';
12
import axios from 'axios';
23

34
import { Logger } from '../../config/logger.config';
@@ -162,13 +163,34 @@ export class TypebotService {
162163
return request.data;
163164
}
164165

165-
public async sendWAMessage(instance: InstanceDto, remoteJid: string, messages: any[], input: any[]) {
166-
processMessages(this.waMonitor.waInstances[instance.instanceName], messages, input).catch((err) => {
167-
console.error('Erro ao processar mensagens:', err);
168-
});
166+
public async sendWAMessage(
167+
instance: InstanceDto,
168+
remoteJid: string,
169+
messages: any[],
170+
input: any[],
171+
clientSideActions: any[],
172+
) {
173+
processMessages(this.waMonitor.waInstances[instance.instanceName], messages, input, clientSideActions).catch(
174+
(err) => {
175+
console.error('Erro ao processar mensagens:', err);
176+
},
177+
);
178+
179+
function findItemAndGetSecondsToWait(array, targetId) {
180+
if (!array) return null;
181+
182+
for (const item of array) {
183+
if (item.lastBubbleBlockId === targetId) {
184+
return item.wait?.secondsToWaitFor;
185+
}
186+
}
187+
return null;
188+
}
169189

170-
async function processMessages(instance, messages, input) {
190+
async function processMessages(instance, messages, input, clientSideActions) {
171191
for (const message of messages) {
192+
const wait = findItemAndGetSecondsToWait(clientSideActions, message.id);
193+
172194
if (message.type === 'text') {
173195
let formattedText = '';
174196

@@ -260,6 +282,10 @@ export class TypebotService {
260282
},
261283
});
262284
}
285+
286+
if (wait) {
287+
await delay(wait * 1000);
288+
}
263289
}
264290

265291
if (input) {
@@ -323,7 +349,7 @@ export class TypebotService {
323349
pushName: msg.pushName,
324350
});
325351

326-
await this.sendWAMessage(instance, remoteJid, data.messages, data.input);
352+
await this.sendWAMessage(instance, remoteJid, data.messages, data.input, data.clientSideActions);
327353

328354
return;
329355
}
@@ -346,7 +372,7 @@ export class TypebotService {
346372
pushName: msg.pushName,
347373
});
348374

349-
await this.sendWAMessage(instance, remoteJid, data.messages, data.input);
375+
await this.sendWAMessage(instance, remoteJid, data.messages, data.input, data.clientSideActions);
350376

351377
return;
352378
}
@@ -414,7 +440,13 @@ export class TypebotService {
414440

415441
const request = await axios.post(url + '/api/v1/sendMessage', reqData);
416442

417-
await this.sendWAMessage(instance, remoteJid, request.data.messages, request.data.input);
443+
await this.sendWAMessage(
444+
instance,
445+
remoteJid,
446+
request.data.messages,
447+
request.data.input,
448+
request.data.clientSideActions,
449+
);
418450

419451
return;
420452
}

0 commit comments

Comments
 (0)