whalibmob 5.40.41 → 5.40.43

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.
@@ -12,6 +12,7 @@ const { BinaryNode } = require('../BinaryNode');
12
12
  const { DeviceManager, jidStrToObj } = require('../DeviceManager');
13
13
  const {
14
14
  encodeMessage,
15
+ encodeInteractiveMessage,
15
16
  encodeText, encodeImageMessage, encodeVideoMessage,
16
17
  encodeAudioMessage, encodeDocumentMessage,
17
18
  encodeStickerMessage, encodeReactionMessage,
@@ -39,6 +40,34 @@ function generateMessageId() {
39
40
 
40
41
  function msNow() { return Math.floor(Date.now() / 1000); }
41
42
 
43
+ // Turn a friendly button spec into a native-flow button {name, buttonParamsJson}.
44
+ // A button already in native-flow shape (it has name + buttonParamsJson) is
45
+ // passed through untouched, so a caller can hand-craft exotic flows.
46
+ // { url, text } → cta_url (a hyperlink/link button)
47
+ // { phone, text } → cta_call (dials the number)
48
+ // { copy, text } → cta_copy (copies a code to the clipboard)
49
+ // { text, id? } → quick_reply (sends the text back as a reply)
50
+ function toNativeFlowButton(b) {
51
+ b = b || {};
52
+ if (b.name && b.buttonParamsJson) return b;
53
+ const text = b.text || b.displayText || b.display_text || '';
54
+ if (b.url) {
55
+ const p = { display_text: text, url: b.url };
56
+ if (b.merchantUrl || b.merchant_url) p.merchant_url = b.merchantUrl || b.merchant_url;
57
+ return { name: 'cta_url', buttonParamsJson: JSON.stringify(p) };
58
+ }
59
+ if (b.phone || b.phoneNumber || b.phone_number) {
60
+ return { name: 'cta_call', buttonParamsJson: JSON.stringify({
61
+ display_text: text, phone_number: b.phone || b.phoneNumber || b.phone_number }) };
62
+ }
63
+ if (b.copy || b.copyCode || b.copy_code) {
64
+ return { name: 'cta_copy', buttonParamsJson: JSON.stringify({
65
+ display_text: text, copy_code: b.copy || b.copyCode || b.copy_code }) };
66
+ }
67
+ return { name: 'quick_reply', buttonParamsJson: JSON.stringify({
68
+ display_text: text, id: b.id || text || 'btn' }) };
69
+ }
70
+
42
71
  function isGroupJid(jid) {
43
72
  return jid.endsWith('@g.us') || jid.endsWith('@community');
44
73
  }
@@ -330,6 +359,88 @@ class MessageSender {
330
359
  return this._sendMessage(toJid, msgId, imgBuf, 'media', { ...options, _mediaSubtype: 'image' });
331
360
  }
332
361
 
362
+ // ─── Interactive (native-flow buttons / "card" message) ─────────────────────
363
+ //
364
+ // Sends a message with up to three tappable buttons: a link button (cta_url),
365
+ // a call button, a copy button, or a quick-reply. Optionally an image on top,
366
+ // giving the "card" look. Works the same on a mobile-registered number and a
367
+ // linked (web/companion) session — a message is encoded identically either way.
368
+ //
369
+ // sendInteractive(to, {
370
+ // body: 'Hello!',
371
+ // footer: 'Powered by whalibmob',
372
+ // image: './banner.jpg', // optional header image
373
+ // buttons: [
374
+ // { text: 'Open site', url: 'https://example.com' },
375
+ // { text: 'Call us', phone: '15551234567' },
376
+ // { text: 'Yes', id: 'yes' }
377
+ // ]
378
+ // })
379
+ async sendInteractive(to, options) {
380
+ options = options || {};
381
+ const msgId = options.id || generateMessageId();
382
+ const toJid = makeJid(to);
383
+
384
+ const raw = Array.isArray(options.buttons) ? options.buttons : [];
385
+ if (raw.length === 0) {
386
+ throw new Boom('sendInteractive needs at least one button', { statusCode: 400 });
387
+ }
388
+ if (raw.length > 3) {
389
+ throw new Boom('WhatsApp allows at most 3 interactive buttons', { statusCode: 400 });
390
+ }
391
+ const buttons = raw.map(toNativeFlowButton);
392
+
393
+ // The header image — the top of the "card". Uploaded like any photo.
394
+ let headerImage = null;
395
+ if (options.image) {
396
+ const buf = this._toMediaBuffer(options.image);
397
+ const [upload, meta] = await Promise.all([
398
+ this._uploadMedia('image', buf),
399
+ imageThumbnail(buf).catch(() => ({}))
400
+ ]);
401
+ headerImage = {
402
+ url: upload.url,
403
+ mimetype: upload.mimetype || 'image/jpeg',
404
+ caption: '',
405
+ fileSha256: upload.fileSha256,
406
+ fileLength: upload.fileLength || 0,
407
+ height: upload.height || meta.height || 0,
408
+ width: upload.width || meta.width || 0,
409
+ mediaKey: upload.mediaKey,
410
+ fileEncSha256: upload.fileEncSha256,
411
+ directPath: upload.directPath,
412
+ mediaKeyTimestamp: upload.mediaKeyTimestamp || Math.floor(Date.now() / 1000),
413
+ jpegThumbnail: upload.jpegThumbnail || meta.thumbnail || null
414
+ };
415
+ }
416
+
417
+ const content = encodeInteractiveMessage({
418
+ body: options.body != null ? options.body : (options.text || ''),
419
+ footer: options.footer,
420
+ title: options.title,
421
+ subtitle: options.subtitle,
422
+ headerImage,
423
+ buttons,
424
+ contextInfo: options.contextInfo
425
+ });
426
+ const msgBuf = encodeMessage('interactive', content);
427
+
428
+ // The <biz> addon that makes clients render the buttons. name='mixed'
429
+ // covers cta_url / quick_reply / cta_call / cta_copy; without this node the
430
+ // server accepts the message but the buttons never show.
431
+ const bizNode = new BinaryNode('biz', {}, [
432
+ new BinaryNode('interactive', { type: 'native_flow', v: '1' }, [
433
+ new BinaryNode('native_flow', { v: '9', name: 'mixed' }, null)
434
+ ])
435
+ ]);
436
+
437
+ const sendOpts = Object.assign({}, options, {
438
+ additionalNodes: [bizNode].concat(options.additionalNodes || [])
439
+ });
440
+ // Routed as a 'text' stanza — an interactive message is a text-type message.
441
+ return this._sendMessage(toJid, msgId, msgBuf, 'text', sendOpts);
442
+ }
443
+
333
444
  // ─── Video ────────────────────────────────────────────────────────────────
334
445
 
335
446
  async sendVideo(to, videoData, options) {
@@ -1613,6 +1724,12 @@ class MessageSender {
1613
1724
  retryDepth: options._retryDepth || 0
1614
1725
  });
1615
1726
 
1727
+ // Extra children a caller needs on the stanza — e.g. the <biz> native-flow
1728
+ // addon an interactive message carries.
1729
+ for (const extra of options.additionalNodes || []) {
1730
+ if (extra) msgContent.push(extra);
1731
+ }
1732
+
1616
1733
  // A poll says in the clear whether it is the poll or a vote on one.
1617
1734
  if (mediaType === 'poll') {
1618
1735
  msgContent.push(new BinaryNode('meta', { polltype: options._pollType || 'creation' }, null));
@@ -376,9 +376,76 @@ function encodeEncAddOnMessage(type, opts) {
376
376
  ]));
377
377
  }
