-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong. |
||
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.
Sorry, something went wrong. |
||
return WAPI.scope(newMsgId, false, result, null); | ||
} else { | ||
return WAPI.scope(newMsgId, true, result, null); | ||
} | ||
} else { | ||
return chat; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hi @jonalan7, is it possible to do a check, before assing a default value? Like