Skip to content

Commit

Permalink
feat: typebot send buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Oct 29, 2024
1 parent fbccf2e commit 65a9c78
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 55 deletions.
92 changes: 45 additions & 47 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ import P from 'pino';
import qrcode, { QRCodeToDataURLOptions } from 'qrcode';
import qrcodeTerminal from 'qrcode-terminal';
import sharp from 'sharp';
import { PassThrough } from 'stream';
import { PassThrough, Readable } from 'stream';
import { v4 } from 'uuid';
import { Readable } from 'stream';

const groupMetadataCache = new CacheService(new CacheEngine(configService, 'groups').getEngine());

Expand Down Expand Up @@ -372,7 +371,7 @@ export class BaileysStartupService extends ChannelStartupService {
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
this.logger.log(
`\n{ instance: ${this.instance.name} pairingCode: ${this.instance.qrcode.pairingCode}, qrcodeCount: ${this.instance.qrcode.count} }\n` +
qrcode,
qrcode,
),
);

Expand Down Expand Up @@ -976,18 +975,18 @@ export class BaileysStartupService extends ChannelStartupService {

const messagesRepository = new Set(
chatwootImport.getRepositoryMessagesCache(instance) ??
(
await this.prismaRepository.message.findMany({
select: { key: true },
where: { instanceId: this.instanceId },
})
).map((message) => {
const key = message.key as {
id: string;
};

return key.id;
}),
(
await this.prismaRepository.message.findMany({
select: { key: true },
where: { instanceId: this.instanceId },
})
).map((message) => {
const key = message.key as {
id: string;
};

return key.id;
}),
);

if (chatwootImport.getRepositoryMessagesCache(instance) === null) {
Expand Down Expand Up @@ -1138,7 +1137,7 @@ export class BaileysStartupService extends ChannelStartupService {
where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid },
});

