chat-adapter-wwebjs 0.1.3 → 0.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/CHANGELOG.md +20 -0
- package/README.md +19 -9
- package/dist/index.d.ts +2 -0
- package/dist/index.js +23 -13
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# chat-adapter-wwebjs
|
|
2
2
|
|
|
3
|
+
## 0.1.5
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
|
|
7
|
+
- The README and the example imported `LocalAuth` by name, which throws under Node: whatsapp-web.js is CommonJS, so ESM sees only its default export. They now import it as `wwebjs` and take `LocalAuth` from it.
|
|
8
|
+
- The README's install failed on pnpm 10, which refuses to build whatsapp-web.js from GitHub and skips Puppeteer's Chromium download until both are allowed in `onlyBuiltDependencies`. It now installs the tested commit in one command and says how to allow both.
|
|
9
|
+
- The README now warns that the `LocalAuth` directory signs in as your WhatsApp account and belongs out of version control.
|
|
10
|
+
|
|
11
|
+
## 0.1.4
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- 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`.
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- 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.
|
|
20
|
+
- A card's link buttons were dropped unless `numberedActions()` listed them. They now go out as `Label: url` lines in every card.
|
|
21
|
+
- A modal with a `closeLabel` stopped only on that word, although its hint tells people to send _cancel_. Either now stops it.
|
|
22
|
+
|
|
3
23
|
## 0.1.3
|
|
4
24
|
|
|
5
25
|
### Added
|
package/README.md
CHANGED
|
@@ -22,12 +22,20 @@ Documentation: [chat-sdk.dev/adapters/community/wwebjs](https://chat-sdk.dev/ada
|
|
|
22
22
|
## Installation
|
|
23
23
|
|
|
24
24
|
```bash
|
|
25
|
-
pnpm add chat-adapter-wwebjs
|
|
25
|
+
pnpm add chat-adapter-wwebjs chat @chat-adapter/shared github:pedroslopez/whatsapp-web.js#064a3d5
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
ESM only, Node >= 20. `chat`, `@chat-adapter/shared` and `whatsapp-web.js` are peers, so you control their versions. whatsapp-web.js
|
|
28
|
+
ESM only, Node >= 20. `chat`, `@chat-adapter/shared` and `whatsapp-web.js` are peers, so you control their versions. WhatsApp Web renamed the IDs whatsapp-web.js reads, and 1.34.7 cannot send or find chats since, so until a release carries the fix, install the commit this adapter is tested against, as above.
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
whatsapp-web.js builds when installed from GitHub, and Puppeteer downloads a Chromium build on install. pnpm 10 and later run those scripts only when you allow them, in `pnpm-workspace.yaml`:
|
|
31
|
+
|
|
32
|
+
```yaml
|
|
33
|
+
onlyBuiltDependencies:
|
|
34
|
+
- whatsapp-web.js
|
|
35
|
+
- puppeteer
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
pnpm pins the commit in the lockfile rather than in `package.json`, so commit the lockfile.
|
|
31
39
|
|
|
32
40
|
## Usage
|
|
33
41
|
|
|
@@ -36,7 +44,9 @@ import { createMemoryState } from "@chat-adapter/state-memory";
|
|
|
36
44
|
import { Chat } from "chat";
|
|
37
45
|
import { createWWebJSAdapter } from "chat-adapter-wwebjs";
|
|
38
46
|
import qrcode from "qrcode-terminal";
|
|
39
|
-
import
|
|
47
|
+
import wwebjs from "whatsapp-web.js";
|
|
48
|
+
|
|
49
|
+
const { LocalAuth } = wwebjs;
|
|
40
50
|
|
|
41
51
|
const whatsapp = createWWebJSAdapter({
|
|
42
52
|
adapterName: "whatsapp",
|
|
@@ -75,7 +85,7 @@ Puppeteer's bundled Chromium covers most machines. On slim Linux images install
|
|
|
75
85
|
|
|
76
86
|
### 2. Keep the session
|
|
77
87
|
|
|
78
|
-
Pick an auth strategy from whatsapp-web.js. `LocalAuth` stores the session in a directory, `RemoteAuth` in a store you provide. Persist it, or you scan again on every start.
|
|
88
|
+
Pick an auth strategy from whatsapp-web.js. `LocalAuth` stores the session in a directory, `RemoteAuth` in a store you provide. Persist it, or you scan again on every start. The session signs in as your WhatsApp account, so keep its directory out of version control, for example by adding `.wwebjs-auth` to `.gitignore`.
|
|
79
89
|
|
|
80
90
|
### 3. Link the device
|
|
81
91
|
|
|
@@ -220,7 +230,7 @@ Anything not wrapped is still reachable through `adapter.client`.
|
|
|
220
230
|
|
|
221
231
|
## Card actions
|
|
222
232
|
|
|
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.
|
|
233
|
+
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
234
|
|
|
225
235
|
```typescript
|
|
226
236
|
import { createWWebJSAdapter, numberedActions } from "chat-adapter-wwebjs";
|
|
@@ -238,7 +248,7 @@ An addon is a plain object with optional `renderCard`, `cardSent` and `onMessage
|
|
|
238
248
|
|
|
239
249
|
## Rich content
|
|
240
250
|
|
|
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).
|
|
251
|
+
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
252
|
|
|
243
253
|
## Streaming
|
|
244
254
|
|
|
@@ -296,7 +306,7 @@ bot.onModalSubmit("booking", (event) =>
|
|
|
296
306
|
| `Select`, `RadioSelect` | An option's number or label |
|
|
297
307
|
| `ExternalSelect` | Words to search, which reach `onOptionsLoad`, then a number from what it found |
|
|
298
308
|
|
|
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
|
|
309
|
+
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
310
|
|
|
301
311
|
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
312
|
|
|
@@ -407,7 +417,7 @@ Examples: `wwebjs:15551234567@c.us` (DM by phone), `wwebjs:99990000111122@lid` (
|
|
|
407
417
|
- **Unofficial.** It automates WhatsApp Web, which WhatsApp may block or change at any time.
|
|
408
418
|
- **One session per number**, as WhatsApp Web itself allows.
|
|
409
419
|
- **No scheduling or ephemeral messages.** `postEphemeral` falls back to a DM; `thread.schedule()` throws.
|
|
410
|
-
-
|
|
420
|
+
- **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
421
|
- **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
422
|
- **A bare `@number` in outgoing text stays text.** Mention someone with `thread.mentionUser(id)`.
|
|
413
423
|
- **`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(
|
|
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 (
|
|
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 === "
|
|
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
|
-
|
|
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
|
+
"version": "0.1.5",
|
|
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.
|
|
54
|
-
"@chat-adapter/state-memory": "^4.
|
|
55
|
-
"@chat-adapter/tests": "^4.
|
|
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": "^
|
|
59
|
-
"chat": "^4.
|
|
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": "^
|
|
65
|
+
"vitest": "^5.1.0",
|
|
66
66
|
"whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#064a3d5"
|
|
67
67
|
},
|
|
68
68
|
"publishConfig": {
|