chat-adapter-wwebjs 0.1.3 → 0.1.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # chat-adapter-wwebjs
2
2
 
3
+ ## 0.1.4
4
+
5
+ ### Changed
6
+
7
+ - A card answered by number with `numberedActions()` follows `richContent` like any other card: plain lines unless it is on. It used WhatsApp's markup either way. Addons read the setting from their context's `richContent`.
8
+
9
+ ### Fixed
10
+
11
+ - A group message that names the bot as `@` and its `userName`, without a WhatsApp mention, went unnoticed under Chat SDK 4.44, which takes an adapter's `false` as final. The adapter now flags only the mentions WhatsApp marks and leaves the rest for the SDK to detect.
12
+ - A card's link buttons were dropped unless `numberedActions()` listed them. They now go out as `Label: url` lines in every card.
13
+ - A modal with a `closeLabel` stopped only on that word, although its hint tells people to send _cancel_. Either now stops it.
14
+
3
15
  ## 0.1.3
4
16
 
5
17
  ### Added
package/README.md CHANGED
@@ -220,7 +220,7 @@ Anything not wrapped is still reachable through `adapter.client`.
220
220
 
221
221
  ## Card actions
222
222
 
223
- WhatsApp Web cannot send buttons, so a card's buttons would be dropped from its text. Add `numberedActions()` and they are listed by number instead, select options included, and a reply with a number — or a label — reaches `onAction` as a click would. Link buttons keep their URL; a button that opens a modal is left out, since WhatsApp has none.
223
+ WhatsApp Web cannot send buttons, so a card's buttons would be dropped from its text. Add `numberedActions()` and they are listed by number instead, select options included, and a reply with a number — or a label — reaches `onAction` as a click would. The card's text follows `richContent`, and a button that opens a modal is left out, since WhatsApp has none.
224
224
 
225
225
  ```typescript
226
226
  import { createWWebJSAdapter, numberedActions } from "chat-adapter-wwebjs";
@@ -238,7 +238,7 @@ An addon is a plain object with optional `renderCard`, `cardSent` and `onMessage
238
238
 
239
239
  ## Rich content
240
240
 
