Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(engine): read contact stories on the whatsapp-web.js engine (#718)

Yudhi Armyndharisaec8f85d649c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e03aa3334..397ca52dd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -90,6 +90,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
the status lifecycle (post + delete) on the default engine. Own-status only (the library revokes
the caller's own status posts).
+- **Read contact stories on the whatsapp-web.js engine.** `getContactStatuses()` and
+ `getContactStatus(contactId)` now return contact "stories" (24h status posts) via
+ whatsapp-web.js `getBroadcasts()` / `getBroadcastById()` flattened to `Status[]` (contact via
+ `broadcast.getContact()`, type from `MessageTypes`, 24h TTL) instead of stubbing to `[]`. The
+ Baileys engine still cannot read stories — `fetchStatus` returns the *about* text, not stories
+ (documented as a library limitation).
+
### Fixed
- **Diagnosable failure for a stale browser profile after a binary-changing upgrade.** Upgrading
diff --git a/docs/engine-capability-matrix.md b/docs/engine-capability-matrix.md
index b7ce05775..3370d3a7f 100644
--- a/docs/engine-capability-matrix.md
+++ b/docs/engine-capability-matrix.md
@@ -16,7 +16,7 @@ Each row also cites an **evidence** string (the exact library file:symbol inspec
The `rootCause`/`evidence` fields are hand-curated from source traces of the installed libraries (`node_modules/@whiskeysockets/baileys`, `node_modules/whatsapp-web.js`); they are not regenerated by the spec and are updated by hand as adapters are wired or libraries change.
-> **Phantom support.** The drift gate's throw-heuristic cannot see adapter methods that silently stub (return `null`/`[]` + a warn log) without throwing. Five wwjs entries — `getCatalog`, `getProducts`, `getProduct`, `getContactStatus`, `getContactStatuses` — are marked `not-available` in the matrix even though their adapter bodies do not throw, because the library either has no API (catalog) or the adapter stubs instead of calling the available symbol (status read). When the drift gate is extended to assert against this matrix, it must consult `status`, not just the throw pattern, for these rows (or the adapter stubs must start throwing).
+> **Phantom support.** The drift gate's throw-heuristic cannot see adapter methods that silently stub (return `null`/`[]` + a warn log) without throwing. Three wwjs entries — `getCatalog`, `getProducts`, `getProduct` — are marked `not-available` in the matrix even though their adapter bodies do not throw, because the library has no API for them. When the drift gate is extended to assert against this matrix, it must consult `status`, not just the throw pattern, for these rows (or the adapter stubs must start throwing).
---
@@ -77,10 +77,10 @@ The `rootCause`/`evidence` fields are hand-curated from source traces of the ins
| Method | baileys | wwjs |
|---|---|---|
-| `getContactStatus` | not-available — **library-limitation** | not-available — **adapter-gap** |
-| `getContactStatuses` | not-available — **library-limitation** | not-available — **adapter-gap** |
+| `getContactStatus` | not-available — **library-limitation** | supported |
+| `getContactStatuses` | not-available — **library-limitation** | supported |
-- **`getContactStatus` / `getContactStatuses` (wwjs, adapter-gap).** The library HAS the getters — `Client.getBroadcastById(contactId)` (`index.d.ts:136`; `Client.js:2771`) and `Client.getBroadcasts()` (`index.d.ts:133`; `Client.js:2759` → `WWebJS.getAllStatuses`). Each `Broadcast.msgs: Message[]`. The adapter stubs to `[]` without calling them (`whatsapp-web-js.adapter.ts:1732,1740`). Wiring: `getBroadcasts()` then flatten — for each `Broadcast`, map `b.msgs` to `Status[]` with the contact resolved via `broadcast.getContact()`.
+> **Wired.** ✅ `getContactStatus` / `getContactStatuses` on whatsapp-web.js — `getBroadcastById(id)` / `getBroadcasts()` flattened to `Status[]` (contact via `broadcast.getContact()`; type from `MessageTypes`; 24h TTL). **Caveat:** `Status.type` is the `text|image|video` union — audio/other story types collapse to `text`.
- **`getContactStatus` / `getContactStatuses` (baileys, library-limitation).** `fetchStatus` (`Socket/chats.d.ts:42` via `USyncStatusProtocol`) returns the *about/profile text* line (`{status, setAt}`), **not** 24h stories. No story-read getter exists; story broadcasts surface only as `status@broadcast` messages via `messages.upsert` / `messaging-history.set` events. Would require OpenWA to accumulate `status@broadcast` messages itself and project them into `Status[]`.
### Messaging misc — delete / history / reactions
@@ -115,8 +115,6 @@ All Tier-1 adapter-gaps have been wired:
| 7 | `getChannelById` : **baileys** | `sock.newsletterMetadata('jid', channelId)` → map `NewsletterMetadata` (`Socket/newsletter.d.ts:9`) | **S** | Channel/newsletter parity. ~1-liner field map. |
| 8 | `unsubscribeFromChannel` : **baileys** | `await sock.newsletterUnfollow(channelId)` (`Socket/newsletter.d.ts:11`) | **S** | 1:1, no jid resolution needed. |
| 9 | `subscribeToChannel` : **baileys** | 2-step: `newsletterMetadata('invite',code).id` then `newsletterFollow(jid)` (`Socket/newsletter.d.ts:9,10`) | **S** | Channel growth (join by invite code). |
-| 10 | `getContactStatuses` : **wwjs** | `client.getBroadcasts()` → flatten `Broadcast.msgs` to `Status[]` (`index.d.ts:133`) | **M** | Read contact stories on the default engine. Adapter already has the stub; replace `[]` with the call + mapping. |
-| 11 | `getContactStatus` : **wwjs** | `client.getBroadcastById(contactId)` → map `Broadcast.msgs` (`index.d.ts:136`) | **M** | Per-contact story read. Pairs with #10. |
### Tier 3 — medium effort
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index e502ce827..9f1b7beb2 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -1126,6 +1126,44 @@ describe('WhatsAppWebJsAdapter status methods', () => {
await readyAdapter({ sendMessage: jest.fn(), revokeStatusMessage }).deleteStatus('STATUS1');
expect(revokeStatusMessage).toHaveBeenCalledWith('STATUS1');
});
+
+ const storyBroadcast = {
+ getContact: () => Promise.resolve({ id: { _serialized: '[email protected]' }, name: 'Alice', pushname: 'Alice' }),
+ msgs: [
+ { id: { _serialized: 'ST1' }, type: 'image', body: 'cap1', timestamp: 1700000020 },
+ { id: { _serialized: 'ST2' }, type: 'chat', body: 'hello', timestamp: 1700000021 },
+ ],
+ };
+
+ it('getContactStatuses reads contact stories via getBroadcasts() mapped to Status[]', async () => {
+ const getBroadcasts = jest.fn().mockResolvedValue([storyBroadcast]);
+ const result = await readyAdapter({ sendMessage: jest.fn(), getBroadcasts }).getContactStatuses();
+ expect(getBroadcasts).toHaveBeenCalled();
+ expect(result).toHaveLength(2);
+ expect(result[0]).toEqual({
+ id: 'ST1',
+ contact: { id: '[email protected]', name: 'Alice', pushName: 'Alice' },
+ type: 'image',
+ caption: 'cap1',
+ timestamp: new Date(1700000020 * 1000),
+ expiresAt: new Date(1700000020 * 1000 + 24 * 3_600_000),
+ });
+ expect(result[1].type).toBe('text');
+ });
+
+ it('getContactStatus reads one contact stories via getBroadcastById mapped to Status[]', async () => {
+ const getBroadcastById = jest.fn().mockResolvedValue(storyBroadcast);
+ const result = await readyAdapter({ sendMessage: jest.fn(), getBroadcastById }).getContactStatus('[email protected]');
+ expect(getBroadcastById).toHaveBeenCalledWith('[email protected]');
+ expect(result).toHaveLength(2);
+ expect(result[0]).toEqual(
+ expect.objectContaining({
+ id: 'ST1',
+ type: 'image',
+ contact: { id: '[email protected]', name: 'Alice', pushName: 'Alice' },
+ }),
+ );
+ });
});
describe('resolveWebVersionPin (#251/#488 — explicit pin + auto-resolve current WA-Web build)', () => {
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.ts b/src/engine/adapters/whatsapp-web-js.adapter.ts
index 7d436a385..77136fc5d 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.ts
@@ -1731,16 +1731,47 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
async getContactStatuses(): Promise<Status[]> {
this.ensureReady();
- // whatsapp-web.js has limited Status API support
- // This is a stub that can be enhanced when the library adds support
- this.logger.warn('getContactStatuses not fully implemented in whatsapp-web.js');
- return [];
+ return this.collectStatuses(await this.client!.getBroadcasts());
}
- async getContactStatus(_contactId: string): Promise<Status[]> {
+ async getContactStatus(contactId: string): Promise<Status[]> {
this.ensureReady();
- this.logger.warn('getContactStatus not fully implemented in whatsapp-web.js');
- return [];
+ return this.collectStatuses([await this.client!.getBroadcastById(contactId)]);
+ }
+
+ /**
+ * Map whatsapp-web.js story Broadcasts (+ their Messages) into the neutral Status shape. Each
+ * Broadcast is one contact's story (its `msgs`); we flatten across broadcasts. type collapses to
+ * the Status union (image/video, else text — audio/other stories are rare and become 'text').
+ * expiresAt is timestamp + 24h (WhatsApp status TTL).
+ */
+ private async collectStatuses(
+ broadcasts: ReadonlyArray<{
+ msgs: Message[];
+ getContact: () => Promise<{ id: { _serialized: string }; name?: string; pushname?: string }>;
+ }>,
+ ): Promise<Status[]> {
+ const statuses: Status[] = [];
+ for (const broadcast of broadcasts) {
+ const contact = await broadcast.getContact();
+ const contactSummary = {
+ id: contact.id._serialized,
+ ...(contact.name ? { name: contact.name } : {}),
+ ...(contact.pushname ? { pushName: contact.pushname } : {}),
+ };
+ for (const msg of broadcast.msgs) {
+ const ts = new Date(msg.timestamp * 1000);
+ statuses.push({
+ id: msg.id._serialized,
+ contact: contactSummary,
+ type: msg.type === MessageTypes.IMAGE ? 'image' : msg.type === MessageTypes.VIDEO ? 'video' : 'text',
+ ...(msg.body ? { caption: msg.body } : {}),
+ timestamp: ts,
+ expiresAt: new Date(ts.getTime() + 24 * 3_600_000),
+ });
+ }
+ }
+ return statuses;
}
private warnedStatusRecipients = false;
diff --git a/src/engine/engine-capability-matrix.ts b/src/engine/engine-capability-matrix.ts
index 4d7d6db25..ebf7ab5d1 100644
--- a/src/engine/engine-capability-matrix.ts
+++ b/src/engine/engine-capability-matrix.ts
@@ -99,16 +99,15 @@ export const ENGINE_CAPABILITY_MATRIX: Record<string, MethodCapability> = {
getChats: { wwjs: { status: 'supported' }, baileys: { status: 'supported' } },
getContactById: { wwjs: { status: 'supported' }, baileys: { status: 'supported' } },
getContactStatus: {
- wwjs: { status: 'not-available', rootCause: 'adapter-gap' },
+ wwjs: { status: 'supported' },
baileys: { status: 'not-available', rootCause: 'library-limitation' },
evidence:
- 'baileys fetchStatus (Socket/chats.d.ts:42 via USyncStatusProtocol) = about/profile text only, NOT 24h stories — no story getter in lib; wwjs Client.getBroadcastById (index.d.ts:136; Client.js:2771) EXISTS but adapter stubs to [] @whatsapp-web-js.adapter.ts:1740',
+ 'baileys fetchStatus (Socket/chats.d.ts:42 via USyncStatusProtocol) = about/profile text only, NOT 24h stories — no story getter in lib',
},
getContactStatuses: {
- wwjs: { status: 'not-available', rootCause: 'adapter-gap' },
+ wwjs: { status: 'supported' },
baileys: { status: 'not-available', rootCause: 'library-limitation' },
- evidence:
- 'baileys fetchStatus = about text only; no story enumerate in lib; wwjs Client.getBroadcasts (index.d.ts:133; Client.js:2759 WWebJS.getAllStatuses) EXISTS but adapter stubs to [] @whatsapp-web-js.adapter.ts:1732',
+ evidence: 'baileys fetchStatus = about text only; no story enumerate in lib',
},
getContacts: { wwjs: { status: 'supported' }, baileys: { status: 'supported' } },
getGroupInfo: { wwjs: { status: 'supported' }, baileys: { status: 'supported' } },