@langitdeveloper/baileys 2.1.4 → 2.1.5
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/lib/Utils/messages.js +332 -0
- package/package.json +1 -1
package/lib/Utils/messages.js
CHANGED
|
@@ -380,6 +380,338 @@ const generateWAMessageContent = async (message, options) => {
|
|
|
380
380
|
else if ('requestPhoneNumber' in message) {
|
|
381
381
|
m.requestPhoneNumberMessage = {};
|
|
382
382
|
}
|
|
383
|
+
else if ('richMessage' in message) {
|
|
384
|
+
const { randomUUID } = require('crypto');
|
|
385
|
+
const rich = message.richMessage;
|
|
386
|
+
const submessages = [];
|
|
387
|
+
const sections = [];
|
|
388
|
+
const richResponseSources = [];
|
|
389
|
+
|
|
390
|
+
const extractIE = (text) => {
|
|
391
|
+
let ie = [], result = '', last = 0, citation_index = 1, hyperlink_index = 0, latex_index = 0, stack = [];
|
|
392
|
+
for (let i = 0; i < text.length; i++) {
|
|
393
|
+
if (text[i] == '[' && text[i - 1] != '\\') {
|
|
394
|
+
stack.push(i);
|
|
395
|
+
} else if (text[i] == ']' && (text[i + 1] == '(' || text[i + 1] == '<')) {
|
|
396
|
+
let start = stack.pop();
|
|
397
|
+
if (start == null) continue;
|
|
398
|
+
let open = text[i + 1], close = open == '(' ? ')' : '>', type = open == '(' ? 'link' : 'latex', end = i + 2, depth = 1;
|
|
399
|
+
while (end < text.length && depth) {
|
|
400
|
+
if (text[end] == open && text[end - 1] != '\\') depth++;
|
|
401
|
+
else if (text[end] == close && text[end - 1] != '\\') depth--;
|
|
402
|
+
end++;
|
|
403
|
+
}
|
|
404
|
+
if (depth) continue;
|
|
405
|
+
let raw = text.slice(start + 1, i).trim(), url = text.slice(i + 2, end - 1).trim(), key, tag, data;
|
|
406
|
+
if (type == 'latex') {
|
|
407
|
+
let [txt = '', width = null, height = null, font_height = null, padding = null] = raw.split('|');
|
|
408
|
+
key = `LATEX_${latex_index++}`;
|
|
409
|
+
tag = `{{${key}}}${txt || 'image'}{{/${key}}}`;
|
|
410
|
+
data = { type: 'latex', ie: { key, text: txt, url, width, height, font_height, padding } };
|
|
411
|
+
} else if (raw) {
|
|
412
|
+
key = `HLINK_${hyperlink_index++}`;
|
|
413
|
+
tag = `{{${key}}}${url}{{/${key}}}`;
|
|
414
|
+
data = { type: 'hyperlink', ie: { key, text: raw, url } };
|
|
415
|
+
} else {
|
|
416
|
+
key = `CITE_${citation_index - 1}`;
|
|
417
|
+
tag = `{{${key}}}${url}{{/${key}}}`;
|
|
418
|
+
data = { type: 'citation', ie: { reference_id: citation_index++, key, text: '', url } };
|
|
419
|
+
}
|
|
420
|
+
result += text.slice(last, start) + tag;
|
|
421
|
+
last = end;
|
|
422
|
+
ie.push(data);
|
|
423
|
+
i = end - 1;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
result += text.slice(last);
|
|
427
|
+
return { text: result, ie };
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
const tokenizer = (code, lang = 'javascript') => {
|
|
431
|
+
const keywordsMap = {
|
|
432
|
+
javascript: new Set(['break','case','catch','continue','debugger','delete','do','else','finally','for','function','if','in','instanceof','new','return','switch','this','throw','try','typeof','var','void','while','with','true','false','null','undefined','class','const','let','super','extends','export','import','yield','static','constructor','async','await','get','set'])
|
|
433
|
+
};
|
|
434
|
+
const TYPE_MAP = { 0:'DEFAULT', 1:'KEYWORD', 2:'METHOD', 3:'STR', 4:'NUMBER', 5:'COMMENT' };
|
|
435
|
+
const keywords = keywordsMap[lang] || new Set();
|
|
436
|
+
const tokens = [];
|
|
437
|
+
let i = 0;
|
|
438
|
+
const push = (content, type) => {
|
|
439
|
+
if (!content) return;
|
|
440
|
+
const last = tokens[tokens.length - 1];
|
|
441
|
+
if (last && last.highlightType === type) last.codeContent += content;
|
|
442
|
+
else tokens.push({ codeContent: content, highlightType: type });
|
|
443
|
+
};
|
|
444
|
+
while (i < code.length) {
|
|
445
|
+
const c = code[i];
|
|
446
|
+
if (/\s/.test(c)) { let s = i; while (i < code.length && /\s/.test(code[i])) i++; push(code.slice(s, i), 0); continue; }
|
|
447
|
+
if (c === '/' && code[i + 1] === '/') { let s = i; i += 2; while (i < code.length && code[i] !== '\n') i++; push(code.slice(s, i), 5); continue; }
|
|
448
|
+
if (c === '"' || c === "'" || c === '`') { let s = i; const q = c; i++; while (i < code.length) { if (code[i] === '\\' && i + 1 < code.length) i += 2; else if (code[i] === q) { i++; break; } else i++; } push(code.slice(s, i), 3); continue; }
|
|
449
|
+
if (/[0-9]/.test(c)) { let s = i; while (i < code.length && /[0-9.]/.test(code[i])) i++; push(code.slice(s, i), 4); continue; }
|
|
450
|
+
if (/[a-zA-Z_$]/.test(c)) { let s = i; while (i < code.length && /[a-zA-Z0-9_$]/.test(code[i])) i++; const word = code.slice(s, i); let type = 0; if (keywords.has(word)) type = 1; else { let j = i; while (j < code.length && /\s/.test(code[j])) j++; if (code[j] === '(') type = 2; } push(word, type); continue; }
|
|
451
|
+
push(c, 0); i++;
|
|
452
|
+
}
|
|
453
|
+
return { codeBlock: tokens, unified_codeBlock: tokens.map(t => ({ content: t.codeContent, type: TYPE_MAP[t.highlightType] })) };
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const toTableMetadata = (arr) => {
|
|
457
|
+
const [header, ...rows] = arr;
|
|
458
|
+
const maxLen = Math.max(header.length, ...rows.map(r => r.length));
|
|
459
|
+
const normalize = (r) => [...r, ...Array(maxLen - r.length).fill('')];
|
|
460
|
+
const unified_rows = [{ is_header: true, cells: normalize(header) }, ...rows.map(r => ({ is_header: false, cells: normalize(r) }))];
|
|
461
|
+
const rowsMeta = unified_rows.map(r => ({ items: r.cells, ...(r.is_header ? { isHeading: true } : {}) }));
|
|
462
|
+
return { title: '', rows: rowsMeta, unified_rows };
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
if (rich.text) {
|
|
466
|
+
const parsed = typeof rich.text === 'string' ? extractIE(rich.text) : rich.text;
|
|
467
|
+
const text = parsed.text || parsed;
|
|
468
|
+
const inline_entities = parsed.ie ? parsed.ie.map(({ type, ie }) => {
|
|
469
|
+
if (type === 'hyperlink') return { key: ie.key, metadata: { display_name: ie.text, is_trusted: true, url: ie.url, __typename: 'GenAIInlineLinkItem' } };
|
|
470
|
+
if (type === 'citation') return { key: ie.key, metadata: { reference_id: ie.reference_id, reference_url: ie.url, reference_title: ie.url, reference_display_name: ie.url, sources: [], __typename: 'GenAISearchCitationItem' } };
|
|
471
|
+
if (type === 'latex') return { key: ie.key, metadata: { latex_expression: ie.text || '', latex_image: { url: ie.url, width: Number(ie.width) || 100, height: Number(ie.height) || 100 }, font_height: Number(ie.font_height) || 83.33, padding: Number(ie.padding) || 15, __typename: 'GenAILatexItem' } };
|
|
472
|
+
return null;
|
|
473
|
+
}).filter(Boolean) : [];
|
|
474
|
+
submessages.push({ messageType: 2, messageText: text });
|
|
475
|
+
sections.push({
|
|
476
|
+
view_model: {
|
|
477
|
+
primitive: { text, inline_entities, __typename: 'GenAIMarkdownTextUXPrimitive' },
|
|
478
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (rich.code) {
|
|
484
|
+
const { language, code } = rich.code;
|
|
485
|
+
const tok = tokenizer(code, language);
|
|
486
|
+
submessages.push({ messageType: 5, codeMetadata: { codeLanguage: language, codeBlocks: tok.codeBlock } });
|
|
487
|
+
sections.push({
|
|
488
|
+
view_model: {
|
|
489
|
+
primitive: { language, code_blocks: tok.unified_codeBlock, __typename: 'GenAICodeUXPrimitive' },
|
|
490
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if (rich.table) {
|
|
496
|
+
const meta = toTableMetadata(rich.table);
|
|
497
|
+
submessages.push({ messageType: 4, tableMetadata: { title: meta.title, rows: meta.rows } });
|
|
498
|
+
sections.push({
|
|
499
|
+
view_model: {
|
|
500
|
+
primitive: { rows: meta.unified_rows, __typename: 'GenATableUXPrimitive' },
|
|
501
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (rich.images) {
|
|
507
|
+
const urls = Array.isArray(rich.images) ? rich.images : [rich.images];
|
|
508
|
+
submessages.push({
|
|
509
|
+
messageType: 1,
|
|
510
|
+
gridImageMetadata: {
|
|
511
|
+
gridImageUrl: { imagePreviewUrl: urls[0] },
|
|
512
|
+
imageUrls: urls.map(url => ({ imagePreviewUrl: url, imageHighResUrl: url, sourceUrl: 'https://www.levvicode.cloud/' }))
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
urls.forEach(url => {
|
|
516
|
+
sections.push({
|
|
517
|
+
view_model: {
|
|
518
|
+
primitive: { media: { url, mime_type: 'image/jpeg' }, imagine_type: 3, status: { status: 'READY' }, __typename: 'GenAIImaginePrimitive' },
|
|
519
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (rich.video) {
|
|
526
|
+
submessages.push({ messageType: 2, messageText: '[ CANNOT_LOAD_VIDEO ]' });
|
|
527
|
+
sections.push({
|
|
528
|
+
view_model: {
|
|
529
|
+
primitive: {
|
|
530
|
+
media: { url: rich.video, mime_type: 'video/mp4', duration: 10 },
|
|
531
|
+
imagine_type: 'ANIMATE',
|
|
532
|
+
status: { status: 'READY' },
|
|
533
|
+
__typename: 'GenAIImaginePrimitive'
|
|
534
|
+
},
|
|
535
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (rich.productSingle) {
|
|
541
|
+
submessages.push({ messageType: 2, messageText: '[ CANNOT_LOAD_PRODUCT ]' });
|
|
542
|
+
sections.push({
|
|
543
|
+
view_model: {
|
|
544
|
+
primitive: { ...rich.productSingle, __typename: 'GenAIProductItemCardPrimitive' },
|
|
545
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
if (rich.productMultiple) {
|
|
551
|
+
submessages.push({ messageType: 2, messageText: '[ CANNOT_LOAD_PRODUCT ]' });
|
|
552
|
+
sections.push({
|
|
553
|
+
view_model: {
|
|
554
|
+
primitives: rich.productMultiple.map(p => ({ ...p, __typename: 'GenAIProductItemCardPrimitive' })),
|
|
555
|
+
__typename: 'GenAIHScrollLayoutViewModel'
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if (rich.product && !rich.productSingle && !rich.productMultiple) {
|
|
561
|
+
submessages.push({ messageType: 2, messageText: '[ CANNOT_LOAD_PRODUCT ]' });
|
|
562
|
+
if (Array.isArray(rich.product)) {
|
|
563
|
+
sections.push({
|
|
564
|
+
view_model: {
|
|
565
|
+
primitives: rich.product.map(p => ({ ...p, __typename: 'GenAIProductItemCardPrimitive' })),
|
|
566
|
+
__typename: 'GenAIHScrollLayoutViewModel'
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
} else {
|
|
570
|
+
sections.push({
|
|
571
|
+
view_model: {
|
|
572
|
+
primitive: { ...rich.product, __typename: 'GenAIProductItemCardPrimitive' },
|
|
573
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
if (rich.post) {
|
|
580
|
+
submessages.push({ messageType: 2, messageText: '[ CANNOT_LOAD_POST ]' });
|
|
581
|
+
if (Array.isArray(rich.post)) {
|
|
582
|
+
sections.push({
|
|
583
|
+
view_model: {
|
|
584
|
+
primitives: rich.post.map(p => ({ ...p, __typename: 'GenAIPostPrimitive' })),
|
|
585
|
+
__typename: 'GenAIHScrollLayoutViewModel'
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
} else {
|
|
589
|
+
sections.push({
|
|
590
|
+
view_model: {
|
|
591
|
+
primitive: { ...rich.post, __typename: 'GenAIPostPrimitive' },
|
|
592
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if (rich.reels) {
|
|
599
|
+
const items = Array.isArray(rich.reels) ? rich.reels : [rich.reels];
|
|
600
|
+
submessages.push({
|
|
601
|
+
messageType: 9,
|
|
602
|
+
contentItemsMetadata: {
|
|
603
|
+
contentType: 1,
|
|
604
|
+
itemsMetadata: items.map(i => ({
|
|
605
|
+
reelItem: { title: i.title, profileIconUrl: i.profileIconUrl, thumbnailUrl: i.thumbnailUrl, videoUrl: i.videoUrl }
|
|
606
|
+
}))
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
sections.push({
|
|
610
|
+
view_model: {
|
|
611
|
+
primitives: items.map(i => ({
|
|
612
|
+
reels_url: i.videoUrl,
|
|
613
|
+
thumbnail_url: i.thumbnailUrl,
|
|
614
|
+
creator: i.title,
|
|
615
|
+
avatar_url: i.profileIconUrl,
|
|
616
|
+
reels_title: i.reels_title || '',
|
|
617
|
+
likes_count: i.likes_count || 0,
|
|
618
|
+
shares_count: i.shares_count || 0,
|
|
619
|
+
view_count: i.view_count || 0,
|
|
620
|
+
reel_source: i.reel_source || 'IG',
|
|
621
|
+
is_verified: i.is_verified || false,
|
|
622
|
+
__typename: 'GenAIReelPrimitive'
|
|
623
|
+
})),
|
|
624
|
+
__typename: 'GenAIHScrollLayoutViewModel'
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
items.forEach((i, idx) => richResponseSources.push({
|
|
628
|
+
provider: 'MahiruBaileys',
|
|
629
|
+
thumbnailCDNURL: i.thumbnailUrl,
|
|
630
|
+
sourceProviderURL: i.videoUrl,
|
|
631
|
+
sourceQuery: '',
|
|
632
|
+
faviconCDNURL: i.profileIconUrl,
|
|
633
|
+
citationNumber: idx + 1,
|
|
634
|
+
sourceTitle: i.title
|
|
635
|
+
}));
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
if (rich.sources) {
|
|
639
|
+
const sourceArr = Array.isArray(rich.sources) ? rich.sources : [rich.sources];
|
|
640
|
+
sections.push({
|
|
641
|
+
view_model: {
|
|
642
|
+
primitive: {
|
|
643
|
+
sources: sourceArr.map(s => typeof s === 'object' ? s : {
|
|
644
|
+
source_type: 'THIRD_PARTY',
|
|
645
|
+
source_display_name: s[2] || '',
|
|
646
|
+
source_subtitle: 'AI',
|
|
647
|
+
source_url: s[1] || '',
|
|
648
|
+
favicon: { url: s[0] || '', mime_type: 'image/jpeg', width: 16, height: 16 }
|
|
649
|
+
}),
|
|
650
|
+
__typename: 'GenAISearchResultPrimitive'
|
|
651
|
+
},
|
|
652
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (rich.tip) {
|
|
658
|
+
submessages.push({ messageType: 2, messageText: rich.tip });
|
|
659
|
+
sections.push({
|
|
660
|
+
view_model: {
|
|
661
|
+
primitive: { text: rich.tip, __typename: 'GenAIMetadataTextPrimitive' },
|
|
662
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (rich.suggestions) {
|
|
668
|
+
sections.push({
|
|
669
|
+
view_model: {
|
|
670
|
+
primitives: rich.suggestions.map(s => ({
|
|
671
|
+
prompt_text: s,
|
|
672
|
+
prompt_type: 'SUGGESTED_PROMPT',
|
|
673
|
+
__typename: 'GenAIFollowUpSuggestionPillPrimitive'
|
|
674
|
+
})),
|
|
675
|
+
__typename: 'GenAIActionRowLayoutViewModel'
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
if (rich.footer) {
|
|
681
|
+
sections.push({
|
|
682
|
+
view_model: {
|
|
683
|
+
primitive: { text: rich.footer, __typename: 'GenAIMetadataTextPrimitive' },
|
|
684
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
const unifiedData = { response_id: randomUUID(), sections };
|
|
690
|
+
|
|
691
|
+
m = {
|
|
692
|
+
messageContextInfo: {
|
|
693
|
+
deviceListMetadata: {},
|
|
694
|
+
deviceListMetadataVersion: 2,
|
|
695
|
+
botMetadata: {
|
|
696
|
+
messageDisclaimerText: rich.title || '',
|
|
697
|
+
richResponseSourcesMetadata: { sources: richResponseSources }
|
|
698
|
+
}
|
|
699
|
+
},
|
|
700
|
+
richResponseMessage: {
|
|
701
|
+
messageType: 1,
|
|
702
|
+
submessages,
|
|
703
|
+
unifiedResponse: {
|
|
704
|
+
data: Buffer.from(JSON.stringify(unifiedData)).toString('base64')
|
|
705
|
+
},
|
|
706
|
+
contextInfo: {
|
|
707
|
+
forwardingScore: 1,
|
|
708
|
+
isForwarded: true,
|
|
709
|
+
forwardedAiBotMessageInfo: { botJid: '0@bot' },
|
|
710
|
+
forwardOrigin: 4
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
}
|
|
383
715
|
else {
|
|
384
716
|
m = await (0, exports.prepareWAMessageMedia)(message, options);
|
|
385
717
|
}
|