Skip to content

Commit 469e696

Browse files
committed
Added Typebot integration
1 parent d99ccd9 commit 469e696

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

src/whatsapp/services/typebot.service.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ export class TypebotService {
113113
return request.data;
114114
}
115115

116-
public async sendWAMessage(instance: InstanceDto, remoteJid: string, messages: any[], closeSession?: boolean) {
117-
processMessages(this.waMonitor.waInstances[instance.instanceName], messages)
116+
public async sendWAMessage(instance: InstanceDto, remoteJid: string, messages: any[], input: any[]) {
117+
processMessages(this.waMonitor.waInstances[instance.instanceName], messages, input)
118118
.then(async () => {
119-
if (closeSession) {
119+
if (!input) {
120120
const typebotData = await this.find(instance);
121121

122122
const session = typebotData.sessions.find((session) => session.remoteJid === remoteJid);
@@ -132,11 +132,13 @@ export class TypebotService {
132132
console.error('Erro ao processar mensagens:', err);
133133
});
134134

135-
async function processMessages(instance, messages) {
135+
async function processMessages(instance, messages, input) {
136136
for (const message of messages) {
137137
if (message.type === 'text') {
138138
let formattedText = '';
139139

140+
let linkPreview = false;
141+
140142
for (const richText of message.content.richText) {
141143
for (const element of richText.children) {
142144
let text = '';
@@ -159,6 +161,7 @@ export class TypebotService {
159161
if (element.url) {
160162
const linkText = element.children[0].text;
161163
text = `[${linkText}](${element.url})`;
164+
linkPreview = true;
162165
}
163166

164167
formattedText += text;
@@ -173,7 +176,7 @@ export class TypebotService {
173176
options: {
174177
delay: 1200,
175178
presence: 'composing',
176-
linkPreview: false,
179+
linkPreview: linkPreview,
177180
},
178181
textMessage: {
179182
text: formattedText,
@@ -223,6 +226,32 @@ export class TypebotService {
223226
});
224227
}
225228
}
229+
230+
if (input) {
231+
if (input.type === 'choice input') {
232+
let formattedText = '';
233+
234+
const items = input.items;
235+
236+
for (const item of items) {
237+
formattedText += `▶️ ${item.content}\n`;
238+
}
239+
240+
formattedText = formattedText.replace(/\n$/, '');
241+
242+
await instance.textMessage({
243+
number: remoteJid.split('@')[0],
244+
options: {
245+
delay: 1200,
246+
presence: 'composing',
247+
linkPreview: false,
248+
},
249+
textMessage: {
250+
text: formattedText,
251+
},
252+
});
253+
}
254+
}
226255
}
227256
}
228257

@@ -252,7 +281,7 @@ export class TypebotService {
252281
remoteJid: remoteJid,
253282
});
254283

255-
await this.sendWAMessage(instance, remoteJid, data.messages);
284+
await this.sendWAMessage(instance, remoteJid, data.messages, data.input);
256285

257286
return;
258287
}
@@ -267,7 +296,7 @@ export class TypebotService {
267296
remoteJid: remoteJid,
268297
});
269298

270-
await this.sendWAMessage(instance, remoteJid, data.messages);
299+
await this.sendWAMessage(instance, remoteJid, data.messages, data.input);
271300

272301
return;
273302
}
@@ -329,7 +358,7 @@ export class TypebotService {
329358
});
330359
}
331360

332-
await this.sendWAMessage(instance, remoteJid, request.data.messages, !request.data.input);
361+
await this.sendWAMessage(instance, remoteJid, request.data.messages, request.data.input);
333362

334363
return;
335364
}

0 commit comments

Comments
 (0)