Skip to content

Commit 5283fdc

Browse files
Merge pull request #2382 from webmastersites/fix/interactive-devicesent
Fix/interactive devicesent
2 parents db60f04 + a4f8e95 commit 5283fdc

File tree

1 file changed

+63
-31
lines changed

1 file changed

+63
-31
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,30 +3595,34 @@ export class BaileysStartupService extends ChannelStartupService {
35953595
]);
35963596

35973597
public async buttonMessage(data: SendButtonsDto) {
3598-
if (data.buttons.length === 0) {
3598+
if (!data.buttons || data.buttons.length === 0) {
35993599
throw new BadRequestException('At least one button is required');
36003600
}
36013601

36023602
const hasReplyButtons = data.buttons.some((btn) => btn.type === 'reply');
3603-
36043603
const hasPixButton = data.buttons.some((btn) => btn.type === 'pix');
3604+
const hasCTAButtons = data.buttons.some((btn) => btn.type === 'url' || btn.type === 'call' || btn.type === 'copy');
36053605

3606-
const hasOtherButtons = data.buttons.some((btn) => btn.type !== 'reply' && btn.type !== 'pix');
3606+
/* =========================
3607+
* REGRAS DE VALIDAÇÃO
3608+
* ========================= */
36073609

3610+
// Reply
36083611
if (hasReplyButtons) {
36093612
if (data.buttons.length > 3) {
36103613
throw new BadRequestException('Maximum of 3 reply buttons allowed');
36113614
}
3612-
if (hasOtherButtons) {
3613-
throw new BadRequestException('Reply buttons cannot be mixed with other button types');
3615+
if (hasCTAButtons || hasPixButton) {
3616+
throw new BadRequestException('Reply buttons cannot be mixed with CTA or PIX buttons');
36143617
}
36153618
}
36163619

3620+
// PIX
36173621
if (hasPixButton) {
36183622
if (data.buttons.length > 1) {
36193623
throw new BadRequestException('Only one PIX button is allowed');
36203624
}
3621-
if (hasOtherButtons) {
3625+
if (hasReplyButtons || hasCTAButtons) {
36223626
throw new BadRequestException('PIX button cannot be mixed with other button types');
36233627
}
36243628

@@ -3627,8 +3631,16 @@ export class BaileysStartupService extends ChannelStartupService {
36273631
message: {
36283632
interactiveMessage: {
36293633
nativeFlowMessage: {
3630-
buttons: [{ name: this.mapType.get('pix'), buttonParamsJson: this.toJSONString(data.buttons[0]) }],
3631-
messageParamsJson: JSON.stringify({ from: 'api', templateId: v4() }),
3634+
buttons: [
3635+
{
3636+
name: this.mapType.get('pix'),
3637+
buttonParamsJson: this.toJSONString(data.buttons[0]),
3638+
},
3639+
],
3640+
messageParamsJson: JSON.stringify({
3641+
from: 'api',
3642+
templateId: v4(),
3643+
}),
36323644
},
36333645
},
36343646
},
@@ -3644,43 +3656,63 @@ export class BaileysStartupService extends ChannelStartupService {
36443656
});
36453657
}
36463658

3647-
const generate = await (async () => {
3648-
if (data?.thumbnailUrl) {
3649-
return await this.prepareMediaMessage({ mediatype: 'image', media: data.thumbnailUrl });
3659+
// CTA (url / call / copy)
3660+
if (hasCTAButtons) {
3661+
if (data.buttons.length > 2) {
3662+
throw new BadRequestException('Maximum of 2 CTA buttons allowed');
3663+
}
3664+
if (hasReplyButtons) {
3665+
throw new BadRequestException('CTA buttons cannot be mixed with reply buttons');
36503666
}
3651-
})();
3667+
}
36523668

3653-
const buttons = data.buttons.map((value) => {
3654-
return { name: this.mapType.get(value.type), buttonParamsJson: this.toJSONString(value) };
3655-
});
3669+
/* =========================
3670+
* HEADER (opcional)
3671+
* ========================= */
3672+
3673+
const generatedMedia = data?.thumbnailUrl
3674+
? await this.prepareMediaMessage({ mediatype: 'image', media: data.thumbnailUrl })
3675+
: null;
3676+
3677+
/* =========================
3678+
* BOTÕES
3679+
* ========================= */
3680+
3681+
const buttons = data.buttons.map((btn) => ({
3682+
name: this.mapType.get(btn.type),
3683+
buttonParamsJson: this.toJSONString(btn),
3684+
}));
3685+
3686+
/* =========================
3687+
* MENSAGEM FINAL
3688+
* ========================= */
36563689

36573690
const message: proto.IMessage = {
36583691
viewOnceMessage: {
36593692
message: {
36603693
interactiveMessage: {
36613694
body: {
36623695
text: (() => {
3663-
let t = '*' + data.title + '*';
3696+
let text = `*${data.title}*`;
36643697
if (data?.description) {
3665-
t += '\n\n';
3666-
t += data.description;
3667-
t += '\n';
3698+
text += `\n\n${data.description}`;
36683699
}
3669-
return t;
3700+
return text;
36703701
})(),
36713702
},
3672-
footer: { text: data?.footer },
3673-
header: (() => {
3674-
if (generate?.message?.imageMessage) {
3675-
return {
3676-
hasMediaAttachment: !!generate.message.imageMessage,
3677-
imageMessage: generate.message.imageMessage,
3678-
};
3679-
}
3680-
})(),
3703+
footer: data?.footer ? { text: data.footer } : undefined,
3704+
header: generatedMedia?.message?.imageMessage
3705+
? {
3706+
hasMediaAttachment: true,
3707+
imageMessage: generatedMedia.message.imageMessage,
3708+
}
3709+
: undefined,
36813710
nativeFlowMessage: {
3682-
buttons: buttons,
3683-
messageParamsJson: JSON.stringify({ from: 'api', templateId: v4() }),
3711+
buttons,
3712+
messageParamsJson: JSON.stringify({
3713+
from: 'api',
3714+
templateId: v4(),
3715+
}),
36843716
},
36853717
},
36863718
},

0 commit comments

Comments
 (0)