378
378
 
379
+ // ─── InteractiveMessage (Message field 45) — native-flow buttons ─────────────
380
+ //
381
+ // A "card" message: an optional image/title header, body text, an optional
382
+ // footer, and up to three native-flow buttons — a link button (cta_url), a
383
+ // quick-reply button, a call button (cta_call) or a copy button (cta_copy).
384
+ //
385
+ // The matching <biz><interactive><native_flow/></biz> stanza node is attached
386
+ // by the sender (MessageSender.sendInteractive); without it the server accepts
387
+ // the message but clients do not render the buttons.
388
+ //
389
+ // InteractiveMessage { header=1, body=2, footer=3, nativeFlowMessage=6,
390
+ // contextInfo=15 }
391
+ // Body / Footer { text=1 }
392
+ // Header { title=1, subtitle=2, imageMessage=4, hasMediaAttachment=5 }
393
+ // NativeFlowMessage { buttons=1 (repeated), messageVersion=3 }
394
+ // NativeFlowButton { name=1, buttonParamsJson=2 }
395
+
396
+ function encodeNativeFlowButton(btn) {
397
+ return Buffer.concat([
398
+ field(1, WIRE_LEN, str(btn.name)),
399
+ field(2, WIRE_LEN, str(btn.buttonParamsJson))
400
+ ]);
401
+ }
402
+
403
+ function encodeInteractiveMessage(opts) {
404
+ opts = opts || {};
405
+ const parts = [];
406
+
407
+ // Header (field 1): title/subtitle and, optionally, an image on top. The
408
+ // image is a full ImageMessage, same shape as a standalone photo.
409
+ if (opts.title || opts.subtitle || opts.headerImage) {
410
+ const header = [];
411
+ if (opts.title) header.push(field(1, WIRE_LEN, str(opts.title)));
412
+ if (opts.subtitle) header.push(field(2, WIRE_LEN, str(opts.subtitle)));
413
+ if (opts.headerImage) {
414
+ header.push(field(4, WIRE_LEN, encodeImageMessage(opts.headerImage)));
415
+ header.push(field(5, WIRE_VARINT, bool(true))); // hasMediaAttachment
416
+ }
417
+ parts.push(field(1, WIRE_LEN, Buffer.concat(header)));
418
+ }
419
+
420
+ // Body (field 2 → text=1): the main text of the card.
421
+ if (opts.body != null) {
422
+ parts.push(field(2, WIRE_LEN, field(1, WIRE_LEN, str(opts.body))));
423
+ }
424
+
425
+ // Footer (field 3 → text=1): small grey text under the buttons.
426
+ if (opts.footer != null) {
427
+ parts.push(field(3, WIRE_LEN, field(1, WIRE_LEN, str(opts.footer))));
428
+ }
429
+
430
+ // NativeFlowMessage (field 6): the buttons. messageVersion is 1.
431
+ const buttons = Array.isArray(opts.buttons) ? opts.buttons : [];
432
+ const nf = [];
433
+ for (const b of buttons) nf.push(field(1, WIRE_LEN, encodeNativeFlowButton(b)));
434
+ nf.push(field(3, WIRE_VARINT, varint(opts.messageVersion || 1)));
435
+ parts.push(field(6, WIRE_LEN, Buffer.concat(nf)));
436
+
437
+ // ContextInfo (field 15): quotes and mentions, like any other message.
438
+ if (opts.contextInfo) {
439
+ parts.push(field(15, WIRE_LEN, encodeContextInfo(opts.contextInfo)));
440
+ }
441
+
442
+ return Buffer.concat(parts);
443
+ }
444
+
379
445
  function encodeMessage(type, payload) {
380
446
  switch (type) {
381
447
  case 'text': return payload;
448
+ case 'interactive': return field(45, WIRE_LEN, payload);
382
449
  case 'image': return field(3, WIRE_LEN, payload);
383
450
  case 'document': return field(7, WIRE_LEN, payload);
384
451
  case 'audio': return field(8, WIRE_LEN, payload);
@@ -1327,6 +1394,7 @@ function encodeSenderKeyDistributionMessage(groupId, axolotlBytes) {
1327
1394
 
1328
1395
  module.exports = {
1329
1396
  encodeText,
1397
+ encodeInteractiveMessage,
1330
1398
  encodeImageMessage,
1331
1399
  encodeVideoMessage,
1332
1400
  encodeAudioMessage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whalibmob",
3
- "version": "5.40.41",
3
+ "version": "5.40.43",
4
4
  "description": "Node.js library for WhatsApp — register a number over SMS, or link as a companion by QR. Signal E2E encryption, media, groups, channels.",
5
5
  "author": "Kunboruto20",
6
6
  "main": "index.js",
@@ -14,6 +14,7 @@
14
14
  "cli.js",
15
15
  "lib/**/*.js",
16
16
  "tools/**/*.js",
17
+ "examples/**/*.js",
17
18
  "README.md",
18
19
  "docs/**/*.md",
19
20
  "llms.txt",