241
- WhatsApp Web has no cards, buttons or modals, so the adapter can only imitate them, and by default it doesn't: a card goes out as plain lines, and the adapter has no `openModal`, so `event.openModal()` resolves to `undefined` and your bot can tell. Set `richContent: true` to imitate them: cards in WhatsApp's markup, a bold title, `*Label:* value` fields and monospaced tables, with their images sent as media and the card as caption, and [modals](#modals) as conversations. Buttons answered by number are a separate opt-in, [`numberedActions()`](#card-actions).
241
+ WhatsApp Web has no cards, buttons or modals, so the adapter can only imitate them, and by default it doesn't: a card goes out as plain lines, its link buttons as `Label: url`, and the adapter has no `openModal`, so `event.openModal()` resolves to `undefined` and your bot can tell. Set `richContent: true` to imitate them: cards in WhatsApp's markup, a bold title, `*Label:* value` fields and monospaced tables, with their images sent as media and the card as caption, and [modals](#modals) as conversations. Buttons answered by number are a separate opt-in, [`numberedActions()`](#card-actions).
242
242
 
243
243
  ## Streaming
244
244
 
@@ -296,7 +296,7 @@ bot.onModalSubmit("booking", (event) =>
296
296
  | `Select`, `RadioSelect` | An option's number or label |
297
297
  | `ExternalSelect` | Words to search, which reach `onOptionsLoad`, then a number from what it found |
298
298
 
299
- An optional field, or one with an initial value, can be answered _skip_. _cancel_, or the modal's `closeLabel`, stops it, and reaches `onModalClose` when the modal sets `notifyOnClose`. An answer that doesn't fit is asked again with the reason. When every field is answered, `onModalSubmit` receives the values: return `{ action: "errors", errors }` and only those fields are asked again, with your messages; return `{ action: "update", modal }` or `{ action: "push", modal }` and the new modal starts. A modal left unanswered stops waiting after a day.
299
+ An optional field, or one with an initial value, can be answered _skip_. _cancel_, or the modal's `closeLabel`, stops it, and reaches `onModalClose` when the modal sets `notifyOnClose`. An answer that doesn't fit is asked again. When every field is answered, `onModalSubmit` receives the values: return `{ action: "errors", errors }` and only those fields are asked again, with your messages; return `{ action: "update", modal }` or `{ action: "push", modal }` and the new modal starts. A modal left unanswered stops waiting after a day.
300
300
 
301
301
  Modals open from actions — `numberedActions()` answers carry a trigger — and from slash commands. To open one for someone without either, pass `triggerIdOf(threadId, userId)` to `adapter.openModal()`. Put the phrases a modal sends in your users' language with `modalTexts`:
302
302
 
@@ -407,7 +407,7 @@ Examples: `wwebjs:15551234567@c.us` (DM by phone), `wwebjs:99990000111122@lid` (
407
407
  - **Unofficial.** It automates WhatsApp Web, which WhatsApp may block or change at any time.
408
408
  - **One session per number**, as WhatsApp Web itself allows.
409
409
  - **No scheduling or ephemeral messages.** `postEphemeral` falls back to a DM; `thread.schedule()` throws.
410
- - **`receiveOwnMessages` includes the adapter's own sends.** Turn it on only if your handlers can tell them apart.
410
+ - **Everything the paired account sends counts as the bot's own**, from the phone too, so the Chat SDK skips its messages, edits and reactions even with `receiveOwnMessages`; only its deletes come through.
411
411
  - **An attachment given only by `url` is fetched over HTTPS**, never from a private or internal address, with a 30-second timeout and `maxMediaBytes` as its limit.
412
412
  - **A bare `@number` in outgoing text stays text.** Mention someone with `thread.mentionUser(id)`.
413
413
  - **`forwardMessage` and event call links fail on current WhatsApp Web.** whatsapp-web.js still calls modules WhatsApp has renamed; both return once it catches up. `sendEvent` without `callType` attaches no link and works.
package/dist/index.d.ts CHANGED
@@ -180,6 +180,8 @@ interface WWebJSAddonContext {
180
180
  adapter: WWebJSAdapter;
181
181
  /** Absent until the Chat SDK has initialized the adapter. */
182
182
  chat: ChatInstance | null;
183
+ /** Whether cards use WhatsApp's markup; see {@link WWebJSAdapterConfig.richContent}. */
184
+ richContent: boolean;
183
185
  threadId: string;
184
186
  }
185
187
  /** A layer over the adapter; every hook is optional. */
package/dist/index.js CHANGED
@@ -96,17 +96,27 @@ ${tableElementToAscii(child.headers, child.rows)}
96
96
  return chartElementToFallbackText(child);
97
97
  case "section":
98
98
  return child.children.map(childText).filter(Boolean).join("\n");
99
+ case "actions":
100
+ return linkButtons(child);
99
101
  case "divider":
100
102
  return DIVIDER;
101
103
  default:
102
104
  return "";
103
105
  }
104
106
  }
107
+ function linkButtons(child) {
108
+ return child.children.flatMap((action) => action.type === "link-button" ? [`${action.label}: ${action.url}`] : []).join("\n");
109
+ }
110
+ function plainText(child) {
111
+ if (child.type === "actions") return linkButtons(child);
112
+ if (child.type === "section") return child.children.map(plainText).filter(Boolean).join("\n");
113
+ return cardChildToFallbackText(child);
114
+ }
105
115
  function cardToText(card) {
106
116
  return [card.title && `*${card.title}*`, card.subtitle, ...card.children.map(childText)].filter(Boolean).join("\n");
107
117
  }
108
118
  function cardToPlainText(card) {
109
- return [card.title, card.subtitle, ...card.children.map(cardChildToFallbackText)].filter(Boolean).join("\n");
119
+ return [card.title, card.subtitle, ...card.children.map(plainText)].filter(Boolean).join("\n");
110
120
  }
111
121
  function cardImages(card) {
112
122
  const inside = (children) => children.flatMap(
@@ -676,7 +686,7 @@ var WWebJSAdapter = class {
676
686
  author: this._author(raw),
677
687
  formatted: this._converter.toAst(text),
678
688
  id: serialize(raw.id),
679
- isMention: this._mentionsBot(raw),
689
+ isMention: this._mentionsBot(raw) || void 0,
680
690
  links: (raw.links ?? []).map(({ link }) => ({ url: link })),
681
691
  metadata: {
682
692
  dateSent: new Date(raw.timestamp * 1e3),
@@ -1219,7 +1229,7 @@ ${questionOf(field, form, this._texts())}` : intro
1219
1229
  const texts = this._texts();
1220
1230
  const text = (raw.body ?? "").trim();
1221
1231
  const event = { adapter: this, callbackId: form.callbackId, raw, user, viewId: form.viewId };
1222
- if (text.toLowerCase() === (form.closeLabel ?? texts.cancel).toLowerCase()) {
1232
+ if ([texts.cancel, form.closeLabel].some((word) => word?.toLowerCase() === text.toLowerCase())) {
1223
1233
  await state.delete(key);
1224
1234
  await this.postMessage(threadId, texts.cancelled);
1225
1235
  if (form.notifyOnClose) {
@@ -1329,7 +1339,7 @@ ${questionOf(first, retry, texts)}`);
1329
1339
  return (this._config.addons ?? []).filter((addon) => addon[hook]);
1330
1340
  }
1331
1341
  _context(threadId) {
1332
- return { adapter: this, chat: this._chat, threadId };
1342
+ return { adapter: this, chat: this._chat, richContent: !!this._config.richContent, threadId };
1333
1343
  }
1334
1344
  _seenKey() {
1335
1345
  return `wwebjs:${this.name}:seen`;
@@ -1818,7 +1828,7 @@ function actionsOf(children) {
1818
1828
  (child) => child.type === "actions" ? [child] : child.type === "section" ? actionsOf(child.children) : []
1819
1829
  );
1820
1830
  }
1821
- function layout(card) {
1831
+ function layout(card, richContent) {
1822
1832
  const choices = [];
1823
1833
  const lines = [];
1824
1834
  const offer = (choice) => {
@@ -1827,13 +1837,12 @@ function layout(card) {
1827
1837
  };
1828
1838
  for (const { children } of actionsOf(card.children)) {
1829
1839
  for (const action of children) {
1830
- if (action.type === "link-button") lines.push(`${action.label}: ${action.url}`);
1831
- else if (action.type === "button") {
1840
+ if (action.type === "button") {
1832
1841
  if (!action.disabled && action.actionType !== "modal") {
1833
1842
  offer({ actionId: action.id, label: action.label, value: action.value });
1834
1843
  }
1835
- } else {
1836
- lines.push(`*${action.label}*`);
1844
+ } else if (action.type !== "link-button") {
1845
+ lines.push(richContent ? `*${action.label}*` : action.label);
1837
1846
  for (const option of action.options) {
1838
1847
  offer({ actionId: action.id, label: option.label, value: option.value });
1839
1848
  }
@@ -1847,13 +1856,14 @@ function keyOf({ adapter, threadId }) {
1847
1856
  }
1848
1857
  function numberedActions(options = {}) {
1849
1858
  return {
1850
- renderCard(card) {
1851
- const { lines } = layout(card);
1859
+ renderCard(card, { richContent }) {
1860
+ const { lines } = layout(card, richContent);
1852
1861
  if (lines.length === 0) return void 0;
1853
- return [cardToText(card), lines.join("\n")].filter(Boolean).join("\n\n");
1862
+ const text = richContent ? cardToText(card) : cardToPlainText(card);
1863
+ return [text, lines.join("\n")].filter(Boolean).join("\n\n");
1854
1864
  },
1855
1865
  async cardSent(card, messageId, context) {
1856
- const { choices } = layout(card);
1866
+ const { choices } = layout(card, context.richContent);
1857
1867
  if (choices.length === 0 || !context.chat) return;
1858
1868
  await context.chat.getState().set(keyOf(context), { choices, messageId }, options.ttlMs ?? TTL_MS);
1859
1869
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chat-adapter-wwebjs",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "WhatsApp adapter for Chat SDK, backed by whatsapp-web.js",
5
5
  "author": "nivandres",
6
6
  "license": "MIT",
@@ -50,19 +50,19 @@
50
50
  "whatsapp-web.js": "^1.34.7"
51
51
  },
52
52
  "devDependencies": {
53
- "@chat-adapter/shared": "^4.40.0",
54
- "@chat-adapter/state-memory": "^4.40.0",
55
- "@chat-adapter/tests": "^4.40.0",
53
+ "@chat-adapter/shared": "^4.44.0",
54
+ "@chat-adapter/state-memory": "^4.44.0",
55
+ "@chat-adapter/tests": "^4.44.0",
56
56
  "@types/node": "^22.0.0",
57
57
  "@types/qrcode-terminal": "^0.12.2",
58
- "@vitest/coverage-v8": "^4.0.0",
59
- "chat": "^4.40.0",
58
+ "@vitest/coverage-v8": "^5.1.0",
59
+ "chat": "^4.44.0",
60
60
  "oxlint": "^1.61.0",
61
61
  "prettier": "^3.8.3",
62
62
  "qrcode-terminal": "^0.12.0",
63
63
  "tsup": "^8.3.0",
64
64
  "typescript": "^5.7.0",
65
- "vitest": "^4.0.0",
65
+ "vitest": "^5.1.0",
66
66
  "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#064a3d5"
67
67
  },
68
68
  "publishConfig": {