@fvckerss/baileys 1.0.12 → 1.0.14

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.
@@ -1,432 +1,1331 @@
1
- /**
2
- * Lia@Changes 09-04-26 [WIP]
3
- * Adds support for tables and code blocks with richResponseMessage (wrapped inside botForwardedMessage).
4
- *
5
- * If you use or copy this code, please credit my name or project. AND DO NOT CHANGE THIS NOTE
6
- * @itsliaaa/baileys
7
- */
8
- import { getRandomValues, randomUUID } from 'crypto';
9
- import { DONATE_URL, LEXER_REGEX } from '../Defaults/index.js';
10
- import { LANGUAGE_KEYWORDS } from '../WABinary/constants.js';
11
- import { CodeHighlightType, RichSubMessageType } from '../Types/RichType.js';
12
- import { proto } from '../../WAProto/index.js';
13
- import { unixTimestampSeconds } from './generics.js';
1
+ import {
2
+ getRandomValues,
3
+ randomBytes,
4
+ randomUUID
5
+ } from 'crypto';
6
+
7
+ import {
8
+ DONATE_URL,
9
+ LEXER_REGEX
10
+ } from '../Defaults/index.js';
11
+
12
+ import {
13
+ LANGUAGE_KEYWORDS
14
+ } from '../WABinary/constants.js';
15
+
16
+ import {
17
+ CodeHighlightType,
18
+ RichSubMessageType
19
+ } from '../Types/RichType.js';
20
+
21
+ import {
22
+ proto
23
+ } from '../../WAProto/index.js';
24
+
14
25
  const NOOP = new Set([]);
15
- export const tokenizeCode = (code, language = 'javascript') => {
16
- const keywords = LANGUAGE_KEYWORDS[language] || NOOP;
26
+
27
+
28
+ /* =========================================================
29
+ * CODE TOKENIZER
30
+ * ========================================================= */
31
+
32
+ export const tokenizeCode = (
33
+ code,
34
+ language = 'javascript'
35
+ ) => {
36
+
37
+ const keywords =
38
+ LANGUAGE_KEYWORDS[language] || NOOP;
39
+
17
40
  const blocks = [];
41
+
18
42
  LEXER_REGEX.lastIndex = 0;
43
+
19
44
  let match;
20
- while ((match = LEXER_REGEX.exec(code)) !== null) {
45
+
46
+ while (
47
+ (match = LEXER_REGEX.exec(code)) !== null
48
+ ) {
49
+
21
50
  if (match[1]) {
22
- blocks.push({ highlightType: CodeHighlightType.COMMENT, codeContent: match[1] });
23
- }
24
- else if (match[2]) {
25
- blocks.push({ highlightType: CodeHighlightType.STRING, codeContent: match[2] });
26
- }
27
- else if (match[3]) {
51
+
28
52
  blocks.push({
29
- highlightType: keywords.has(match[3]) ? CodeHighlightType.KEYWORD : CodeHighlightType.METHOD,
30
- codeContent: match[3],
53
+ highlightType:
54
+ CodeHighlightType.COMMENT,
55
+
56
+ codeContent:
57
+ match[1]
31
58
  });
32
- }
33
- else if (match[4]) {
59
+
60
+ } else if (match[2]) {
61
+
34
62
  blocks.push({
35
- highlightType: keywords.has(match[4]) ? CodeHighlightType.KEYWORD : CodeHighlightType.DEFAULT,
36
- codeContent: match[4],
63
+ highlightType:
64
+ CodeHighlightType.STRING,
65
+
66
+ codeContent:
67
+ match[2]
68
+ });
69
+
70
+ } else if (match[3]) {
71
+
72
+ blocks.push({
73
+ highlightType:
74
+ keywords.has(match[3])
75
+ ? CodeHighlightType.KEYWORD
76
+ : CodeHighlightType.METHOD,
77
+
78
+ codeContent:
79
+ match[3]
80
+ });
81
+
82
+ } else if (match[4]) {
83
+
84
+ blocks.push({
85
+ highlightType:
86
+ keywords.has(match[4])
87
+ ? CodeHighlightType.KEYWORD
88
+ : CodeHighlightType.DEFAULT,
89
+
90
+ codeContent:
91
+ match[4]
92
+ });
93
+
94
+ } else if (match[5]) {
95
+
96
+ blocks.push({
97
+ highlightType:
98
+ CodeHighlightType.NUMBER,
99
+
100
+ codeContent:
101
+ match[5]
102
+ });
103
+
104
+ } else {
105
+
106
+ blocks.push({
107
+ highlightType:
108
+ CodeHighlightType.DEFAULT,
109
+
110
+ codeContent:
111
+ match[6]
37
112
  });
38
- }
39
- else if (match[5]) {
40
- blocks.push({ highlightType: CodeHighlightType.NUMBER, codeContent: match[5] });
41
- }
42
- else {
43
- blocks.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: match[6] });
44
113
  }
45
114
  }
115
+
46
116
  return blocks;
47
117
  };
