Skip to content

Commit

Permalink
fix: sendButtons - (reformulated)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Sep 8, 2021
1 parent 28515c4 commit 28d7898
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 50 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,26 @@ available can be found in [here](/src/api/layers) and
##### Here, `chatId` could be `<phoneNumber>@c.us` or `<phoneNumber>-<groupId>@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('[email protected]', 'Title', buttons, 'Description').then((result) => {
const buttons = [
{
"buttonText": {
"displayText": "Text of Button 1"
}
},
{
"buttonText": {
"displayText": "Text of Button 2"
}
}
]
await client.sendButtons('[email protected]', '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('[email protected]', './audio.mp3').then((result) => {
console.log('Result: ', result); //return object success
Expand Down
79 changes: 57 additions & 22 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]
* @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<Message> {
buttons: [],
subtitle: string
): Promise<object> {
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);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/wapi/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
89 changes: 78 additions & 11 deletions src/lib/wapi/functions/send-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,87 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/

/**
* Send message with options
* @param {string} chatid the numberid [email protected]
* Send buttons reply
* @param {string} to the numberid [email protected]
* @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}`;

This comment has been minimized.

Copy link
@liperg

liperg Sep 10, 2021

Hi @jonalan7, is it possible to do a check, before assing a default value? Like

if(!buttons[index].buttonId) {
 buttons[index].buttonId = `id${index}`;
}
if(!buttons[index].type ) {
  buttons[index].type = 1;
}
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') {

This comment has been minimized.

Copy link
@irfanmiankhan

irfanmiankhan Jul 29, 2022

Thanks to the community for providing such a appritiated things...
I test the code, all is working well but the buttons are not getting sent..
image
can you please fix this

return WAPI.scope(newMsgId, false, result, null);
} else {
return WAPI.scope(newMsgId, true, result, null);
}
} else {
return chat;
}
}
4 changes: 2 additions & 2 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ import {
logout,
setGroupDescription,
setGroupTitle,
setGroupSettings
setGroupSettings,
sendButtons
} from './functions';
import { sendButtons } from './functions/send-buttons';
import {
base64ToFile,
generateMediaKey,
Expand Down
8 changes: 4 additions & 4 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ interface WAPI {
options?: any
) => Promise<string>;
sendButtons: (
chat: any,
to: string,
title: string,
buttons: any,
description?: any
) => Promise<string>;
buttons: [],
subtitle: string
) => Promise<Object>;
sendMessageWithThumb: (
thumb: string,
url: string,
Expand Down

0 comments on commit 28d7898

Please sign in to comment.