-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsend-list-menu.js
152 lines (142 loc) · 3.76 KB
/
send-list-menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Send List menu
* @param {string} to the numberid [email protected]
* @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
) {
if (!menu[index].rows[i].rowId) {
menu[index].rows[i].rowId = `dessert_${i}`;
}
}
} 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 && chat.id) {
const newMsgId = await window.WAPI.getNewMessageId(chat.id._serialized);
const fromwWid = await Store.MaybeMeUser.getMaybeMeUser();
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' ||
result.messageSendResult === 'OK'
) {
return WAPI.scope(newMsgId, false, result, null);
} else {
return WAPI.scope(newMsgId, true, result, null);
}
} else {
return chat;
}
}