48
- // Lia@Changes 09-04-26 --- Inject buffer into unifiedResponse.data to support proper rendering of rich messages (ex: tables and code blocks)
49
- export const toUnified = (submessages, uuid) => ({
50
- response_id: uuid || randomUUID(),
51
- sections: submessages.map((submessage, index) => {
118
+
119
+ export const toUnified = (
120
+ submessages,
121
+ uuid,
122
+ footerText
123
+ ) => {
124
+
125
+ const sections = [];
126
+
127
+ for (const submessage of submessages) {
128
+
52
129
  switch (submessage.messageType) {
53
- case RichSubMessageType.CODE:
54
- const codeMetadata = submessage.codeMetadata;
55
- return {
130
+
131
+ /* =============================================
132
+ * CODE
133
+ * ============================================= */
134
+
135
+ case RichSubMessageType.CODE: {
136
+
137
+ const codeMetadata =
138
+ submessage.codeMetadata;
139
+
140
+ sections.push({
56
141
  view_model: {
57
142
  primitive: {
58
- language: codeMetadata.codeLanguage,
59
- code_blocks: codeMetadata.codeBlocks.map((block) => ({ content: block.codeContent, type: CodeHighlightType[block.highlightType] })),
60
- __typename: 'GenAICodeUXPrimitive'
143
+ language:
144
+ codeMetadata.codeLanguage,
145
+
146
+ code_blocks:
147
+ codeMetadata.codeBlocks.map(
148
+ (block) => ({
149
+ content:
150
+ block.codeContent,
151
+
152
+ type:
153
+ CodeHighlightType[
154
+ block.highlightType
155
+ ]
156
+ })
157
+ ),
158
+
159
+ __typename:
160
+ 'GenAICodeUXPrimitive'
61
161
  },
62
- __typename: 'GenAISingleLayoutViewModel'
162
+
163
+ __typename:
164
+ 'GenAISingleLayoutViewModel'
63
165
  }
64
- };
65
- case RichSubMessageType.CONTENT_ITEMS: {
66
- const meta = submessage.contentItemsMetadata
67
- const first = meta.itemsMetadata?.[0]
166
+ });
68
167
 
69
- if (first?.__typename === "GenAIProductItemCardPrimitive") {
70
- return {
71
- view_model: {
72
- primitive: first,
73
- __typename: "GenAISingleLayoutViewModel"
168
+ break;
74
169
  }
75
- }
76
- }
77
170
 
78
- if (first?.__typename === "GenAIFollowUpSuggestionPillPrimitive") {
79
- return {
80
- view_model: {
81
- primitives: meta.itemsMetadata,
82
- __typename: "GenAIActionRowLayoutViewModel"
83
- }
84
- }
85
- }
86
171
 
87
- return {}
88
- }
89
- case RichSubMessageType.INLINE_IMAGE:
90
- return {};
91
- case RichSubMessageType.LATEX:
92
- return {};
93
- case RichSubMessageType.TABLE:
94
- const tableMetadata = submessage.tableMetadata;
95
- return {
172
+ /* =============================================
173
+ * TEXT
174
+ * ============================================= */
175
+
176
+ case RichSubMessageType.TEXT: {
177
+
178
+ sections.push({
96
179
  view_model: {
97
180
  primitive: {
98
- title: tableMetadata.title,
99
- rows: tableMetadata.rows.map((row) => ({
100
- is_header: row.isHeading,
101
- cells: row.items,
102
- markdown_cells: row.items.map((item) => ({ text: item }))
103
- })),
104
- __typename: 'GenATableUXPrimitive'
181
+ text:
182
+ submessage.messageText,
183
+
184
+ inline_entities:
185
+ submessage.inlineEntities || [],
186
+
187
+ __typename:
188
+ 'GenAIMarkdownTextUXPrimitive'
105
189
  },
106
- __typename: 'GenAISingleLayoutViewModel'
190
+
191
+ __typename:
192
+ 'GenAISingleLayoutViewModel'
107
193
  }
108
- };
109
- case RichSubMessageType.TEXT:
110
- return {
194
+ });
195
+
196
+ break;
197
+ }
198
+
199
+
200
+ /* =============================================
201
+ * CONTENT ITEMS
202
+ * ============================================= */
203
+
204
+ case RichSubMessageType.CONTENT_ITEMS: {
205
+
206
+ const meta =
207
+ submessage.contentItemsMetadata;
208
+
209
+ const items =
210
+ meta?.itemsMetadata || [];
211
+
212
+ if (!items.length) {
213
+ break;
214
+ }
215
+
216
+ const first =
217
+ items[0];
218
+
219
+
220
+ /* -----------------------------------------
221
+ * PRODUCT
222
+ * ----------------------------------------- */
223
+
224
+ if (
225
+ first?.__typename ===
226
+ 'GenAIProductItemCardPrimitive'
227
+ ) {
228
+
229
+ sections.push({
230
+ view_model: {
231
+ primitive:
232
+ first,
233
+
234
+ __typename:
235
+ 'GenAISingleLayoutViewModel'
236
+ }
237
+ });
238
+
239
+ break;
240
+ }
241
+
242
+
243
+ /* -----------------------------------------
244
+ * SUGGESTED
245
+ * ----------------------------------------- */
246
+
247
+ if (
248
+ first?.__typename ===
249
+ 'GenAIFollowUpSuggestionPillPrimitive'
250
+ ) {
251
+
252
+ sections.push({
253
+ view_model: {
254
+ primitives:
255
+ items,
256
+
257
+ __typename:
258
+ 'GenAIActionRowLayoutViewModel'
259
+ }
260
+ });
261
+
262
+ break;
263
+ }
264
+
265
+ break;
266
+ }
267
+
268
+
269
+ /* =============================================
270
+ * TABLE
271
+ * ============================================= */
272
+
273
+ case RichSubMessageType.TABLE: {
274
+
275
+ const tableMetadata =
276
+ submessage.tableMetadata;
277
+
278
+ sections.push({
111
279
  view_model: {
112
280
  primitive: {
113
- text: submessage.messageText,
114
- inline_entities: submessage.inlineEntities || [],
115
- __typename: 'GenAIMarkdownTextUXPrimitive'
281
+
282
+ title:
283
+ tableMetadata.title,
284
+
285
+ rows:
286
+ tableMetadata.rows.map(
287
+ (row) => ({
288
+ is_header:
289
+ row.isHeading,
290
+
291
+ cells:
292
+ row.items,
293
+
294
+ markdown_cells:
295
+ row.items.map(
296
+ (item) => ({
297
+ text:
298
+ item
299
+ })
300
+ )
301
+ })
302
+ ),
303
+
304
+ __typename:
305
+ 'GenATableUXPrimitive'
116
306
  },
117
- __typename: 'GenAISingleLayoutViewModel'
307
+
308
+ __typename:
309
+ 'GenAISingleLayoutViewModel'
118
310
  }
119
- };
120
- case RichSubMessageType.METADATA_TEXT:
121
- return {
122
- view_model: {
123
- primitive: {
124
- text: submessage.messageText,
125
- __typename: "GenAIMetadataTextPrimitive"
126
- },
127
- __typename: "GenAISingleLayoutViewModel"
311
+ });
312
+
313
+ break;
314
+ }
315
+
316
+ case RichSubMessageType.INLINE_IMAGE:
317
+ case RichSubMessageType.LATEX:
318
+ default:
319
+ break;
128
320
  }
129
321
  }
130
- }
131
- return submessage;
132
- })
133
- });
134
- export const prepareRichResponseMessage = (content) => {
135
- const { alignment, code, contentText, disclaimerText, footerText, headerText, imageText, inlineImage, inlineVideo, items, language, latex, links, noHeading, posts, products, product, suggested, richResponse, table, tapLinkUrl, title } = content;
136
- let submessages = [];
137
- if (Array.isArray(richResponse)) {
138
- submessages = richResponse.map((submessage) => {
139
- if (submessage.text) {
140
- return {
141
- messageType: RichSubMessageType.TEXT,
142
- messageText: submessage.text,
143
- inlineEntities: submessage.inlineEntities
144
- };
322
+
323
+ if (
324
+ typeof footerText === 'string' &&
325
+ footerText.trim()
326
+ ) {
327
+
328
+ sections.push({
329
+ view_model: {
330
+ primitive: {
331
+ text:
332
+ footerText,
333
+
334
+ __typename:
335
+ 'GenAIMarkdownTextUXPrimitive'
336
+ },
337
+
338
+ __typename:
339
+ 'GenAISingleLayoutViewModel'
145
340
  }
146
- else if (submessage.code) {
147
- return {
148
- messageType: RichSubMessageType.CODE,
149
- codeMetadata: {
150
- codeLanguage: submessage.language,
151
- codeBlocks: submessage.code
341
+ });
342
+ }
343
+
344
+
345
+ return {
346
+ response_id:
347
+ uuid || randomUUID(),
348
+
349
+ sections
350
+ };
351
+ };
352
+
353
+
354
+ /* =========================================================
355
+ * PREPARE RICH RESPONSE MESSAGE
356
+ * ========================================================= */
357
+
358
+ export const prepareRichResponseMessage = (
359
+ content
360
+ ) => {
361
+
362
+ const {
363
+ alignment,
364
+ code,
365
+ contentText,
366
+ disclaimerText,
367
+ footerText,
368
+ headerText,
369
+ imageText,
370
+ inlineImage,
371
+ inlineVideo,
372
+ items,
373
+ language,
374
+ latex,
375
+ links,
376
+ noHeading,
377
+ posts,
378
+ products,
379
+ product,
380
+ suggested,
381
+ richResponse,
382
+ table,
383
+ tapLinkUrl,
384
+ title
385
+ } = content;
386
+
387
+
388
+ /* =====================================================
389
+ * PRODUCT / PRODUCTS
390
+ * ===================================================== */
391
+
392
+ const dataProduct =
393
+ products || product;
394
+
395
+
396
+ let submessages = [];
397
+
398
+
399
+ /* =====================================================
400
+ * RICH RESPONSE ARRAY
401
+ * ===================================================== */
402
+
403
+ if (
404
+ Array.isArray(richResponse)
405
+ ) {
406
+
407
+ submessages =
408
+ richResponse.map(
409
+ (submessage) => {
410
+
411
+ /* =====================================
412
+ * TEXT
413
+ * ===================================== */
414
+
415
+ if (
416
+ submessage.text
417
+ ) {
418
+
419
+ return {
420
+ messageType:
421
+ RichSubMessageType.TEXT,
422
+
423
+ messageText:
424
+ submessage.text,
425
+
426
+ inlineEntities:
427
+ submessage.inlineEntities
428
+ };
152
429
  }
153
- };
154
- }
155
- else if (submessage.items) {
156
- return {
157
- messageType: RichSubMessageType.CONTENT_ITEMS,
158
- contentItemsMetadata: {
159
- itemsMetadata: submessage.items,
160
- contentType: proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL
430
+
431
+
432
+ /* =====================================
433
+ * CODE
434
+ * ===================================== */
435
+
436
+ if (
437
+ submessage.code
438
+ ) {
439
+
440
+ const codeLanguage =
441
+ submessage.language ||
442
+ 'javascript';
443
+
444
+ return {
445
+ messageType:
446
+ RichSubMessageType.CODE,
447
+
448
+ codeMetadata: {
449
+
450
+ codeLanguage,
451
+
452
+ codeBlocks:
453
+ Array.isArray(
454
+ submessage.code
455
+ )
456
+ ? submessage.code
457
+ : tokenizeCode(
458
+ submessage.code,
459
+ codeLanguage
460
+ )
461
+ }
462
+ };
161
463
  }
162
- };
163
- }
164
- else if (submessage.inlineImage) {
165
- return {
166
- messageType: RichSubMessageType.INLINE_IMAGE,
167
- imageMetadata: {
168
- imageUrl: submessage.inlineImage,
169
- imageText: submessage.imageText,
170
- alignment: submessage.alignment,
171
- tapLinkUrl: submessage.tapLinkUrl
464
+
465
+
466
+ /* =====================================
467
+ * ITEMS
468
+ * ===================================== */
469
+
470
+ if (
471
+ submessage.items
472
+ ) {
473
+
474
+ return {
475
+ messageType:
476
+ RichSubMessageType.CONTENT_ITEMS,
477
+
478
+ contentItemsMetadata: {
479
+
480
+ itemsMetadata:
481
+ submessage.items,
482
+
483
+ contentType:
484
+ proto
485
+ .AIRichResponseContentItemsMetadata
486
+ .ContentType
487
+ .CAROUSEL
488
+ }
489
+ };
172
490
  }
173
- };
174
- }
175
- else if (submessage.inlineVideo) {
176
- return {
177
- messageType: RichSubMessageType.TEXT,
178
- messageText: 'INLINE_VIDEO'
179
- };
180
- }
181
- else if (submessage.latex) {
182
- return {
183
- messageType: RichSubMessageType.LATEX,
184
- latexMetadata: {
185
- text: submessage.text,
186
- expressions: submessage.latex
491
+
492
+
493
+ /* =====================================
494
+ * INLINE IMAGE
495
+ * ===================================== */
496
+
497
+ if (
498
+ submessage.inlineImage
499
+ ) {
500
+
501
+ return {
502
+ messageType:
503
+ RichSubMessageType.INLINE_IMAGE,
504
+
505
+ imageMetadata: {
506
+
507
+ imageUrl:
508
+ submessage.inlineImage,
509
+
510
+ imageText:
511
+ submessage.imageText,
512
+
513
+ alignment:
514
+ submessage.alignment,
515
+
516
+ tapLinkUrl:
517
+ submessage.tapLinkUrl
518
+ }
519
+ };
187
520
  }
188
- };
189
- }
190
- else if (submessage.posts) {
191
- return {
192
- messageType: RichSubMessageType.TEXT,
193
- messageText: 'POSTS'
194
- };
195
- }
196
- else if (submessage.products) {
197
- return {
198
- messageType: RichSubMessageType.PRODUCT,
199
- productMetadata: {
200
- products: submessage.products,
201
- layout: submessage.layout || 'HScroll'
202
- }
203
- };
204
- }
205
- else if (submessage.suggested) {
206
- return {
207
- messageType: RichSubMessageType.TEXT,
208
- messageText: 'SUGGESTED_PROMPT'
209
- };
210
- }
211
- else if (submessage.table) {
212
- return {
213
- messageType: RichSubMessageType.TABLE,
214
- tableMetadata: {
215
- title: submessage.title,
216
- rows: submessage.table
521
+
522
+
523
+ /* =====================================
524
+ * INLINE VIDEO
525
+ * ===================================== */
526
+
527
+ if (
528
+ submessage.inlineVideo
529
+ ) {
530
+
531
+ return {
532
+ messageType:
533
+ RichSubMessageType.TEXT,
534
+
535
+ messageText:
536
+ 'INLINE_VIDEO'
537
+ };
217
538
  }
218
- };
219
- }
220
- return submessage;
221
- });
539
+
540
+
541
+ /* =====================================
542
+ * LATEX
543
+ * ===================================== */
544
+
545
+ if (
546
+ submessage.latex
547
+ ) {
548
+
549
+ return {
550
+ messageType:
551
+ RichSubMessageType.LATEX,
552
+
553
+ latexMetadata: {
554
+
555
+ text:
556
+ submessage.text,
557
+
558
+ expressions:
559
+ submessage.latex
560
+ }
561
+ };
562
+ }
563
+
564
+
565
+ /* =====================================
566
+ * POSTS
567
+ * ===================================== */
568
+
569
+ if (
570
+ submessage.posts
571
+ ) {
572
+
573
+ return {
574
+ messageType:
575
+ RichSubMessageType.TEXT,
576
+
577
+ messageText:
578
+ 'POSTS'
579
+ };
580
+ }
581
+
582
+
583
+ /* =====================================
584
+ * SUGGESTED
585
+ * ===================================== */
586
+
587
+ if (
588
+ submessage.suggested
589
+ ) {
590
+
591
+ return {
592
+ messageType:
593
+ RichSubMessageType.CONTENT_ITEMS,
594
+
595
+ contentItemsMetadata: {
596
+
597
+ itemsMetadata:
598
+ submessage.suggested.map(
599
+ (text) => ({
600
+ prompt_text:
601
+ text,
602
+
603
+ prompt_type:
604
+ 'SUGGESTED_PROMPT',
605
+
606
+ __typename:
607
+ 'GenAIFollowUpSuggestionPillPrimitive'
608
+ })
609
+ )
610
+ }
611
+ };
612
+ }
613
+
614
+
615
+ /* =====================================
616
+ * TABLE
617
+ * ===================================== */
618
+
619
+ if (
620
+ submessage.table
621
+ ) {
622
+
623
+ return {
624
+ messageType:
625
+ RichSubMessageType.TABLE,
626
+
627
+ tableMetadata: {
628
+
629
+ title:
630
+ submessage.title,
631
+
632
+ rows:
633
+ submessage.table
634
+ }
635
+ };
636
+ }
637
+
638
+
639
+ return submessage;
640
+ }
641
+ );
222
642
  }
643
+
644
+
645
+ /* =====================================================
646
+ * NORMAL OBJECT
647
+ * ===================================================== */
648
+
223
649
  else {
224
- if (headerText) {
650
+
651
+ /* ================================================
652
+ * HEADER
653
+ * ================================================ */
654
+
655
+ if (
656
+ headerText
657
+ ) {
658
+
225
659
  submessages.push({
226
- messageType: RichSubMessageType.TEXT,
227
- messageText: headerText
660
+
661
+ messageType:
662
+ RichSubMessageType.TEXT,
663
+
664
+ messageText:
665
+ headerText
228
666
  });
229
667
  }
230
- if (contentText) {
668
+
669
+
670
+ /* ================================================
671
+ * CONTENT
672
+ * ================================================ */
673
+
674
+ if (
675
+ contentText
676
+ ) {
677
+
231
678
  submessages.push({
232
- messageType: RichSubMessageType.TEXT,
233
- messageText: contentText
679
+
680
+ messageType:
681
+ RichSubMessageType.TEXT,
682
+
683
+ messageText:
684
+ contentText
234
685
  });
235
686
  }
236
- if (code) {
237
- language ||= 'javascript';
687
+
688
+
689
+ /* ================================================
690
+ * CODE
691
+ * ================================================ */
692
+
693
+ if (
694
+ code
695
+ ) {
696
+
697
+ const codeLanguage =
698
+ language || 'javascript';
699
+
238
700
  submessages.push({
239
- messageType: RichSubMessageType.CODE,
701
+
702
+ messageType:
703
+ RichSubMessageType.CODE,
704
+
240
705
  codeMetadata: {
241
- codeLanguage: language,
242
- codeBlocks: tokenizeCode(code, language)
706
+
707
+ codeLanguage,
708
+
709
+ codeBlocks:
710
+ tokenizeCode(
711
+ code,
712
+ codeLanguage
713
+ )
243
714
  }
244
715
  });
245
716
  }
246
- if (items) {
717
+
718
+
719
+ /* ================================================
720
+ * ITEMS
721
+ * ================================================ */
722
+
723
+ if (
724
+ items
725
+ ) {
726
+
247
727
  submessages.push({
248
- messageType: RichSubMessageType.CONTENT_ITEMS,
728
+
729
+ messageType:
730
+ RichSubMessageType.CONTENT_ITEMS,
731
+
249
732
  contentItemsMetadata: {
250
- itemsMetadata: items,
251
- contentType: proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL
733
+
734
+ itemsMetadata:
735
+ items,
736
+
737
+ contentType:
738
+ proto
739
+ .AIRichResponseContentItemsMetadata
740
+ .ContentType
741
+ .CAROUSEL
252
742
  }
253
743
  });
254
744
  }
255
- if (inlineImage) {
745
+
746
+
747
+ /* ================================================
748
+ * INLINE IMAGE
749
+ * ================================================ */
750
+
751
+ if (
752
+ inlineImage
753
+ ) {
754
+
256
755
  submessages.push({
257
- messageType: RichSubMessageType.INLINE_IMAGE,
756
+
757
+ messageType:
758
+ RichSubMessageType.INLINE_IMAGE,
759
+
258
760
  imageMetadata: {
259
- imageUrl: inlineImage,
260
- imageText,
261
- alignment,
262
- tapLinkUrl
761
+
762
+ imageUrl:
763
+ inlineImage,
764
+
765
+ imageText:
766
+ imageText,
767
+
768
+ alignment:
769
+ alignment,
770
+
771
+ tapLinkUrl:
772
+ tapLinkUrl
263
773
  }
264
774
  });
265
775
  }
266
- if (inlineVideo) {
776
+
777
+
778
+ /* ================================================
779
+ * INLINE VIDEO
780
+ * ================================================ */
781
+
782
+ if (
783
+ inlineVideo
784
+ ) {
785
+
267
786
  submessages.push({
268
- messageType: RichSubMessageType.TEXT,
269
- messageText: 'INLINE_VIDEO'
787
+
788
+ messageType:
789
+ RichSubMessageType.TEXT,
790
+
791
+ messageText:
792
+ 'INLINE_VIDEO'
270
793
  });
271
794
  }
272
- if (latex) {
795
+
796
+
797
+ /* ================================================
798
+ * LATEX
799
+ * ================================================ */
800
+
801
+ if (
802
+ latex
803
+ ) {
804
+
273
805
  submessages.push({
274
- messageType: RichSubMessageType.LATEX,
806
+
807
+ messageType:
808
+ RichSubMessageType.LATEX,
809
+
275
810
  latexMetadata: {
276
- text,
277
- expressions: latex
811
+
812
+ text:
813
+ contentText,
814
+
815
+ expressions:
816
+ latex
278
817
  }
279
818
  });
280
819
  }
281
- if (links) {
282
- links.forEach((linkField, index) => {
283
- const prefix = 'SS_' + index;
284
- const url = linkField.url || DONATE_URL;
285
- const sources = linkField.sources?.map((sourceField) => ({
286
- source_type: 'THIRD_PARTY',
287
- source_display_name: sourceField.displayName || 'Donate',
288
- source_subtitle: sourceField.subtitle || 'Saweria',
289
- source_url: sourceField.url || url
290
- }));
291
- submessages.push({
292
- messageType: RichSubMessageType.TEXT,
293
- messageText: linkField.text + ` {{${prefix}}}¹{{/${prefix}}} `,
294
- inlineEntities: [{
295
- key: prefix,
296
- metadata: {
297
- reference_id: index + 1,
298
- reference_url: url,
299
- reference_title: linkField.title || 'For Donation via Saweria',
300
- reference_display_name: linkField.displayName || 'Donation',
301
- sources: sources || [],
302
- __typename: 'GenAISearchCitationItem'
820
+
821
+
822
+ /* ================================================
823
+ * LINKS
824
+ * ================================================ */
825
+
826
+ if (
827
+ links
828
+ ) {
829
+
830
+ links.forEach(
831
+ (linkField, index) => {
832
+
833
+ const prefix =
834
+ 'SS_' + index;
835
+
836
+ const url =
837
+ linkField.url ||
838
+ DONATE_URL;
839
+
840
+ const sources =
841
+ linkField.sources?.map(
842
+ (sourceField) => ({
843
+
844
+ source_type:
845
+ 'THIRD_PARTY',
846
+
847
+ source_display_name:
848
+ sourceField.displayName ||
849
+ 'Donate',
850
+
851
+ source_subtitle:
852
+ sourceField.subtitle ||
853
+ 'Saweria',
854
+
855
+ source_url:
856
+ sourceField.url ||
857
+ url
858
+ })
859
+ );
860
+
861
+
862
+ submessages.push({
863
+
864
+ messageType:
865
+ RichSubMessageType.TEXT,
866
+
867
+ messageText:
868
+ linkField.text +
869
+ ` {{${prefix}}}¹{{/${prefix}}} `,
870
+
871
+ inlineEntities: [
872
+ {
873
+
874
+ key:
875
+ prefix,
876
+
877
+ metadata: {
878
+
879
+ reference_id:
880
+ index + 1,
881
+
882
+ reference_url:
883
+ url,
884
+
885
+ reference_title:
886
+ linkField.title ||
887
+ 'For Donation via Saweria',
888
+
889
+ reference_display_name:
890
+ linkField.displayName ||
891
+ 'Donation',
892
+
893
+ sources:
894
+ sources || [],
895
+
896
+ __typename:
897
+ 'GenAISearchCitationItem'
898
+ }
303
899
  }
304
- }]
305
- });
306
- });
900
+ ]
901
+ });
902
+ }
903
+ );
307
904
  }
308
- if (posts) {
905
+
906
+
907
+ /* ================================================
908
+ * POSTS
909
+ * ================================================ */
910
+
911
+ if (
912
+ posts
913
+ ) {
914
+
309
915
  submessages.push({
310
- messageType: RichSubMessageType.TEXT,
311
- messageText: 'POSTS'
916
+
917
+ messageType:
918
+ RichSubMessageType.TEXT,
919
+
920
+ messageText:
921
+ 'POSTS'
312
922
  });
313
923
  }
314
-
315
- const dataProduct = products || product
316
- if (dataProduct) {
317
- const list = Array.isArray(dataProduct) ? dataProduct : [dataProduct]
318
-
319
- submessages.push({
320
- messageType: RichSubMessageType.CONTENT_ITEMS,
321
- contentItemsMetadata: {
322
- contentType: proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL,
323
- itemsMetadata: list.map(item => ({
324
- title: item.title,
325
- brand: item.brand,
326
- price: item.price,
327
- sale_price: item.sale_price,
328
- product_url: item.product_url,
329
- image: {
330
- url: item.image_url
331
- },
332
- additional_images: item.icon_url ? [{
333
- url: item.icon_url
334
- }] : [],
335
- __typename: "GenAIProductItemCardPrimitive"
336
- }))
924
+
925
+
926
+ /* ================================================
927
+ * PRODUCT
928
+ * ================================================ */
929
+
930
+ if (
931
+ dataProduct
932
+ ) {
933
+
934
+ const list =
935
+ Array.isArray(dataProduct)
936
+ ? dataProduct
937
+ : [dataProduct];
938
+
939
+
940
+ submessages.push({
941
+
942
+ messageType:
943
+ RichSubMessageType.CONTENT_ITEMS,
944
+
945
+ contentItemsMetadata: {
946
+
947
+ contentType:
948
+ proto
949
+ .AIRichResponseContentItemsMetadata
950
+ .ContentType
951
+ .CAROUSEL,
952
+
953
+ itemsMetadata:
954
+ list.map(
955
+ (item) => ({
956
+
957
+ title:
958
+ item.title,
959
+
960
+ brand:
961
+ item.brand,
962
+
963
+ price:
964
+ item.price,
965
+
966
+ sale_price:
967
+ item.sale_price,
968
+
969
+ product_url:
970
+ item.product_url,
971
+
972
+ image:
973
+ item.image_url
974
+ ? {
975
+ url:
976
+ item.image_url
977
+ }
978
+ : undefined,
979
+
980
+ additional_images:
981
+ item.icon_url
982
+ ? [
983
+ {
984
+ url:
985
+ item.icon_url
986
+ }
987
+ ]
988
+ : [],
989
+
990
+ __typename:
991
+ 'GenAIProductItemCardPrimitive'
992
+ })
993
+ )
994
+ }
995
+ });
337
996
  }
338
- })
339
- }
340
- if (suggested) {
341
- submessages.push({
342
- messageType: RichSubMessageType.CONTENT_ITEMS,
343
- contentItemsMetadata: {
344
- itemsMetadata: suggested.map(s => ({
345
- prompt_text: s,
346
- prompt_type: "SUGGESTED_PROMPT",
347
- __typename: "GenAIFollowUpSuggestionPillPrimitive"
348
- }))
997
+
998
+
999
+ /* ================================================
1000
+ * SUGGESTED
1001
+ * ================================================ */
1002
+
1003
+ if (
1004
+ suggested
1005
+ ) {
1006
+
1007
+ const suggestionList =
1008
+ Array.isArray(suggested)
1009
+ ? suggested
1010
+ : [suggested];
1011
+
1012
+
1013
+ submessages.push({
1014
+
1015
+ messageType:
1016
+ RichSubMessageType.CONTENT_ITEMS,
1017
+
1018
+ contentItemsMetadata: {
1019
+
1020
+ itemsMetadata:
1021
+ suggestionList.map(
1022
+ (text) => ({
1023
+
1024
+ prompt_text:
1025
+ text,
1026
+
1027
+ prompt_type:
1028
+ 'SUGGESTED_PROMPT',
1029
+
1030
+ __typename:
1031
+ 'GenAIFollowUpSuggestionPillPrimitive'
1032
+ })
1033
+ )
1034
+ }
1035
+ });
349
1036
  }
350
- })
351
- }
352
- if (table) {
1037
+
1038
+
1039
+ /* ================================================
1040
+ * TABLE
1041
+ * ================================================ */
1042
+
1043
+ if (
1044
+ table
1045
+ ) {
1046
+
353
1047
  submessages.push({
354
- messageType: RichSubMessageType.TABLE,
1048
+
1049
+ messageType:
1050
+ RichSubMessageType.TABLE,
1051
+
355
1052
  tableMetadata: {
356
- title,
357
- rows: table.map((items, index) => ({
358
- isHeading: !noHeading && index == 0,
359
- items
360
- }))
1053
+
1054
+ title:
1055
+ title,
1056
+
1057
+ rows:
1058
+ table.map(
1059
+ (items, index) => ({
1060
+
1061
+ isHeading:
1062
+ !noHeading &&
1063
+ index === 0,
1064
+
1065
+ items
1066
+ })
1067
+ )
361
1068
  }
362
1069
  });
363
1070
  }
364
- if (footerText) {
365
- submessages.push({
366
- messageType: RichSubMessageType.METADATA_TEXT,
367
- messageText: footerText
368
- });
369
- }
370
1071
  }
371
- const uuid = randomUUID();
372
- const unified = toUnified(submessages, uuid);
373
- const richResponseMessage = proto.AIRichResponseMessage.create({
374
- submessages,
375
- messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
376
- unifiedResponse: {
377
- data: Buffer.from(JSON.stringify(unified)) // Lia@Note 25-04-26 --- Expects "ArrayBufferLike"
1072
+
1073
+
1074
+ /* =====================================================
1075
+ * RESPONSE ID
1076
+ * ===================================================== */
1077
+
1078
+ const uuid =
1079
+ randomUUID();
1080
+
1081
+
1082
+ /* =====================================================
1083
+ * UNIFIED RESPONSE
1084
+ * ===================================================== */
1085
+
1086
+ const unified =
1087
+ toUnified(
1088
+ submessages,
1089
+ uuid,
1090
+ footerText
1091
+ );
1092
+
1093
+
1094
+ const base64Data =
1095
+ Buffer
1096
+ .from(
1097
+ JSON.stringify(unified)
1098
+ )
1099
+ .toString('base64');
1100
+
1101
+
1102
+ /* =====================================================
1103
+ * OURIN V2 CONTEXT INFO
1104
+ * ===================================================== */
1105
+
1106
+ const contextInfo = {
1107
+
1108
+ mentionedJid: [],
1109
+
1110
+ groupMentions: [],
1111
+
1112
+ statusAttributions: [],
1113
+
1114
+ forwardingScore: 2,
1115
+
1116
+ isForwarded: true,
1117
+
1118
+ forwardedAiBotMessageInfo: {
1119
+
1120
+ botJid:
1121
+ '259786046210223@bot'
378
1122
  },
379
- contextInfo: {
380
- isForwarded: true,
381
- forwardingScore: 1,
382
- forwardedAiBotMessageInfo: { botJid: '867051314767696@bot' },
383
- forwardOrigin: 4
1123
+
1124
+ forwardOrigin: 4,
1125
+
1126
+ botMessageSharingInfo: {
1127
+
1128
+ botEntryPointOrigin: 1,
1129
+
1130
+ forwardScore: 2
384
1131
  }
385
- });
386
- const message = wrapToBotForwardedMessage(richResponseMessage);
387
- const botMetadata = message.messageContextInfo.botMetadata;
388
- // Lia@Note 13-05-26 --- Add disclaimer text on richResponseMessage
389
- if (disclaimerText) {
390
- botMetadata.messageDisclaimerText = disclaimerText;
1132
+ };
1133
+
1134
+ const richResponseMessage =
1135
+ proto.AIRichResponseMessage.create({
1136
+
1137
+ submessages: [],
1138
+
1139
+ messageType:
1140
+ proto
1141
+ .AIRichResponseMessageType
1142
+ .AI_RICH_RESPONSE_TYPE_STANDARD,
1143
+
1144
+ unifiedResponse: {
1145
+
1146
+ data:
1147
+ base64Data
1148
+ },
1149
+
1150
+ contextInfo
1151
+ });
1152
+
1153
+
1154
+ /* =====================================================
1155
+ * WRAP
1156
+ * ===================================================== */
1157
+
1158
+ const message =
1159
+ wrapToBotForwardedMessage(
1160
+ richResponseMessage
1161
+ );
1162
+
1163
+
1164
+ /* =====================================================
1165
+ * BOT METADATA
1166
+ * ===================================================== */
1167
+
1168
+ const botMetadata =
1169
+ message
1170
+ .messageContextInfo
1171
+ .botMetadata;
1172
+
1173
+
1174
+ /* =====================================================
1175
+ * DISCLAIMER
1176
+ * ===================================================== */
1177
+
1178
+ if (
1179
+ disclaimerText
1180
+ ) {
1181
+
1182
+ botMetadata.messageDisclaimerText =
1183
+ disclaimerText;
391
1184
  }
392
- // Lia@Note 15-05-26 --- Add responseId from unified directly to botMetadata
393
- botMetadata.botResponseId = uuid;
1185
+
1186
+
1187
+ /* =====================================================
1188
+ * RESPONSE ID
1189
+ * ===================================================== */
1190
+
1191
+ botMetadata.botResponseId =
1192
+ uuid;
1193
+
1194
+
394
1195
  return message;
395
1196
  };
396
- // Lia@Note 17-04-26 --- signature and certificateChain for proofs[] field
1197
+
1198
+
1199
+ /* =========================================================
1200
+ * BOT METADATA SIGNATURE
1201
+ * ========================================================= */
1202
+
397
1203
  export const botMetadataSignature = () => {
398
- const signature = new Uint8Array(64);
399
- getRandomValues(signature);
1204
+
1205
+ const signature =
1206
+ new Uint8Array(64);
1207
+
1208
+ getRandomValues(
1209
+ signature
1210
+ );
1211
+
400
1212
  return signature;
401
1213
  };
402
- export const botMetadataCertificate = (length = 685) => {
403
- const certificate = new Uint8Array(length);
404
- certificate[0] = 48;
405
- certificate[1] = 130;
406
- getRandomValues(certificate.subarray(2));
1214
+
1215
+
1216
+ /* =========================================================
1217
+ * BOT METADATA CERTIFICATE
1218
+ * ========================================================= */
1219
+
1220
+ export const botMetadataCertificate = (
1221
+ length = 685
1222
+ ) => {
1223
+
1224
+ const certificate =
1225
+ new Uint8Array(length);
1226
+
1227
+ certificate[0] =
1228
+ 48;
1229
+
1230
+ certificate[1] =
1231
+ 130;
1232
+
1233
+ getRandomValues(
1234
+ certificate.subarray(2)
1235
+ );
1236
+
407
1237
  return certificate;
408
1238
  };
409
- export const wrapToBotForwardedMessage = (richResponseMessage) => ({
410
- messageContextInfo: {
411
- botMetadata: {
412
- // Lia@Note 09-04-26 --- TODO: Fill verificationMetadata field
413
- verificationMetadata: {
414
- proofs: [
415
- {
416
- certificateChain: [
417
- botMetadataCertificate(),
418
- botMetadataCertificate(892)
419
- ],
420
- version: 1,
421
- useCase: 1,
422
- signature: botMetadataSignature()
423
- }
424
- ]
1239
+
1240
+
1241
+ /* =========================================================
1242
+ * WRAP TO BOT FORWARDED MESSAGE
1243
+ * ========================================================= */
1244
+
1245
+ export const wrapToBotForwardedMessage = (
1246
+ richResponseMessage
1247
+ ) => {
1248
+
1249
+ return {
1250
+
1251
+ messageContextInfo: {
1252
+
1253
+ /* =============================================
1254
+ * OURIN V2 MESSAGE CONTEXT
1255
+ * ============================================= */
1256
+
1257
+ threadId: [],
1258
+
1259
+ deviceListMetadata: {
1260
+
1261
+ senderKeyIndexes: [],
1262
+
1263
+ recipientKeyIndexes: [],
1264
+
1265
+ recipientKeyHash: '',
1266
+
1267
+ recipientTimestamp:
1268
+ Math.floor(
1269
+ Date.now() / 1000
1270
+ )
1271
+ },
1272
+
1273
+ deviceListMetadataVersion:
1274
+ 2,
1275
+
1276
+ messageSecret:
1277
+ randomBytes(32),
1278
+
1279
+
1280
+ /* =============================================
1281
+ * BOT METADATA
1282
+ * ============================================= */
1283
+
1284
+ botMetadata: {
1285
+
1286
+ verificationMetadata: {
1287
+
1288
+ proofs: [
1289
+ {
1290
+
1291
+ certificateChain: [
1292
+
1293
+ botMetadataCertificate(),
1294
+
1295
+ botMetadataCertificate(
1296
+ 892
1297
+ )
1298
+ ],
1299
+
1300
+ version:
1301
+ 1,
1302
+
1303
+ useCase:
1304
+ 1,
1305
+
1306
+ signature:
1307
+ botMetadataSignature()
1308
+ }
1309
+ ]
1310
+ }
1311
+ }
1312
+ },
1313
+
1314
+
1315
+ /* ================================================
1316
+ * BOT FORWARDED MESSAGE
1317
+ * ================================================ */
1318
+
1319
+ botForwardedMessage: {
1320
+
1321
+ message: {
1322
+
1323
+ richResponseMessage
1324
+
425
1325
  }
426
1326
  }
427
- },
428
- botForwardedMessage: {
429
- message: { richResponseMessage }
430
- }
431
- });
1327
+ };
1328
+ };
1329
+
1330
+
432
1331
  //# sourceMappingURL=rich-message-utils.js.map