From 28d7898ff5330b0b59cb4f393c19abf848fcc848 Mon Sep 17 00:00:00 2001 From: jonalan Date: Wed, 8 Sep 2021 15:39:49 -0300 Subject: [PATCH] fix: sendButtons - (reformulated) --- README.md | 25 ++++---- src/api/layers/sender.layer.ts | 79 ++++++++++++++++------- src/lib/wapi/functions/index.js | 1 + src/lib/wapi/functions/send-buttons.js | 89 ++++++++++++++++++++++---- src/lib/wapi/wapi.js | 4 +- src/types/WAPI.d.ts | 8 +-- 6 files changed, 156 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index c43663c84..af9d70a51 100644 --- a/README.md +++ b/README.md @@ -313,23 +313,26 @@ available can be found in [here](/src/api/layers) and ##### Here, `chatId` could be `@c.us` or `-@g.us` ```javascript - // Send Messages with Buttons Reply - - const buttons = [ - - {buttonId: 'id1', buttonText: {displayText: 'Text of Button 1'}, type: 1}, - {buttonId: 'id2', buttonText: {displayText: 'Text of Button 2'}, type: 1}, - {buttonId: 'id3', buttonText: {displayText: 'Text of Button 3'}, type: 1} - ] - -await client.sendButtons('000000000000@c.us', 'Title', buttons, 'Description').then((result) => { +const buttons = [ + { + "buttonText": { + "displayText": "Text of Button 1" + } + }, + { + "buttonText": { + "displayText": "Text of Button 2" + } + } + ] +await client.sendButtons('000000000000@c.us', 'Title', buttons, 'Description') + .then((result) => { console.log('Result: ', result); //return object success }) .catch((erro) => { console.error('Error when sending: ', erro); //return object error }); - // Send audio file MP3 await client.sendVoice('000000000000@c.us', './audio.mp3').then((result) => { console.log('Result: ', result); //return object success diff --git a/src/api/layers/sender.layer.ts b/src/api/layers/sender.layer.ts index 19b9f977e..6b65a8b74 100644 --- a/src/api/layers/sender.layer.ts +++ b/src/api/layers/sender.layer.ts @@ -283,32 +283,67 @@ export class SenderLayer extends ListenerLayer { /** * Send buttons reply - * @param to Chat id - * @param title title of message buttons - * @param description description of message buttons - * @param buttons array with buttons options + * @param {string} to the numberid xxx@c.us + * @param {string} title the titulo + * @param {string} subtitle the subtitle + * @param {array} buttons arrays */ public async sendButtons( - to: any, + to: string, title: string, - buttons: any, - description?: string - ): Promise { + buttons: [], + subtitle: string + ): Promise { return new Promise(async (resolve, reject) => { - try { - const messageId = await this.page.evaluate( - ({ to, title, buttons, description }) => { - return WAPI.sendButtons(to, title, buttons, description); - }, - { to, title, buttons, description } - ); - const result = (await this.page.evaluate( - (messageId: any) => WAPI.getMessageById(messageId), - messageId - )) as Message; - resolve(result); - } catch (error) { - reject(error); + const typeFunction = 'sendButtons'; + const type = 'string'; + const obj = 'object'; + const check = [ + { + param: 'to', + type: type, + value: to, + function: typeFunction, + isUser: true, + }, + { + param: 'title', + type: type, + value: title, + function: typeFunction, + isUser: true, + }, + { + param: 'subtitle', + type: type, + value: subtitle, + function: typeFunction, + isUser: true, + }, + { + param: 'buttons', + type: obj, + value: buttons, + function: typeFunction, + isUser: true, + }, + ]; + const validating = checkValuesSender(check); + if (typeof validating === 'object') { + return reject(validating); + } + + const result = await this.page.evaluate( + ({ to, title, buttons, subtitle }) => { + return WAPI.sendButtons(to, title, buttons, subtitle); + }, + { to, title, buttons, subtitle } + ); + + if (result['erro'] == true) { + return reject(result); + } else { + return resolve(result); } }); } diff --git a/src/lib/wapi/functions/index.js b/src/lib/wapi/functions/index.js index 53fa4bcde..8a64c75d1 100644 --- a/src/lib/wapi/functions/index.js +++ b/src/lib/wapi/functions/index.js @@ -155,3 +155,4 @@ export { logout } from './logout'; export { setGroupDescription } from './set-group-description'; export { setGroupTitle } from './set-group-title'; export { setGroupSettings } from './set-group-settings'; +export { sendButtons } from './send-buttons'; diff --git a/src/lib/wapi/functions/send-buttons.js b/src/lib/wapi/functions/send-buttons.js index 0564f6c3a..1bdcb1ba6 100644 --- a/src/lib/wapi/functions/send-buttons.js +++ b/src/lib/wapi/functions/send-buttons.js @@ -52,20 +52,87 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM */ - /** - * Send message with options - * @param {string} chatid the numberid xxx@c.us + * Send buttons reply + * @param {string} to the numberid xxx@c.us * @param {string} title the titulo - * @param {string} description the description + * @param {string} subtitle the subtitle * @param {array} buttons arrays */ -export async function sendButtons(chatId, title, buttons, description = '') { - let options = { - footer: description, - isDynamicReplyButtonsMsg: true, - dynamicReplyButtons: buttons - }; +export async function sendButtons(to, title, buttons, subtitle) { + if (typeof title != 'string' || title.length === 0) { + return WAPI.scope(to, true, 404, 'It is necessary to write a title!'); + } + + if (typeof subtitle != 'string' || subtitle.length === 0) { + return WAPI.scope(to, true, 404, 'It is necessary to write a subtitle!'); + } + + if (Array.isArray(buttons) && buttons.length > 0) { + for (let index in buttons) { + if (typeof buttons[index] !== 'function') { + if (!buttons[index].buttonText) { + return WAPI.scope(to, true, 404, 'passed object buttonText'); + } + if (typeof buttons[index].buttonText !== 'object') { + return WAPI.scope(to, true, 404, 'passed object value in buttonText'); + } + if (!buttons[index].buttonText.displayText) { + return WAPI.scope(to, true, 404, 'passed object displayText'); + } + if (typeof buttons[index].buttonText.displayText !== 'string') { + return WAPI.scope( + to, + true, + 404, + 'passed string value in displayText' + ); + } + buttons[index].buttonId = `id${index}`; + buttons[index].type = 1; + } + } + } + + const chat = await WAPI.sendExist(to); + + if (chat && chat.status != 404) { + const newMsgId = await window.WAPI.getNewMessageId(chat.id); + const fromwWid = await window.Store.Conn.wid; - return WAPI.sendMessageOptions(chatId, title, options); + const message = { + id: newMsgId, + ack: 0, + from: fromwWid, + to: chat.id, + local: !0, + self: 'out', + t: parseInt(new Date().getTime() / 1000), + isNewMsg: !0, + type: 'chat', + body: title, + caption: title, + content: title, + footer: subtitle, + isDynamicReplyButtonsMsg: true, + isForwarded: false, + isFromTemplate: false, + invis: true, + fromMe: false + }; + var obj = { + dynamicReplyButtons: buttons + }; + Object.assign(message, obj); + var result = ( + await Promise.all(window.Store.addAndSendMsgToChat(chat, message)) + )[1]; + if (result === 'success' || result === 'OK') { + return WAPI.scope(newMsgId, false, result, null); + } else { + return WAPI.scope(newMsgId, true, result, null); + } + } else { + return chat; + } } diff --git a/src/lib/wapi/wapi.js b/src/lib/wapi/wapi.js index 5b334249e..23652b73b 100644 --- a/src/lib/wapi/wapi.js +++ b/src/lib/wapi/wapi.js @@ -159,9 +159,9 @@ import { logout, setGroupDescription, setGroupTitle, - setGroupSettings + setGroupSettings, + sendButtons } from './functions'; -import { sendButtons } from './functions/send-buttons'; import { base64ToFile, generateMediaKey, diff --git a/src/types/WAPI.d.ts b/src/types/WAPI.d.ts index a038a3ed2..b1e2eb40f 100644 --- a/src/types/WAPI.d.ts +++ b/src/types/WAPI.d.ts @@ -219,11 +219,11 @@ interface WAPI { options?: any ) => Promise; sendButtons: ( - chat: any, + to: string, title: string, - buttons: any, - description?: any - ) => Promise; + buttons: [], + subtitle: string + ) => Promise; sendMessageWithThumb: ( thumb: string, url: string,