Skip to content

Commit

Permalink
feat: sendListMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Sep 16, 2021
1 parent 83dcc79 commit fd8792d
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,45 @@ export class SenderLayer extends ListenerLayer {
super(page, session, options);
}

/**
* Send List menu
* @param to the numberid xxx@c.us
* @param title the titulo
* @param subtitle the subtitle
* @param description the description
* @param buttonText the name button
* @param menu List menu
*/
public async sendListMenu(
to: string,
title: string,
subTitle: string,
description: string,
buttonText: string,
menu: Array<any>
): Promise<Object> {
return new Promise(async (resolve, reject) => {
const result = await this.page.evaluate(
({ to, title, subTitle, description, buttonText, menu }) => {
return WAPI.sendListMenu(
to,
title,
subTitle,
description,
buttonText,
menu
);
},
{ to, title, subTitle, description, buttonText, menu }
);
if (result['erro'] == true) {
return reject(result);
} else {
return resolve(result);
}
});
}

/**
* Sends a text message to given chat
* @param to chat id: xxxxx@us.c
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 @@ -156,3 +156,4 @@ export { setGroupDescription } from './set-group-description';
export { setGroupTitle } from './set-group-title';
export { setGroupSettings } from './set-group-settings';
export { sendButtons } from './send-buttons';
export { sendListMenu } from './send-list-menu';
207 changes: 207 additions & 0 deletions src/lib/wapi/functions/send-list-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mMMMMMMMMMNNNmmNNNMMNMM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MN/ +NMMMN- /yyyyyys d. dMMMMNo` dNy-+ymmmho-+NN- dMMMMMMMMN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMNyNMMMMN+::::::::::m+/mMMMMMMd: dMMNho///+ymMMN+/mMMMMMMMMNs/hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMNsmMMMMMMMMMMMMMMNNNNMMNNNMMNNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
/**
* Send List menu
* @param {string} to the numberid xxx@c.us
* @param {string} title the titulo
* @param {string} subtitle the subtitle
* @param {string} description the description
* @param {string} buttonText the name button
* @param {array} menu List menu
*/
export async function sendListMenu(
to,
title,
subTitle,
description,
buttonText,
menu
) {
if (!title && typeof title != 'string') {
return WAPI.scope(null, true, 404, 'Enter the title variable as an string');
}

if (!subTitle && typeof subTitle != 'string') {
return WAPI.scope(
null,
true,
404,
'Enter the SubTitle variable as an string'
);
}

if (!description && typeof description != 'string') {
return WAPI.scope(
null,
true,
404,
'Enter the description variable as an string'
);
}

if (!buttonText && typeof buttonText != 'string') {
return WAPI.scope(
null,
true,
404,
'Enter the buttonText variable as an string'
);
}

if (!menu && Array.isArray(menu) === false) {
return WAPI.scope(null, true, 404, 'Enter the menu variable as an array');
}

for (let index in menu) {
if (index !== 'remove') {
if (
!!menu[index].title &&
typeof menu[index].title === 'string' &&
menu[index].title.length
) {
if (
!!menu[index].rows &&
Array.isArray(menu[index].rows) &&
menu[index].rows.length
) {
for (let i in menu[index].rows) {
if (i !== 'remove') {
if (
!!menu[index].rows[i].title &&
menu[index].rows[i].title.length
) {
if (
!!menu[index].rows[i].description &&
menu[index].rows[i].description.length
) {
menu[index].rows[i].rowId = `dessert_${i}`;
} else {
return WAPI.scope(
null,
true,
404,
'Enter the Description variable as an string'
);
}
} else {
return WAPI.scope(
null,
true,
404,
'Enter the Title variable as an string'
);
}
}
}
} else {
return WAPI.scope(null, true, 404, 'Rows must be an object array');
}
} else {
return WAPI.scope(null, true, 404, 'Incorrect Title passed in menu');
}
}
}

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;
const inChat = await WAPI.getchatId(chat.id).catch(() => {});

if (inChat) {
chat.lastReceivedKey._serialized = inChat._serialized;
chat.lastReceivedKey.id = inChat.id;
}

const message = {
id: newMsgId,
ack: 0,
from: fromwWid,
to: chat.id,
local: !0,
self: 'out',
t: parseInt(new Date().getTime() / 1000),
isNewMsg: !0,
invis: true,
footer: subTitle,
notifyName: '',
type: 'list',
interactiveAnnotations: true,
interactiveMessage: true,
star: false,
broadcast: false,
fromMe: false,
list: {
title: title,
description: description,
buttonText: buttonText,
listType: 1,
sections: menu
}
};

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;
}
}
4 changes: 3 additions & 1 deletion src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ import {
setGroupDescription,
setGroupTitle,
setGroupSettings,
sendButtons
sendButtons,
sendListMenu
} from './functions';
import {
base64ToFile,
Expand Down Expand Up @@ -275,6 +276,7 @@ if (typeof window.WAPI === 'undefined') {
window.WAPI.setGroupSettings = setGroupSettings;

// Chatting functions
window.WAPI.sendListMenu = sendListMenu;
window.WAPI.sendChatstate = sendChatstate;
window.WAPI.sendMessageWithThumb = sendMessageWithThumb;
window.WAPI.processMessageObj = processMessageObj;
Expand Down
8 changes: 8 additions & 0 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ interface WAPI {
title: string
) => Promise<object>;
sendMessage: (to: string, content: string) => Promise<Object>;
sendListMenu: (
to: string,
title: string,
subTitle: string,
description: string,
buttonText: string,
menu: Array<any>
) => Promise<Object>;
sendMessageMentioned: (...args: any) => any;
sendMessageOptions: (
chat: any,
Expand Down

0 comments on commit fd8792d

Please sign in to comment.