baileys-mbuilder 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MessageBuilder.js +1627 -0
- package/README.md +204 -0
- package/index.js +1663 -0
- package/package.json +39 -0
|
@@ -0,0 +1,1627 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Do not remove this watermark.
|
|
3
|
+
*
|
|
4
|
+
* NIXCODE - Advanced WhatsApp Interactive Message Builder
|
|
5
|
+
* Built for creating buttons, carousels, native flows,
|
|
6
|
+
* and AI rich response payloads using Baileys with
|
|
7
|
+
* fluent chaining, flexible payload customization,
|
|
8
|
+
* and scalable architecture for modern bot development.
|
|
9
|
+
*
|
|
10
|
+
* Runtime:
|
|
11
|
+
* - Baileys: @whiskeysockets/baileys (latest)
|
|
12
|
+
*
|
|
13
|
+
* Created by Nixel
|
|
14
|
+
* Contributors: ~ Ahmad tumbuh kembang
|
|
15
|
+
*
|
|
16
|
+
* WhatsApp: wa.me/6282139672290
|
|
17
|
+
* Channel: https://whatsapp.com/channel/0029VbCV1ck8fewpdNb2TY2k
|
|
18
|
+
*
|
|
19
|
+
* Copyright (c) 2026 Nixel
|
|
20
|
+
*
|
|
21
|
+
* Permission is granted to use and modify this library
|
|
22
|
+
* for personal or commercial projects.
|
|
23
|
+
*
|
|
24
|
+
* Reuploading, reselling, relicensing, or redistributing
|
|
25
|
+
* this library as a standalone product is prohibited.
|
|
26
|
+
*
|
|
27
|
+
* Do not claim this project as your own original work.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
const VERSION = "4.5";
|
|
31
|
+
|
|
32
|
+
import { generateWAMessageFromContent, prepareWAMessageMedia } from "baileys";
|
|
33
|
+
import crypto from "crypto";
|
|
34
|
+
import sharp from "sharp";
|
|
35
|
+
|
|
36
|
+
function extractIE(
|
|
37
|
+
text,
|
|
38
|
+
{ extract = true, hyperlink = true, citation = true, latex = true } = {},
|
|
39
|
+
) {
|
|
40
|
+
if (!extract) {
|
|
41
|
+
return {
|
|
42
|
+
text,
|
|
43
|
+
ie: [],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
let ie = [],
|
|
47
|
+
result = "",
|
|
48
|
+
last = 0,
|
|
49
|
+
citation_index = 1,
|
|
50
|
+
hyperlink_index = 0,
|
|
51
|
+
latex_index = 0,
|
|
52
|
+
stack = [];
|
|
53
|
+
for (let i = 0; i < text.length; i++) {
|
|
54
|
+
if (text[i] == "[" && text[i - 1] != "\\") {
|
|
55
|
+
stack.push(i);
|
|
56
|
+
} else if (text[i] == "]" && (text[i + 1] == "(" || text[i + 1] == "<")) {
|
|
57
|
+
let start = stack.pop();
|
|
58
|
+
if (start == null) continue;
|
|
59
|
+
let open = text[i + 1],
|
|
60
|
+
close = open == "(" ? ")" : ">",
|
|
61
|
+
type = open == "(" ? "link" : "latex",
|
|
62
|
+
end = i + 2,
|
|
63
|
+
depth = 1;
|
|
64
|
+
while (end < text.length && depth) {
|
|
65
|
+
if (text[end] == open && text[end - 1] != "\\") depth++;
|
|
66
|
+
else if (text[end] == close && text[end - 1] != "\\") depth--;
|
|
67
|
+
end++;
|
|
68
|
+
}
|
|
69
|
+
if (depth) continue;
|
|
70
|
+
let raw = text.slice(start + 1, i).trim(),
|
|
71
|
+
url = text.slice(i + 2, end - 1).trim(),
|
|
72
|
+
key,
|
|
73
|
+
tag,
|
|
74
|
+
data;
|
|
75
|
+
if (type == "latex") {
|
|
76
|
+
if (!latex) continue;
|
|
77
|
+
let [
|
|
78
|
+
txt = "",
|
|
79
|
+
width = null,
|
|
80
|
+
height = null,
|
|
81
|
+
font_height = null,
|
|
82
|
+
padding = null,
|
|
83
|
+
] = raw.split("|");
|
|
84
|
+
key = `\u004E\u0049\u0058\u0045\u004C_LATEX_${latex_index++}`;
|
|
85
|
+
tag = `{{${key}}}${txt || "image"}{{/${key}}}`;
|
|
86
|
+
data = {
|
|
87
|
+
type: "latex",
|
|
88
|
+
ie: {
|
|
89
|
+
key,
|
|
90
|
+
text: txt,
|
|
91
|
+
url,
|
|
92
|
+
width,
|
|
93
|
+
height,
|
|
94
|
+
font_height,
|
|
95
|
+
padding,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
} else if (raw) {
|
|
99
|
+
if (!hyperlink) continue;
|
|
100
|
+
key = `\u004E\u0049\u0058\u0045\u004C_HYPERLINK_${hyperlink_index++}`;
|
|
101
|
+
tag = `{{${key}}}${url}{{/${key}}}`;
|
|
102
|
+
data = {
|
|
103
|
+
type: "hyperlink",
|
|
104
|
+
ie: {
|
|
105
|
+
key,
|
|
106
|
+
text: raw,
|
|
107
|
+
url,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
} else {
|
|
111
|
+
if (!citation) continue;
|
|
112
|
+
key = `\u004E\u0049\u0058\u0045\u004C_CITATION_${citation_index - 1}`;
|
|
113
|
+
tag = `{{${key}}}${url}{{/${key}}}`;
|
|
114
|
+
data = {
|
|
115
|
+
type: "citation",
|
|
116
|
+
ie: {
|
|
117
|
+
reference_id: citation_index++,
|
|
118
|
+
key,
|
|
119
|
+
text: "",
|
|
120
|
+
url,
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
result += text.slice(last, start) + tag;
|
|
125
|
+
last = end;
|
|
126
|
+
ie.push(data);
|
|
127
|
+
i = end - 1;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
result += text.slice(last);
|
|
131
|
+
return {
|
|
132
|
+
text: result,
|
|
133
|
+
ie,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
class BaseBuilder {
|
|
138
|
+
constructor() {
|
|
139
|
+
this._title = "";
|
|
140
|
+
this._subtitle = "";
|
|
141
|
+
this._body = "";
|
|
142
|
+
this._footer = "";
|
|
143
|
+
this._contextInfo = {};
|
|
144
|
+
this._extraPayload = {};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
setTitle(title) {
|
|
148
|
+
if (typeof title !== "string") {
|
|
149
|
+
throw new TypeError("Title must be a string");
|
|
150
|
+
}
|
|
151
|
+
this._title = title;
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
setSubtitle(subtitle) {
|
|
156
|
+
if (typeof subtitle !== "string") {
|
|
157
|
+
throw new TypeError("Subtitle must be a string");
|
|
158
|
+
}
|
|
159
|
+
this._subtitle = subtitle;
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
setBody(body) {
|
|
164
|
+
if (typeof body !== "string") {
|
|
165
|
+
throw new TypeError("Body must be a string");
|
|
166
|
+
}
|
|
167
|
+
this._body = body;
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
setFooter(footer) {
|
|
172
|
+
if (typeof footer !== "string") {
|
|
173
|
+
throw new TypeError("Footer must be a string");
|
|
174
|
+
}
|
|
175
|
+
this._footer = footer;
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
setContextInfo(obj) {
|
|
180
|
+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
|
|
181
|
+
throw new TypeError("ContextInfo must be a plain object");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
this._contextInfo = obj;
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
addPayload(obj) {
|
|
189
|
+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
|
|
190
|
+
throw new TypeError("Payload must be a plain object");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
Object.assign(this._extraPayload, obj);
|
|
194
|
+
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static async resize(buffer, x, y, fit = "cover") {
|
|
199
|
+
return await sharp(buffer)
|
|
200
|
+
.resize(x, y, {
|
|
201
|
+
fit,
|
|
202
|
+
position: "center",
|
|
203
|
+
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
204
|
+
})
|
|
205
|
+
.png()
|
|
206
|
+
.toBuffer();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
static async fetchBuffer(url, options = {}, config = {}) {
|
|
210
|
+
try {
|
|
211
|
+
let response = await fetch(url, options);
|
|
212
|
+
if (!response.ok) throw Error(`HTTP ${response.status}`);
|
|
213
|
+
return Buffer.from(await response.arrayBuffer());
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (config.silent) return Buffer.alloc(0);
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
class Button extends BaseBuilder {
|
|
222
|
+
#client;
|
|
223
|
+
|
|
224
|
+
constructor(client) {
|
|
225
|
+
super();
|
|
226
|
+
if (!client) {
|
|
227
|
+
throw new Error("Socket is required");
|
|
228
|
+
}
|
|
229
|
+
this.#client = client;
|
|
230
|
+
|
|
231
|
+
this._buttons = [];
|
|
232
|
+
this._data;
|
|
233
|
+
this._currentSelectionIndex = -1;
|
|
234
|
+
this._currentSectionIndex = -1;
|
|
235
|
+
this._params = {};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
setVideo(path, options = {}) {
|
|
239
|
+
if (!path) throw new Error("Url or buffer needed");
|
|
240
|
+
Buffer.isBuffer(path)
|
|
241
|
+
? (this._data = { video: path, ...options })
|
|
242
|
+
: (this._data = { video: { url: path }, ...options });
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
setImage(path, options = {}) {
|
|
247
|
+
if (!path) throw new Error("Url or buffer needed");
|
|
248
|
+
Buffer.isBuffer(path)
|
|
249
|
+
? (this._data = { image: path, ...options })
|
|
250
|
+
: (this._data = { image: { url: path }, ...options });
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
setDocument(path, options = {}) {
|
|
255
|
+
if (!path) throw new Error("Url or buffer needed");
|
|
256
|
+
Buffer.isBuffer(path)
|
|
257
|
+
? (this._data = { document: path, ...options })
|
|
258
|
+
: (this._data = { document: { url: path }, ...options });
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
setMedia(obj) {
|
|
263
|
+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
|
|
264
|
+
throw new TypeError("Media must be a plain object");
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
this._data = obj;
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
clearButtons() {
|
|
272
|
+
this._buttons = [];
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
setParams(obj) {
|
|
277
|
+
this._params = obj;
|
|
278
|
+
return this;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
addButton(name, params) {
|
|
282
|
+
this._buttons.push({
|
|
283
|
+
name,
|
|
284
|
+
buttonParamsJson:
|
|
285
|
+
typeof params === "string" ? params : JSON.stringify(params),
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
makeRow(header = "", title = "", description = "", id = "") {
|
|
292
|
+
if (
|
|
293
|
+
this._currentSelectionIndex === -1 ||
|
|
294
|
+
this._currentSectionIndex === -1
|
|
295
|
+
) {
|
|
296
|
+
throw new Error("You need to create a selection and a section first");
|
|
297
|
+
}
|
|
298
|
+
const buttonParams = JSON.parse(
|
|
299
|
+
this._buttons[this._currentSelectionIndex].buttonParamsJson,
|
|
300
|
+
);
|
|
301
|
+
buttonParams.sections[this._currentSectionIndex].rows.push({
|
|
302
|
+
header,
|
|
303
|
+
title,
|
|
304
|
+
description,
|
|
305
|
+
id,
|
|
306
|
+
});
|
|
307
|
+
this._buttons[this._currentSelectionIndex].buttonParamsJson =
|
|
308
|
+
JSON.stringify(buttonParams);
|
|
309
|
+
return this;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
makeSection(title = "", highlight_label = "") {
|
|
313
|
+
if (this._currentSelectionIndex === -1) {
|
|
314
|
+
throw new Error("You need to create a selection first");
|
|
315
|
+
}
|
|
316
|
+
const buttonParams = JSON.parse(
|
|
317
|
+
this._buttons[this._currentSelectionIndex].buttonParamsJson,
|
|
318
|
+
);
|
|
319
|
+
buttonParams.sections.push({ title, highlight_label, rows: [] });
|
|
320
|
+
this._currentSectionIndex = buttonParams.sections.length - 1;
|
|
321
|
+
this._buttons[this._currentSelectionIndex].buttonParamsJson =
|
|
322
|
+
JSON.stringify(buttonParams);
|
|
323
|
+
return this;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
addSelection(title, options = {}) {
|
|
327
|
+
this._buttons.push({
|
|
328
|
+
...options,
|
|
329
|
+
name: "single_select",
|
|
330
|
+
buttonParamsJson: JSON.stringify({ title, sections: [] }),
|
|
331
|
+
});
|
|
332
|
+
this._currentSelectionIndex = this._buttons.length - 1;
|
|
333
|
+
this._currentSectionIndex = -1;
|
|
334
|
+
return this;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
addReply(display_text = "", id = "", options = {}) {
|
|
338
|
+
this._buttons.push({
|
|
339
|
+
name: "quick_reply",
|
|
340
|
+
buttonParamsJson: JSON.stringify({
|
|
341
|
+
display_text,
|
|
342
|
+
id,
|
|
343
|
+
...options,
|
|
344
|
+
}),
|
|
345
|
+
});
|
|
346
|
+
return this;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
addCall(display_text = "", id = "", options = {}) {
|
|
350
|
+
this._buttons.push({
|
|
351
|
+
name: "cta_call",
|
|
352
|
+
buttonParamsJson: JSON.stringify({
|
|
353
|
+
display_text,
|
|
354
|
+
id,
|
|
355
|
+
...options,
|
|
356
|
+
}),
|
|
357
|
+
});
|
|
358
|
+
return this;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
addReminder(display_text = "", id = "", options = {}) {
|
|
362
|
+
this._buttons.push({
|
|
363
|
+
name: "cta_reminder",
|
|
364
|
+
buttonParamsJson: JSON.stringify({
|
|
365
|
+
display_text,
|
|
366
|
+
id,
|
|
367
|
+
...options,
|
|
368
|
+
}),
|
|
369
|
+
});
|
|
370
|
+
return this;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
addCancelReminder(display_text = "", id = "", options = {}) {
|
|
374
|
+
this._buttons.push({
|
|
375
|
+
name: "cta_cancel_reminder",
|
|
376
|
+
buttonParamsJson: JSON.stringify({
|
|
377
|
+
display_text,
|
|
378
|
+
id,
|
|
379
|
+
...options,
|
|
380
|
+
}),
|
|
381
|
+
});
|
|
382
|
+
return this;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
addAddress(display_text = "", id = "", options = {}) {
|
|
386
|
+
this._buttons.push({
|
|
387
|
+
name: "address_message",
|
|
388
|
+
buttonParamsJson: JSON.stringify({
|
|
389
|
+
display_text,
|
|
390
|
+
id,
|
|
391
|
+
...options,
|
|
392
|
+
}),
|
|
393
|
+
});
|
|
394
|
+
return this;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
addLocation(options = {}) {
|
|
398
|
+
this._buttons.push({
|
|
399
|
+
name: "send_location",
|
|
400
|
+
buttonParamsJson: JSON.stringify(options),
|
|
401
|
+
});
|
|
402
|
+
return this;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
addUrl(
|
|
406
|
+
display_text = "",
|
|
407
|
+
url = "",
|
|
408
|
+
webview_interaction = false,
|
|
409
|
+
options = {},
|
|
410
|
+
) {
|
|
411
|
+
this._buttons.push({
|
|
412
|
+
...options,
|
|
413
|
+
name: "cta_url",
|
|
414
|
+
buttonParamsJson: JSON.stringify({
|
|
415
|
+
display_text,
|
|
416
|
+
url,
|
|
417
|
+
webview_interaction,
|
|
418
|
+
...options,
|
|
419
|
+
}),
|
|
420
|
+
});
|
|
421
|
+
return this;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
addCopy(display_text = "", copy_code = "", options = {}) {
|
|
425
|
+
this._buttons.push({
|
|
426
|
+
name: "cta_copy",
|
|
427
|
+
buttonParamsJson: JSON.stringify({
|
|
428
|
+
display_text,
|
|
429
|
+
copy_code,
|
|
430
|
+
...options,
|
|
431
|
+
}),
|
|
432
|
+
});
|
|
433
|
+
return this;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
static paramsList = {
|
|
437
|
+
limited_time_offer: {
|
|
438
|
+
text: "string",
|
|
439
|
+
url: "string",
|
|
440
|
+
copy_code: "string",
|
|
441
|
+
expiration_time: "number",
|
|
442
|
+
},
|
|
443
|
+
bottom_sheet: {
|
|
444
|
+
in_thread_buttons_limit: "number",
|
|
445
|
+
divider_indices: ["number"],
|
|
446
|
+
list_title: "string",
|
|
447
|
+
button_title: "string",
|
|
448
|
+
},
|
|
449
|
+
tap_target_configuration: {
|
|
450
|
+
title: "string",
|
|
451
|
+
description: "string",
|
|
452
|
+
canonical_url: "string",
|
|
453
|
+
domain: "string",
|
|
454
|
+
buttonIndex: "number",
|
|
455
|
+
},
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
async toCard() {
|
|
459
|
+
return {
|
|
460
|
+
body: {
|
|
461
|
+
text: this._body,
|
|
462
|
+
},
|
|
463
|
+
footer: {
|
|
464
|
+
text: this._footer,
|
|
465
|
+
},
|
|
466
|
+
header: {
|
|
467
|
+
title: this._title,
|
|
468
|
+
subtitle: this._subtitle,
|
|
469
|
+
hasMediaAttachment: !!this._data,
|
|
470
|
+
...(this._data
|
|
471
|
+
? await prepareWAMessageMedia(this._data, {
|
|
472
|
+
upload: this.#client.waUploadToServer,
|
|
473
|
+
}).catch((e) => {
|
|
474
|
+
if (String(e).includes("Invalid media type")) return this._data;
|
|
475
|
+
throw e;
|
|
476
|
+
})
|
|
477
|
+
: {}),
|
|
478
|
+
},
|
|
479
|
+
nativeFlowMessage: {
|
|
480
|
+
messageParamsJson: JSON.stringify(this._params),
|
|
481
|
+
buttons: this._buttons,
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
async build(jid, { ...options } = {}) {
|
|
487
|
+
const message = await this.toCard();
|
|
488
|
+
|
|
489
|
+
return generateWAMessageFromContent(
|
|
490
|
+
jid,
|
|
491
|
+
{
|
|
492
|
+
...this._extraPayload,
|
|
493
|
+
interactiveMessage: {
|
|
494
|
+
...message,
|
|
495
|
+
contextInfo: this._contextInfo,
|
|
496
|
+
},
|
|
497
|
+
},
|
|
498
|
+
{ ...options },
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
async send(jid, { ...options } = {}) {
|
|
503
|
+
const msg = await this.build(jid, options);
|
|
504
|
+
|
|
505
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
506
|
+
messageId: msg.key.id,
|
|
507
|
+
additionalNodes: [
|
|
508
|
+
{
|
|
509
|
+
tag: "biz",
|
|
510
|
+
attrs: {},
|
|
511
|
+
content: [
|
|
512
|
+
{
|
|
513
|
+
tag: "interactive",
|
|
514
|
+
attrs: { type: "native_flow", v: "1" },
|
|
515
|
+
content: [
|
|
516
|
+
{ tag: "native_flow", attrs: { v: "9", name: "mixed" } },
|
|
517
|
+
],
|
|
518
|
+
},
|
|
519
|
+
],
|
|
520
|
+
},
|
|
521
|
+
],
|
|
522
|
+
...options,
|
|
523
|
+
});
|
|
524
|
+
return msg;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
class ButtonV2 extends BaseBuilder {
|
|
529
|
+
#client;
|
|
530
|
+
|
|
531
|
+
constructor(client) {
|
|
532
|
+
super();
|
|
533
|
+
if (!client) {
|
|
534
|
+
throw new Error("Socket is required");
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
this.#client = client;
|
|
538
|
+
this._image;
|
|
539
|
+
this._data;
|
|
540
|
+
this._buttons = [];
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
addButton(displayText = "", buttonId = crypto.randomUUID()) {
|
|
544
|
+
this._buttons.push({
|
|
545
|
+
buttonId,
|
|
546
|
+
buttonText: { displayText },
|
|
547
|
+
type: 1,
|
|
548
|
+
});
|
|
549
|
+
return this;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
addRawButton(obj) {
|
|
553
|
+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
|
|
554
|
+
throw new TypeError("Buttons must be a plain object");
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
this._buttons.push(obj);
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
setThumbnail(path) {
|
|
562
|
+
if (!path) throw new Error("Url or buffer needed");
|
|
563
|
+
this._image = path;
|
|
564
|
+
return this;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
setMedia(obj) {
|
|
568
|
+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
|
|
569
|
+
throw new TypeError("Media must be a plain object");
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
this._data = obj;
|
|
573
|
+
return this;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
async build(jid, { ...options } = {}) {
|
|
577
|
+
let _thumbnail = this._image
|
|
578
|
+
? await BaseBuilder.resize(
|
|
579
|
+
Buffer.isBuffer(this._image)
|
|
580
|
+
? this._image
|
|
581
|
+
: await BaseBuilder.fetchBuffer(this._image, {}, { silent: true }),
|
|
582
|
+
300,
|
|
583
|
+
300,
|
|
584
|
+
)
|
|
585
|
+
: null;
|
|
586
|
+
const msg = generateWAMessageFromContent(
|
|
587
|
+
jid,
|
|
588
|
+
{
|
|
589
|
+
...this._extraPayload,
|
|
590
|
+
buttonsMessage: {
|
|
591
|
+
contentText: this._body,
|
|
592
|
+
footerText: this._footer,
|
|
593
|
+
...(this._data
|
|
594
|
+
? this._data
|
|
595
|
+
: {
|
|
596
|
+
headerType: 6,
|
|
597
|
+
locationMessage: {
|
|
598
|
+
degreesLatitude: 0,
|
|
599
|
+
degreesLongitude: 0,
|
|
600
|
+
name: this._title,
|
|
601
|
+
address: this._subtitle,
|
|
602
|
+
jpegThumbnail: _thumbnail,
|
|
603
|
+
},
|
|
604
|
+
}),
|
|
605
|
+
viewOnce: true,
|
|
606
|
+
contextInfo: this._contextInfo,
|
|
607
|
+
buttons: [...this._buttons],
|
|
608
|
+
},
|
|
609
|
+
},
|
|
610
|
+
{ ...options },
|
|
611
|
+
);
|
|
612
|
+
return msg;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
async send(jid, { ...options } = {}) {
|
|
616
|
+
if (this._buttons.length < 1)
|
|
617
|
+
throw new Error("ButtonV2 requires at least one button");
|
|
618
|
+
const msg = await this.build(jid, options);
|
|
619
|
+
|
|
620
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
621
|
+
messageId: msg.key.id,
|
|
622
|
+
additionalNodes: [
|
|
623
|
+
{
|
|
624
|
+
tag: "biz",
|
|
625
|
+
attrs: {},
|
|
626
|
+
content: [
|
|
627
|
+
{
|
|
628
|
+
tag: "interactive",
|
|
629
|
+
attrs: { type: "native_flow", v: "1" },
|
|
630
|
+
content: [
|
|
631
|
+
{ tag: "native_flow", attrs: { v: "9", name: "mixed" } },
|
|
632
|
+
],
|
|
633
|
+
},
|
|
634
|
+
],
|
|
635
|
+
},
|
|
636
|
+
],
|
|
637
|
+
...options,
|
|
638
|
+
});
|
|
639
|
+
return msg;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
class Carousel extends BaseBuilder {
|
|
644
|
+
#client;
|
|
645
|
+
|
|
646
|
+
constructor(client) {
|
|
647
|
+
super();
|
|
648
|
+
if (!client) {
|
|
649
|
+
throw new Error("Socket is required");
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
this.#client = client;
|
|
653
|
+
this._cards = [];
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
addCard(card) {
|
|
657
|
+
const cards = Array.isArray(card) ? card : [card];
|
|
658
|
+
const baseIndex = this._cards.length;
|
|
659
|
+
|
|
660
|
+
for (const [index, c] of cards.entries()) {
|
|
661
|
+
if (!c?.header?.hasMediaAttachment) {
|
|
662
|
+
throw new Error(
|
|
663
|
+
`Card [${baseIndex + index}] must include an image or video in header`,
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
this._cards.push(...cards);
|
|
669
|
+
return this;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
build(jid, { ...options } = {}) {
|
|
673
|
+
return generateWAMessageFromContent(
|
|
674
|
+
jid,
|
|
675
|
+
{
|
|
676
|
+
...this._extraPayload,
|
|
677
|
+
interactiveMessage: {
|
|
678
|
+
header: {
|
|
679
|
+
hasMediaAttachment: false,
|
|
680
|
+
},
|
|
681
|
+
body: { text: this._body },
|
|
682
|
+
footer: { text: this._footer },
|
|
683
|
+
contextInfo: this._contextInfo,
|
|
684
|
+
carouselMessage: {
|
|
685
|
+
cards: this._cards,
|
|
686
|
+
},
|
|
687
|
+
},
|
|
688
|
+
},
|
|
689
|
+
{ ...options },
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
async send(jid, { ...options } = {}) {
|
|
694
|
+
const msg = this.build(jid, options);
|
|
695
|
+
|
|
696
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
697
|
+
messageId: msg.key.id,
|
|
698
|
+
additionalNodes: [
|
|
699
|
+
{
|
|
700
|
+
tag: "biz",
|
|
701
|
+
attrs: {},
|
|
702
|
+
content: [
|
|
703
|
+
{
|
|
704
|
+
tag: "interactive",
|
|
705
|
+
attrs: { type: "native_flow", v: "1" },
|
|
706
|
+
content: [
|
|
707
|
+
{ tag: "native_flow", attrs: { v: "9", name: "mixed" } },
|
|
708
|
+
],
|
|
709
|
+
},
|
|
710
|
+
],
|
|
711
|
+
},
|
|
712
|
+
],
|
|
713
|
+
...options,
|
|
714
|
+
});
|
|
715
|
+
return msg;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
class AIRich extends BaseBuilder {
|
|
720
|
+
#client;
|
|
721
|
+
|
|
722
|
+
constructor(client) {
|
|
723
|
+
if (!client) {
|
|
724
|
+
throw new Error("Socket is required");
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
super();
|
|
728
|
+
this.#client = client;
|
|
729
|
+
this._contextInfo = {};
|
|
730
|
+
this._submessages = [];
|
|
731
|
+
this._sections = [];
|
|
732
|
+
this._richResponseSources = [];
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
static newLayout(name, data) {
|
|
736
|
+
return {
|
|
737
|
+
view_model: {
|
|
738
|
+
[Array.isArray(data) ? "primitives" : "primitive"]: data,
|
|
739
|
+
__typename: `GenAI${name}LayoutViewModel`,
|
|
740
|
+
},
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
addSubmessage(submessage) {
|
|
745
|
+
const items = Array.isArray(submessage) ? submessage : [submessage];
|
|
746
|
+
|
|
747
|
+
for (const item of items) {
|
|
748
|
+
if (typeof item !== "object" || item === null || Array.isArray(item)) {
|
|
749
|
+
throw new TypeError(
|
|
750
|
+
"Submessage must be a plain object or array of plain objects",
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
this._submessages.push(item);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
return this;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
addSection(section) {
|
|
761
|
+
const items = Array.isArray(section) ? section : [section];
|
|
762
|
+
|
|
763
|
+
for (const item of items) {
|
|
764
|
+
if (typeof item !== "object" || item === null || Array.isArray(item)) {
|
|
765
|
+
throw new TypeError(
|
|
766
|
+
"Section must be a plain object or array of plain objects",
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
this._sections.push(item);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
return this;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
addText(text, { hyperlink = true, citation = true, latex = true } = {}) {
|
|
777
|
+
if (typeof text != "string") {
|
|
778
|
+
throw new TypeError("Text must be a string");
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
const extractedIE = extractIE(text, {
|
|
782
|
+
hyperlink,
|
|
783
|
+
citation,
|
|
784
|
+
latex,
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
const inline_entities = extractedIE.ie.map(({ type, ie }) => {
|
|
788
|
+
if (type == "hyperlink") {
|
|
789
|
+
return {
|
|
790
|
+
key: ie.key,
|
|
791
|
+
metadata: {
|
|
792
|
+
display_name: ie.text,
|
|
793
|
+
is_trusted: true,
|
|
794
|
+
url: ie.url,
|
|
795
|
+
__typename: "GenAIInlineLinkItem",
|
|
796
|
+
},
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
if (type == "citation") {
|
|
800
|
+
return {
|
|
801
|
+
key: ie.key,
|
|
802
|
+
metadata: {
|
|
803
|
+
reference_id: ie.reference_id,
|
|
804
|
+
reference_url: ie.url,
|
|
805
|
+
reference_title: ie.url,
|
|
806
|
+
reference_display_name: ie.url,
|
|
807
|
+
sources: [],
|
|
808
|
+
__typename: "GenAISearchCitationItem",
|
|
809
|
+
},
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
if (type == "latex") {
|
|
813
|
+
return {
|
|
814
|
+
key: ie.key,
|
|
815
|
+
metadata: {
|
|
816
|
+
latex_expression: ie.text,
|
|
817
|
+
latex_image: {
|
|
818
|
+
url: ie.url,
|
|
819
|
+
width: Number(ie.width) || 100,
|
|
820
|
+
height: Number(ie.height) || 100,
|
|
821
|
+
},
|
|
822
|
+
font_height: Number(ie.font_height) || 83.333333333333,
|
|
823
|
+
padding: Number(ie.padding) || 15,
|
|
824
|
+
__typename: "GenAILatexItem",
|
|
825
|
+
},
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
return {
|
|
830
|
+
key: ie.key,
|
|
831
|
+
metadata: {
|
|
832
|
+
latex_expression: ie.text,
|
|
833
|
+
latex_image: {
|
|
834
|
+
url: ie.url,
|
|
835
|
+
width,
|
|
836
|
+
height,
|
|
837
|
+
},
|
|
838
|
+
font_height: Number(ie.font_height) || 83.333333333333,
|
|
839
|
+
padding: Number(ie.padding) || 15,
|
|
840
|
+
__typename: "GenAILatexItem",
|
|
841
|
+
},
|
|
842
|
+
};
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
this._submessages.push({
|
|
846
|
+
messageType: 2,
|
|
847
|
+
messageText: extractedIE.text,
|
|
848
|
+
});
|
|
849
|
+
|
|
850
|
+
this._sections.push(
|
|
851
|
+
AIRich.newLayout("Single", {
|
|
852
|
+
text: extractedIE.text,
|
|
853
|
+
...(inline_entities.length && {
|
|
854
|
+
inline_entities,
|
|
855
|
+
}),
|
|
856
|
+
__typename: "GenAIMarkdownTextUXPrimitive",
|
|
857
|
+
}),
|
|
858
|
+
);
|
|
859
|
+
|
|
860
|
+
return this;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
addCode(language, code) {
|
|
864
|
+
if (typeof language !== "string" || typeof code !== "string") {
|
|
865
|
+
throw new TypeError("Language and code must be a string");
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
const meta = AIRich.tokenizer(code, language);
|
|
869
|
+
|
|
870
|
+
this._submessages.push({
|
|
871
|
+
messageType: 5,
|
|
872
|
+
codeMetadata: {
|
|
873
|
+
codeLanguage: language,
|
|
874
|
+
codeBlocks: meta.codeBlock,
|
|
875
|
+
},
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
this._sections.push(
|
|
879
|
+
AIRich.newLayout("Single", {
|
|
880
|
+
language,
|
|
881
|
+
code_blocks: meta.unified_codeBlock,
|
|
882
|
+
__typename: "GenAICodeUXPrimitive",
|
|
883
|
+
}),
|
|
884
|
+
);
|
|
885
|
+
|
|
886
|
+
return this;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
addTable(table) {
|
|
890
|
+
if (!Array.isArray(table)) {
|
|
891
|
+
throw new TypeError("Table must be an array");
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
const meta = AIRich.toTableMetadata(table);
|
|
895
|
+
|
|
896
|
+
this._submessages.push({
|
|
897
|
+
messageType: 4,
|
|
898
|
+
tableMetadata: {
|
|
899
|
+
title: meta.title,
|
|
900
|
+
rows: meta.rows,
|
|
901
|
+
},
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
this._sections.push(
|
|
905
|
+
AIRich.newLayout("Single", {
|
|
906
|
+
rows: meta.unified_rows,
|
|
907
|
+
__typename: "GenAITableUXPrimitive",
|
|
908
|
+
}),
|
|
909
|
+
);
|
|
910
|
+
|
|
911
|
+
return this;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
addSource(sources = []) {
|
|
915
|
+
if (
|
|
916
|
+
!(
|
|
917
|
+
Array.isArray(sources) &&
|
|
918
|
+
(sources.every((item) => typeof item === "string") ||
|
|
919
|
+
sources.every(
|
|
920
|
+
(item) =>
|
|
921
|
+
Array.isArray(item) && item.every((v) => typeof v === "string"),
|
|
922
|
+
))
|
|
923
|
+
)
|
|
924
|
+
) {
|
|
925
|
+
throw new TypeError(
|
|
926
|
+
"Sources must be a string array or an array of string arrays",
|
|
927
|
+
);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
if (sources.every((item) => typeof item === "string")) {
|
|
931
|
+
sources = [sources];
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
const source = sources.map(([profile_url, url, text]) => ({
|
|
935
|
+
source_type: "THIRD_PARTY",
|
|
936
|
+
source_display_name: text ?? "",
|
|
937
|
+
source_subtitle: "AI",
|
|
938
|
+
source_url: url ?? "",
|
|
939
|
+
favicon: {
|
|
940
|
+
url: profile_url ?? "",
|
|
941
|
+
mime_type: "image/jpeg",
|
|
942
|
+
width: 16,
|
|
943
|
+
height: 16,
|
|
944
|
+
},
|
|
945
|
+
}));
|
|
946
|
+
|
|
947
|
+
this._sections.push(
|
|
948
|
+
AIRich.newLayout("Single", {
|
|
949
|
+
sources: source,
|
|
950
|
+
__typename: "GenAISearchResultPrimitive",
|
|
951
|
+
}),
|
|
952
|
+
);
|
|
953
|
+
|
|
954
|
+
return this;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
addReels(reelsItems = []) {
|
|
958
|
+
if (
|
|
959
|
+
!(
|
|
960
|
+
(reelsItems &&
|
|
961
|
+
typeof reelsItems === "object" &&
|
|
962
|
+
!Array.isArray(reelsItems)) ||
|
|
963
|
+
(Array.isArray(reelsItems) &&
|
|
964
|
+
reelsItems.every(
|
|
965
|
+
(item) => item && typeof item === "object" && !Array.isArray(item),
|
|
966
|
+
))
|
|
967
|
+
)
|
|
968
|
+
) {
|
|
969
|
+
throw new TypeError(
|
|
970
|
+
"Reels items must be an object or an array of objects",
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
if (!Array.isArray(reelsItems)) {
|
|
975
|
+
reelsItems = [reelsItems];
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
this._submessages.push({
|
|
979
|
+
messageType: 9,
|
|
980
|
+
contentItemsMetadata: {
|
|
981
|
+
contentType: 1,
|
|
982
|
+
itemsMetadata: reelsItems.map((item) => ({
|
|
983
|
+
reelItem: {
|
|
984
|
+
title: item.username ?? "",
|
|
985
|
+
profileIconUrl: item.profileIconUrl ?? item.profile_url ?? "",
|
|
986
|
+
thumbnailUrl: item.thumbnailUrl ?? item.thumbnail ?? "",
|
|
987
|
+
videoUrl: item.videoUrl ?? item.url ?? "",
|
|
988
|
+
},
|
|
989
|
+
})),
|
|
990
|
+
},
|
|
991
|
+
});
|
|
992
|
+
|
|
993
|
+
reelsItems.forEach((item, idx) => {
|
|
994
|
+
this._richResponseSources.push({
|
|
995
|
+
provider: "\u004E\u0049\u0058\u0045\u004C",
|
|
996
|
+
thumbnailCDNURL: item.thumbnailUrl ?? item.thumbnail ?? "",
|
|
997
|
+
sourceProviderURL: item.videoUrl ?? item.url ?? "",
|
|
998
|
+
sourceQuery: "",
|
|
999
|
+
faviconCDNURL: item.profileIconUrl ?? item.profile_url ?? "",
|
|
1000
|
+
citationNumber: idx + 1,
|
|
1001
|
+
sourceTitle: item.username ?? "",
|
|
1002
|
+
});
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
this._sections.push(
|
|
1006
|
+
AIRich.newLayout(
|
|
1007
|
+
"HScroll",
|
|
1008
|
+
reelsItems.map((item) => ({
|
|
1009
|
+
reels_url: item.videoUrl ?? item.url ?? "",
|
|
1010
|
+
thumbnail_url: item.thumbnailUrl ?? item.thumbnail ?? "",
|
|
1011
|
+
creator: item.username ?? item.title ?? "",
|
|
1012
|
+
avatar_url: item.profileIconUrl ?? item.profile_url ?? "",
|
|
1013
|
+
reels_title: item.reels_title ?? item.title ?? "",
|
|
1014
|
+
likes_count: item.likes_count ?? item.like ?? 0,
|
|
1015
|
+
shares_count: item.shares_count ?? item.share ?? 0,
|
|
1016
|
+
view_count: item.view_count ?? item.view ?? 0,
|
|
1017
|
+
reel_source: item.reel_source ?? item.source ?? "IG",
|
|
1018
|
+
is_verified: !!(item.is_verified || item.verified),
|
|
1019
|
+
__typename: "GenAIReelPrimitive",
|
|
1020
|
+
})),
|
|
1021
|
+
),
|
|
1022
|
+
);
|
|
1023
|
+
|
|
1024
|
+
return this;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
addImage(imageUrl) {
|
|
1028
|
+
if (
|
|
1029
|
+
!(
|
|
1030
|
+
typeof imageUrl === "string" ||
|
|
1031
|
+
(Array.isArray(imageUrl) &&
|
|
1032
|
+
imageUrl.every((v) => typeof v === "string"))
|
|
1033
|
+
)
|
|
1034
|
+
) {
|
|
1035
|
+
throw new TypeError("imageUrl must be a string or array of strings");
|
|
1036
|
+
}
|
|
1037
|
+
const imageUrls = Array.isArray(imageUrl)
|
|
1038
|
+
? imageUrl.map((url) => ({
|
|
1039
|
+
imagePreviewUrl: url,
|
|
1040
|
+
imageHighResUrl: url,
|
|
1041
|
+
sourceUrl: String.fromCharCode(
|
|
1042
|
+
104,
|
|
1043
|
+
116,
|
|
1044
|
+
116,
|
|
1045
|
+
112,
|
|
1046
|
+
115,
|
|
1047
|
+
58,
|
|
1048
|
+
47,
|
|
1049
|
+
47,
|
|
1050
|
+
102,
|
|
1051
|
+
105,
|
|
1052
|
+
111,
|
|
1053
|
+
114,
|
|
1054
|
+
97,
|
|
1055
|
+
46,
|
|
1056
|
+
110,
|
|
1057
|
+
105,
|
|
1058
|
+
120,
|
|
1059
|
+
101,
|
|
1060
|
+
108,
|
|
1061
|
+
46,
|
|
1062
|
+
109,
|
|
1063
|
+
121,
|
|
1064
|
+
46,
|
|
1065
|
+
105,
|
|
1066
|
+
100,
|
|
1067
|
+
47,
|
|
1068
|
+
),
|
|
1069
|
+
}))
|
|
1070
|
+
: [
|
|
1071
|
+
{
|
|
1072
|
+
imagePreviewUrl: imageUrl,
|
|
1073
|
+
imageHighResUrl: imageUrl,
|
|
1074
|
+
sourceUrl: String.fromCharCode(
|
|
1075
|
+
104,
|
|
1076
|
+
116,
|
|
1077
|
+
116,
|
|
1078
|
+
112,
|
|
1079
|
+
115,
|
|
1080
|
+
58,
|
|
1081
|
+
47,
|
|
1082
|
+
47,
|
|
1083
|
+
102,
|
|
1084
|
+
105,
|
|
1085
|
+
111,
|
|
1086
|
+
114,
|
|
1087
|
+
97,
|
|
1088
|
+
46,
|
|
1089
|
+
110,
|
|
1090
|
+
105,
|
|
1091
|
+
120,
|
|
1092
|
+
101,
|
|
1093
|
+
108,
|
|
1094
|
+
46,
|
|
1095
|
+
109,
|
|
1096
|
+
121,
|
|
1097
|
+
46,
|
|
1098
|
+
105,
|
|
1099
|
+
100,
|
|
1100
|
+
47,
|
|
1101
|
+
),
|
|
1102
|
+
},
|
|
1103
|
+
];
|
|
1104
|
+
|
|
1105
|
+
this._submessages.push({
|
|
1106
|
+
messageType: 1,
|
|
1107
|
+
gridImageMetadata: {
|
|
1108
|
+
gridImageUrl: {
|
|
1109
|
+
imagePreviewUrl: Array.isArray(imageUrl) ? imageUrl[0] : imageUrl,
|
|
1110
|
+
},
|
|
1111
|
+
imageUrls,
|
|
1112
|
+
},
|
|
1113
|
+
});
|
|
1114
|
+
|
|
1115
|
+
imageUrls.forEach(({ imagePreviewUrl }) => {
|
|
1116
|
+
this._sections.push(
|
|
1117
|
+
AIRich.newLayout("Single", {
|
|
1118
|
+
media: {
|
|
1119
|
+
url: imagePreviewUrl,
|
|
1120
|
+
mime_type: "image/png",
|
|
1121
|
+
},
|
|
1122
|
+
imagine_type: "IMAGE",
|
|
1123
|
+
status: {
|
|
1124
|
+
status: "READY",
|
|
1125
|
+
},
|
|
1126
|
+
__typename: "GenAIImaginePrimitive",
|
|
1127
|
+
}),
|
|
1128
|
+
);
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
return this;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
addVideo(videoUrl) {
|
|
1135
|
+
if (
|
|
1136
|
+
!(
|
|
1137
|
+
typeof videoUrl === "string" ||
|
|
1138
|
+
(Array.isArray(videoUrl) &&
|
|
1139
|
+
videoUrl.every((v) => typeof v === "string"))
|
|
1140
|
+
)
|
|
1141
|
+
) {
|
|
1142
|
+
throw new TypeError("videoUrl must be a string or array of strings");
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
const videoUrls = (Array.isArray(videoUrl) ? videoUrl : [videoUrl]).map(
|
|
1146
|
+
(item) => {
|
|
1147
|
+
const [url, duration = 0] = item.split("|");
|
|
1148
|
+
|
|
1149
|
+
return {
|
|
1150
|
+
videoPreviewUrl: url,
|
|
1151
|
+
videoHighResUrl: url,
|
|
1152
|
+
duration: Number(duration) || 0,
|
|
1153
|
+
sourceUrl: String.fromCharCode(
|
|
1154
|
+
104,
|
|
1155
|
+
116,
|
|
1156
|
+
116,
|
|
1157
|
+
112,
|
|
1158
|
+
115,
|
|
1159
|
+
58,
|
|
1160
|
+
47,
|
|
1161
|
+
47,
|
|
1162
|
+
102,
|
|
1163
|
+
105,
|
|
1164
|
+
111,
|
|
1165
|
+
114,
|
|
1166
|
+
97,
|
|
1167
|
+
46,
|
|
1168
|
+
110,
|
|
1169
|
+
105,
|
|
1170
|
+
120,
|
|
1171
|
+
101,
|
|
1172
|
+
108,
|
|
1173
|
+
46,
|
|
1174
|
+
109,
|
|
1175
|
+
121,
|
|
1176
|
+
46,
|
|
1177
|
+
105,
|
|
1178
|
+
100,
|
|
1179
|
+
47,
|
|
1180
|
+
),
|
|
1181
|
+
};
|
|
1182
|
+
},
|
|
1183
|
+
);
|
|
1184
|
+
|
|
1185
|
+
this._submessages.push({
|
|
1186
|
+
messageType: 2,
|
|
1187
|
+
messageText: "[ CANNOT_LOAD_VIDEO - \u004E\u0049\u0058\u0045\u004C ]",
|
|
1188
|
+
});
|
|
1189
|
+
|
|
1190
|
+
videoUrls.forEach(({ videoPreviewUrl, duration = 0 }) => {
|
|
1191
|
+
this._sections.push(
|
|
1192
|
+
AIRich.newLayout("Single", {
|
|
1193
|
+
media: {
|
|
1194
|
+
url: videoPreviewUrl,
|
|
1195
|
+
mime_type: "video/mp4",
|
|
1196
|
+
duration,
|
|
1197
|
+
},
|
|
1198
|
+
imagine_type: "ANIMATE",
|
|
1199
|
+
status: {
|
|
1200
|
+
status: "READY",
|
|
1201
|
+
},
|
|
1202
|
+
__typename: "GenAIImaginePrimitive",
|
|
1203
|
+
}),
|
|
1204
|
+
);
|
|
1205
|
+
});
|
|
1206
|
+
|
|
1207
|
+
return this;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
addProduct(data = {}) {
|
|
1211
|
+
if (
|
|
1212
|
+
!(
|
|
1213
|
+
(data && typeof data === "object" && !Array.isArray(data)) ||
|
|
1214
|
+
(Array.isArray(data) &&
|
|
1215
|
+
data.every(
|
|
1216
|
+
(item) => item && typeof item === "object" && !Array.isArray(item),
|
|
1217
|
+
))
|
|
1218
|
+
)
|
|
1219
|
+
) {
|
|
1220
|
+
throw new TypeError(
|
|
1221
|
+
"Product items must be an object or an array of objects",
|
|
1222
|
+
);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
this._submessages.push({
|
|
1226
|
+
messageType: 2,
|
|
1227
|
+
messageText: "[ CANNOT_LOAD_PRODUCT - NIXEL ]",
|
|
1228
|
+
});
|
|
1229
|
+
|
|
1230
|
+
const items = Array.isArray(data) ? data : [data];
|
|
1231
|
+
|
|
1232
|
+
const product = items.map((item) => ({
|
|
1233
|
+
title: item.title,
|
|
1234
|
+
brand: item.brand,
|
|
1235
|
+
price: item.price,
|
|
1236
|
+
sale_price: item.sale_price,
|
|
1237
|
+
product_url: item.product_url ?? item.url,
|
|
1238
|
+
image: {
|
|
1239
|
+
url: item.image_url ?? item.image,
|
|
1240
|
+
},
|
|
1241
|
+
additional_images: [
|
|
1242
|
+
{
|
|
1243
|
+
url: item.icon_url ?? item.icon,
|
|
1244
|
+
},
|
|
1245
|
+
],
|
|
1246
|
+
__typename: "GenAIProductItemCardPrimitive",
|
|
1247
|
+
}));
|
|
1248
|
+
|
|
1249
|
+
this._sections.push(
|
|
1250
|
+
AIRich.newLayout(
|
|
1251
|
+
Array.isArray(data) ? "HScroll" : "Single",
|
|
1252
|
+
Array.isArray(data) ? product : product[0],
|
|
1253
|
+
),
|
|
1254
|
+
);
|
|
1255
|
+
|
|
1256
|
+
return this;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
addPost(data = {}) {
|
|
1260
|
+
if (
|
|
1261
|
+
!(
|
|
1262
|
+
(data && typeof data === "object" && !Array.isArray(data)) ||
|
|
1263
|
+
(Array.isArray(data) &&
|
|
1264
|
+
data.every(
|
|
1265
|
+
(item) => item && typeof item === "object" && !Array.isArray(item),
|
|
1266
|
+
))
|
|
1267
|
+
)
|
|
1268
|
+
) {
|
|
1269
|
+
throw new TypeError(
|
|
1270
|
+
"Post items must be an object or an array of objects",
|
|
1271
|
+
);
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
const posts = Array.isArray(data) ? data : [data];
|
|
1275
|
+
|
|
1276
|
+
this._submessages.push({
|
|
1277
|
+
messageType: 2,
|
|
1278
|
+
messageText: "[ CANNOT_LOAD_POST - NIXEL ]",
|
|
1279
|
+
});
|
|
1280
|
+
|
|
1281
|
+
const primitives = posts.map((p) => ({
|
|
1282
|
+
title: p.title ?? "",
|
|
1283
|
+
subtitle: p.subtitle ?? "",
|
|
1284
|
+
username: p.username ?? "",
|
|
1285
|
+
profile_picture_url: p.profile_picture_url ?? p.profile_url ?? "",
|
|
1286
|
+
is_verified: !!(p.is_verified || p.verified),
|
|
1287
|
+
thumbnail_url: p.thumbnail_url ?? p.thumbnail ?? "",
|
|
1288
|
+
post_caption: p.post_caption ?? p.caption ?? "",
|
|
1289
|
+
likes_count: p.likes_count ?? p.like ?? 0,
|
|
1290
|
+
comments_count: p.comments_count ?? p.comment ?? 0,
|
|
1291
|
+
shares_count: p.shares_count ?? p.share ?? 0,
|
|
1292
|
+
post_url: p.post_url ?? p.url ?? "",
|
|
1293
|
+
post_deeplink: p.post_deeplink ?? p.deeplink ?? "",
|
|
1294
|
+
source_app: p.source_app || p.source || "INSTAGRAM",
|
|
1295
|
+
footer_label: p.footer_label ?? p.footer ?? "",
|
|
1296
|
+
footer_icon: p.footer_icon ?? p.icon ?? "",
|
|
1297
|
+
is_carousel: posts.length > 1,
|
|
1298
|
+
orientation: p.orientation ?? "LANDSCAPE",
|
|
1299
|
+
post_type: p.post_type ?? "VIDEO",
|
|
1300
|
+
__typename: "GenAIPostPrimitive",
|
|
1301
|
+
}));
|
|
1302
|
+
|
|
1303
|
+
this._sections.push(AIRich.newLayout("HScroll", primitives));
|
|
1304
|
+
|
|
1305
|
+
return this;
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
addTip(text) {
|
|
1309
|
+
this._submessages.push({
|
|
1310
|
+
messageType: 2,
|
|
1311
|
+
messageText: text,
|
|
1312
|
+
});
|
|
1313
|
+
|
|
1314
|
+
this._sections.push(
|
|
1315
|
+
AIRich.newLayout("Single", {
|
|
1316
|
+
text,
|
|
1317
|
+
__typename: "GenAIMetadataTextPrimitive",
|
|
1318
|
+
}),
|
|
1319
|
+
);
|
|
1320
|
+
|
|
1321
|
+
return this;
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
addSuggest(suggestion) {
|
|
1325
|
+
if (
|
|
1326
|
+
!(
|
|
1327
|
+
typeof suggestion === "string" ||
|
|
1328
|
+
(Array.isArray(suggestion) &&
|
|
1329
|
+
suggestion.every((v) => typeof v === "string"))
|
|
1330
|
+
)
|
|
1331
|
+
) {
|
|
1332
|
+
throw new TypeError("Suggestion must be a string or array of strings");
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
const suggest = Array.isArray(suggestion)
|
|
1336
|
+
? suggestion.map((text) => ({
|
|
1337
|
+
prompt_text: text,
|
|
1338
|
+
prompt_type: "SUGGESTED_PROMPT",
|
|
1339
|
+
__typename: "GenAIFollowUpSuggestionPillPrimitive",
|
|
1340
|
+
}))
|
|
1341
|
+
: [
|
|
1342
|
+
{
|
|
1343
|
+
prompt_text: suggestion,
|
|
1344
|
+
prompt_type: "SUGGESTED_PROMPT",
|
|
1345
|
+
__typename: "GenAIFollowUpSuggestionPillPrimitive",
|
|
1346
|
+
},
|
|
1347
|
+
];
|
|
1348
|
+
|
|
1349
|
+
this._sections.push(AIRich.newLayout("ActionRow", suggest));
|
|
1350
|
+
|
|
1351
|
+
return this;
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
build({
|
|
1355
|
+
forwarded = true,
|
|
1356
|
+
includesUnifiedResponse = true,
|
|
1357
|
+
includesSubmessages = true,
|
|
1358
|
+
quoted,
|
|
1359
|
+
quotedParticipant,
|
|
1360
|
+
...options
|
|
1361
|
+
} = {}) {
|
|
1362
|
+
const forward = forwarded
|
|
1363
|
+
? {
|
|
1364
|
+
forwardingScore: 1,
|
|
1365
|
+
isForwarded: true,
|
|
1366
|
+
forwardedAiBotMessageInfo: { botJid: "0@bot" },
|
|
1367
|
+
forwardOrigin: 4,
|
|
1368
|
+
}
|
|
1369
|
+
: {};
|
|
1370
|
+
|
|
1371
|
+
const qObj = quoted
|
|
1372
|
+
? {
|
|
1373
|
+
stanzaId: quoted?.key?.id || quoted?.id,
|
|
1374
|
+
participant:
|
|
1375
|
+
quotedParticipant ||
|
|
1376
|
+
quoted?.key?.participant ||
|
|
1377
|
+
quoted?.key?.remoteJid,
|
|
1378
|
+
quotedType: 0,
|
|
1379
|
+
quotedMessage:
|
|
1380
|
+
typeof quoted === "object" && quoted !== null
|
|
1381
|
+
? (quoted.message ?? quoted)
|
|
1382
|
+
: undefined,
|
|
1383
|
+
}
|
|
1384
|
+
: {};
|
|
1385
|
+
|
|
1386
|
+
const sections = this._footer
|
|
1387
|
+
? [
|
|
1388
|
+
...this._sections,
|
|
1389
|
+
AIRich.newLayout("Single", {
|
|
1390
|
+
text: this._footer,
|
|
1391
|
+
__typename: "GenAIMetadataTextPrimitive",
|
|
1392
|
+
}),
|
|
1393
|
+
]
|
|
1394
|
+
: [...this._sections];
|
|
1395
|
+
|
|
1396
|
+
return {
|
|
1397
|
+
messageContextInfo: {
|
|
1398
|
+
deviceListMetadata: {},
|
|
1399
|
+
deviceListMetadataVersion: 2,
|
|
1400
|
+
botMetadata: {
|
|
1401
|
+
messageDisclaimerText: this._title,
|
|
1402
|
+
richResponseSourcesMetadata: { sources: this._richResponseSources },
|
|
1403
|
+
},
|
|
1404
|
+
},
|
|
1405
|
+
...this._extraPayload,
|
|
1406
|
+
botForwardedMessage: {
|
|
1407
|
+
message: {
|
|
1408
|
+
richResponseMessage: {
|
|
1409
|
+
messageType: 1,
|
|
1410
|
+
submessages: includesSubmessages ? this._submessages : [],
|
|
1411
|
+
unifiedResponse: {
|
|
1412
|
+
data: includesUnifiedResponse
|
|
1413
|
+
? Buffer.from(
|
|
1414
|
+
JSON.stringify({
|
|
1415
|
+
response_id: crypto.randomUUID(),
|
|
1416
|
+
sections,
|
|
1417
|
+
}),
|
|
1418
|
+
).toString("base64")
|
|
1419
|
+
: "",
|
|
1420
|
+
},
|
|
1421
|
+
contextInfo: {
|
|
1422
|
+
...forward,
|
|
1423
|
+
...qObj,
|
|
1424
|
+
...this._contextInfo,
|
|
1425
|
+
},
|
|
1426
|
+
},
|
|
1427
|
+
},
|
|
1428
|
+
},
|
|
1429
|
+
};
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
async send(
|
|
1433
|
+
jid,
|
|
1434
|
+
{
|
|
1435
|
+
forwarded,
|
|
1436
|
+
includesUnifiedResponse,
|
|
1437
|
+
includesSubmessages,
|
|
1438
|
+
...options
|
|
1439
|
+
} = {},
|
|
1440
|
+
) {
|
|
1441
|
+
const msg = this.build({ forwarded, includesUnifiedResponse, ...options });
|
|
1442
|
+
|
|
1443
|
+
return await this.#client.relayMessage(jid, msg, { ...options });
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
static tokenizer(code, lang = "javascript") {
|
|
1447
|
+
const keywordsMap = {
|
|
1448
|
+
javascript: new Set([
|
|
1449
|
+
"break",
|
|
1450
|
+
"case",
|
|
1451
|
+
"catch",
|
|
1452
|
+
"continue",
|
|
1453
|
+
"debugger",
|
|
1454
|
+
"delete",
|
|
1455
|
+
"do",
|
|
1456
|
+
"else",
|
|
1457
|
+
"finally",
|
|
1458
|
+
"for",
|
|
1459
|
+
"function",
|
|
1460
|
+
"if",
|
|
1461
|
+
"in",
|
|
1462
|
+
"instanceof",
|
|
1463
|
+
"new",
|
|
1464
|
+
"return",
|
|
1465
|
+
"switch",
|
|
1466
|
+
"this",
|
|
1467
|
+
"throw",
|
|
1468
|
+
"try",
|
|
1469
|
+
"typeof",
|
|
1470
|
+
"var",
|
|
1471
|
+
"void",
|
|
1472
|
+
"while",
|
|
1473
|
+
"with",
|
|
1474
|
+
"true",
|
|
1475
|
+
"false",
|
|
1476
|
+
"null",
|
|
1477
|
+
"undefined",
|
|
1478
|
+
"class",
|
|
1479
|
+
"const",
|
|
1480
|
+
"let",
|
|
1481
|
+
"super",
|
|
1482
|
+
"extends",
|
|
1483
|
+
"export",
|
|
1484
|
+
"import",
|
|
1485
|
+
"yield",
|
|
1486
|
+
"static",
|
|
1487
|
+
"constructor",
|
|
1488
|
+
"async",
|
|
1489
|
+
"await",
|
|
1490
|
+
"get",
|
|
1491
|
+
"set",
|
|
1492
|
+
]),
|
|
1493
|
+
};
|
|
1494
|
+
|
|
1495
|
+
const TYPE_MAP = {
|
|
1496
|
+
0: "DEFAULT",
|
|
1497
|
+
1: "KEYWORD",
|
|
1498
|
+
2: "METHOD",
|
|
1499
|
+
3: "STR",
|
|
1500
|
+
4: "NUMBER",
|
|
1501
|
+
5: "COMMENT",
|
|
1502
|
+
};
|
|
1503
|
+
|
|
1504
|
+
const keywords = keywordsMap[lang] || new Set();
|
|
1505
|
+
const tokens = [];
|
|
1506
|
+
|
|
1507
|
+
let i = 0;
|
|
1508
|
+
|
|
1509
|
+
const push = (content, type) => {
|
|
1510
|
+
if (!content) return;
|
|
1511
|
+
const last = tokens[tokens.length - 1];
|
|
1512
|
+
if (last && last.highlightType === type) last.codeContent += content;
|
|
1513
|
+
else tokens.push({ codeContent: content, highlightType: type });
|
|
1514
|
+
};
|
|
1515
|
+
|
|
1516
|
+
while (i < code.length) {
|
|
1517
|
+
const c = code[i];
|
|
1518
|
+
|
|
1519
|
+
if (/\s/.test(c)) {
|
|
1520
|
+
let s = i;
|
|
1521
|
+
while (i < code.length && /\s/.test(code[i])) i++;
|
|
1522
|
+
push(code.slice(s, i), 0);
|
|
1523
|
+
continue;
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
if (c === "/" && code[i + 1] === "/") {
|
|
1527
|
+
let s = i;
|
|
1528
|
+
i += 2;
|
|
1529
|
+
while (i < code.length && code[i] !== "\n") i++;
|
|
1530
|
+
push(code.slice(s, i), 5);
|
|
1531
|
+
continue;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
1535
|
+
let s = i;
|
|
1536
|
+
const q = c;
|
|
1537
|
+
i++;
|
|
1538
|
+
while (i < code.length) {
|
|
1539
|
+
if (code[i] === "\\" && i + 1 < code.length) i += 2;
|
|
1540
|
+
else if (code[i] === q) {
|
|
1541
|
+
i++;
|
|
1542
|
+
break;
|
|
1543
|
+
} else i++;
|
|
1544
|
+
}
|
|
1545
|
+
push(code.slice(s, i), 3);
|
|
1546
|
+
continue;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
if (/[0-9]/.test(c)) {
|
|
1550
|
+
let s = i;
|
|
1551
|
+
while (i < code.length && /[0-9.]/.test(code[i])) i++;
|
|
1552
|
+
push(code.slice(s, i), 4);
|
|
1553
|
+
continue;
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
if (/[a-zA-Z_$]/.test(c)) {
|
|
1557
|
+
let s = i;
|
|
1558
|
+
while (i < code.length && /[a-zA-Z0-9_$]/.test(code[i])) i++;
|
|
1559
|
+
const word = code.slice(s, i);
|
|
1560
|
+
|
|
1561
|
+
let type = 0;
|
|
1562
|
+
if (keywords.has(word)) type = 1;
|
|
1563
|
+
else {
|
|
1564
|
+
let j = i;
|
|
1565
|
+
while (j < code.length && /\s/.test(code[j])) j++;
|
|
1566
|
+
if (code[j] === "(") type = 2;
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
push(word, type);
|
|
1570
|
+
continue;
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
push(c, 0);
|
|
1574
|
+
i++;
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
return {
|
|
1578
|
+
codeBlock: tokens,
|
|
1579
|
+
unified_codeBlock: tokens.map((t) => ({
|
|
1580
|
+
content: t.codeContent,
|
|
1581
|
+
type: TYPE_MAP[t.highlightType],
|
|
1582
|
+
})),
|
|
1583
|
+
};
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
static toTableMetadata(arr) {
|
|
1587
|
+
if (
|
|
1588
|
+
!Array.isArray(arr) ||
|
|
1589
|
+
!arr.every(
|
|
1590
|
+
(row) =>
|
|
1591
|
+
Array.isArray(row) && row.every((cell) => typeof cell === "string"),
|
|
1592
|
+
)
|
|
1593
|
+
) {
|
|
1594
|
+
throw new TypeError("Table must be a nested array of strings");
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
const [header, ...rows] = arr;
|
|
1598
|
+
|
|
1599
|
+
const maxLen = Math.max(header.length, ...rows.map((r) => r.length));
|
|
1600
|
+
|
|
1601
|
+
const normalize = (r) => [...r, ...Array(maxLen - r.length).fill("")];
|
|
1602
|
+
|
|
1603
|
+
const unified_rows = [
|
|
1604
|
+
{
|
|
1605
|
+
is_header: true,
|
|
1606
|
+
cells: normalize(header),
|
|
1607
|
+
},
|
|
1608
|
+
...rows.map((r) => ({
|
|
1609
|
+
is_header: false,
|
|
1610
|
+
cells: normalize(r),
|
|
1611
|
+
})),
|
|
1612
|
+
];
|
|
1613
|
+
|
|
1614
|
+
const rowsMeta = unified_rows.map((r) => ({
|
|
1615
|
+
items: r.cells,
|
|
1616
|
+
...(r.is_header ? { isHeading: true } : {}),
|
|
1617
|
+
}));
|
|
1618
|
+
|
|
1619
|
+
return {
|
|
1620
|
+
title: "",
|
|
1621
|
+
rows: rowsMeta,
|
|
1622
|
+
unified_rows,
|
|
1623
|
+
};
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
export { VERSION, Button, ButtonV2, Carousel, AIRich };
|