if (!!existingChat) {
if (existingChat) {
const chatToInsert = {
remoteJid: received.key.remoteJid,
instanceId: this.instanceId,
Expand Down Expand Up @@ -1474,7 +1473,7 @@ export class BaileysStartupService extends ChannelStartupService {
where: { instanceId: this.instanceId, remoteJid: message.remoteJid },
});

if (!!existingChat) {
if (existingChat) {
const chatToInsert = {
remoteJid: message.remoteJid,
instanceId: this.instanceId,
Expand Down Expand Up @@ -2507,9 +2506,7 @@ export class BaileysStartupService extends ChannelStartupService {

const prepareMedia = await prepareWAMessageMedia(
{
[type]: isURL(mediaMessage.media)
? { url: mediaMessage.media }
: Buffer.from(mediaMessage.media, 'base64'),
[type]: isURL(mediaMessage.media) ? { url: mediaMessage.media } : Buffer.from(mediaMessage.media, 'base64'),
} as any,
{ upload: this.client.waUploadToServer },
);
Expand Down Expand Up @@ -2564,7 +2561,7 @@ export class BaileysStartupService extends ChannelStartupService {
if (mediaMessage.mediatype === 'ptv') {
prepareMedia[mediaType] = prepareMedia[type + 'Message'];
mimetype = 'video/mp4';

if (!prepareMedia[mediaType]) {
throw new Error('Failed to prepare video message');
}
Expand All @@ -2588,7 +2585,6 @@ export class BaileysStartupService extends ChannelStartupService {

this.logger.verbose(`Video duration: ${duration} seconds`);
prepareMedia[mediaType].seconds = duration;

} catch (error) {
this.logger.error('Error getting video duration:');
this.logger.error(error);
Expand Down Expand Up @@ -2954,43 +2950,43 @@ export class BaileysStartupService extends ChannelStartupService {
currency: button.currency,
total_amount: {
value: 0,
offset: 100
offset: 100,
},
reference_id: this.generateRandomId(),
type: "physical-goods",
type: 'physical-goods',
order: {
status: "pending",
status: 'pending',
subtotal: {
value: 0,
offset: 100
offset: 100,
},
order_type: "ORDER",
order_type: 'ORDER',
items: [
{
name: "",
name: '',
amount: {
value: 0,
offset: 100
offset: 100,
},
quantity: 0,
sale_amount: {
value: 0,
offset: 100
}
}
]
offset: 100,
},
},
],
},
payment_settings: [
{
type: "pix_static_code",
type: 'pix_static_code',
pix_static_code: {
merchant_name: button.name,
key: button.key,
key_type: this.mapKeyType.get(button.keyType)
}
}
key_type: this.mapKeyType.get(button.keyType),
},
},
],
share_payment_status: false
share_payment_status: false,
}),
};

Expand Down Expand Up @@ -3018,11 +3014,11 @@ export class BaileysStartupService extends ChannelStartupService {
throw new BadRequestException('At least one button is required');
}

const hasReplyButtons = data.buttons.some(btn => btn.type === 'reply');
const hasPixButton = data.buttons.some(btn => btn.type === 'pix');
const hasOtherButtons = data.buttons.some(btn => btn.type !== 'reply' && btn.type !== 'pix');
const hasReplyButtons = data.buttons.some((btn) => btn.type === 'reply');

const hasPixButton = data.buttons.some((btn) => btn.type === 'pix');

const hasOtherButtons = data.buttons.some((btn) => btn.type !== 'reply' && btn.type !== 'pix');

if (hasReplyButtons) {
if (data.buttons.length > 3) {
Expand All @@ -3046,10 +3042,12 @@ export class BaileysStartupService extends ChannelStartupService {
message: {
interactiveMessage: {
nativeFlowMessage: {
buttons: [{
name: this.mapType.get('pix'),
buttonParamsJson: this.toJSONString(data.buttons[0]),
}],
buttons: [
{
name: this.mapType.get('pix'),
buttonParamsJson: this.toJSONString(data.buttons[0]),
},
],
messageParamsJson: JSON.stringify({
from: 'api',
templateId: v4(),
Expand Down
88 changes: 80 additions & 8 deletions src/api/integrations/chatbot/typebot/services/typebot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,88 @@ export class TypebotService {

formattedText = formattedText.replace(/\n$/, '');

await instance.textMessage(
{
if (formattedText.includes('[buttons]')) {
const buttonJson = {
number: remoteJid.split('@')[0],
delay: settings?.delayMessage || 1000,
text: formattedText,
},
false,
);
thumbnailUrl: undefined,
title: '',
description: '',
footer: '',
buttons: [],
};

sendTelemetry('/message/sendText');
const thumbnailUrlMatch = formattedText.match(/\[thumbnailUrl\]([\s\S]*?)(?=\[title\])/);
const titleMatch = formattedText.match(/\[title\]([\s\S]*?)(?=\[description\])/);
const descriptionMatch = formattedText.match(/\[description\]([\s\S]*?)(?=\[footer\])/);
const footerMatch = formattedText.match(/\[footer\]([\s\S]*?)(?=\[(?:reply|pix|copy|call|url))/);

if (titleMatch) buttonJson.title = titleMatch[1].trim();
if (thumbnailUrlMatch) buttonJson.thumbnailUrl = thumbnailUrlMatch[1].trim();
if (descriptionMatch) buttonJson.description = descriptionMatch[1].trim();
if (footerMatch) buttonJson.footer = footerMatch[1].trim();

const buttonTypes = {
reply: /\[reply\]([\s\S]*?)(?=\[(?:reply|pix|copy|call|url)|$)/g,
pix: /\[pix\]([\s\S]*?)(?=\[(?:reply|pix|copy|call|url)|$)/g,
copy: /\[copy\]([\s\S]*?)(?=\[(?:reply|pix|copy|call|url)|$)/g,
call: /\[call\]([\s\S]*?)(?=\[(?:reply|pix|copy|call|url)|$)/g,
url: /\[url\]([\s\S]*?)(?=\[(?:reply|pix|copy|call|url)|$)/g,
};

for (const [type, pattern] of Object.entries(buttonTypes)) {
let match;
while ((match = pattern.exec(formattedText)) !== null) {
const content = match[1].trim();
const button: any = { type };

switch (type) {
case 'pix':
button.currency = content.match(/currency: (.*?)(?:\n|$)/)?.[1]?.trim();
button.name = content.match(/name: (.*?)(?:\n|$)/)?.[1]?.trim();
button.keyType = content.match(/keyType: (.*?)(?:\n|$)/)?.[1]?.trim();
button.key = content.match(/key: (.*?)(?:\n|$)/)?.[1]?.trim();
break;

case 'reply':
button.displayText = content.match(/displayText: (.*?)(?:\n|$)/)?.[1]?.trim();
button.id = content.match(/id: (.*?)(?:\n|$)/)?.[1]?.trim();
break;

case 'copy':
button.displayText = content.match(/displayText: (.*?)(?:\n|$)/)?.[1]?.trim();
button.copyCode = content.match(/copyCode: (.*?)(?:\n|$)/)?.[1]?.trim();
break;

case 'call':
button.displayText = content.match(/displayText: (.*?)(?:\n|$)/)?.[1]?.trim();
button.phoneNumber = content.match(/phone: (.*?)(?:\n|$)/)?.[1]?.trim();
break;

case 'url':
button.displayText = content.match(/displayText: (.*?)(?:\n|$)/)?.[1]?.trim();
button.url = content.match(/url: (.*?)(?:\n|$)/)?.[1]?.trim();
break;
}

if (Object.keys(button).length > 1) {
buttonJson.buttons.push(button);
}
}
}

await instance.buttonMessage(buttonJson);
} else {
await instance.textMessage(
{
number: remoteJid.split('@')[0],
delay: settings?.delayMessage || 1000,
text: formattedText,
},
false,
);

sendTelemetry('/message/sendText');
}
}

if (message.type === 'image') {
Expand Down

0 comments on commit 65a9c78

Please sign in to comment.