WhiskeySockets / Baileys

Lightweight full-featured typescript/javascript WhatsApp Web API
https://baileys.whiskeysockets.io/
MIT License
4.1k stars 1.38k forks source link

[BUG] help with buttons #1105

Closed leandrodesign closed 3 weeks ago

leandrodesign commented 3 weeks ago

Can someone help me? I can't send buttons; where am I going wrong?

async sendButton(phoneNumber, data, delayMessage) { await this.verifyId(this.getWhatsAppId(phoneNumber)); await this.instance.sock?.presenceSubscribe(phoneNumber); await this.setStatus('composing', phoneNumber); await delay(delayMessage); await this.setStatus('paused', phoneNumber); const mess = { viewOnceMessage: { message: { messageContextInfo: { deviceListMetadata: {}, deviceListMetadataVersion: 2, }, interactiveMessage: { body: { text: (() => { let t = '' + data.title + ''; if (data?.description) { t += '\n\n'; t += data.description; t += '\n'; } return t; })(), }, footer: { text: data?.footer, }, // Message text nativeFlowMessage: { templateButtons: processButton(data.buttons), // Processed buttons }, }, }, }, }; const result = await this.instance.sock?.sendMessage( this.getWhatsAppId(phoneNumber), mess // Pass the mess object directly ); return result; // Return the send result }

module.exports = function processButton(buttons) { const preparedButtons = []; for (let i = 0; i < buttons.length; i++) { let btn = buttons[i]; let Button = { name: '', }; switch (btn.type) { case "replyButton": Button.name = "quick_reply"; Button.quickReplyButton = { displayText: btn.title ?? '', id: btn.id ?? '', }; break; case "callButton": Button.name = "cta_call"; Button.callButton = { displayText: btn.title ?? '', phoneNumber: btn.payload ?? '', }; break; case "urlButton": Button.name = "cta_url"; Button.urlButton = { displayText: btn.title ?? '', url: btn.payload ?? '', }; break; default: // Set a default name if necessary Button.name = "unknown_button"; break; } preparedButtons.push(Button); } return preparedButtons; }