Skip to content

Fix: Support media extraction from templateMessage in getBase64FromMediaMessage #1715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3437,16 +3437,34 @@ export class BaileysStartupService extends ChannelStartupService {
let mediaMessage: any;
let mediaType: string;

for (const type of TypeMediaMessage) {
mediaMessage = msg.message[type];
if (mediaMessage) {
mediaType = type;
break;
if (msg.message?.templateMessage) {
const template =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Potential edge case if both hydratedTemplate and hydratedFourRowTemplate are undefined.

Accessing template[type] without verifying that template is defined may cause a runtime error. Please add a check to ensure template is defined before using it.

msg.message.templateMessage.hydratedTemplate || msg.message.templateMessage.hydratedFourRowTemplate;

for (const type of TypeMediaMessage) {
if (template[type]) {
mediaMessage = template[type];
mediaType = type;
msg.message = { [type]: { ...template[type], url: template[type].staticUrl } };
break;
}
}

if (!mediaMessage) {
throw 'Template message does not contain a supported media type';
}
} else {
for (const type of TypeMediaMessage) {
mediaMessage = msg.message[type];
if (mediaMessage) {
mediaType = type;
break;
}
}
}

if (!mediaMessage) {
throw 'The message is not of the media type';
if (!mediaMessage) {
throw 'The message is not of the media type';
}
}

if (typeof mediaMessage['mediaKey'] === 'object') {
Expand Down