Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): add sender author + contact to incoming message payload; doc fixes (#223)
Yudhi Armyndharis8983e5400f13
diff --git a/README.md b/README.mdindex 0b8b3e15a..18a346fba 100644--- a/README.md+++ b/README.md@@ -40,6 +40,7 @@ Built on a **pluggable architecture**, OpenWA lets you swap database engines (SQ| πΉ **Multi-Session Ready** | Run multiple WhatsApp sessions concurrently on one instance || π³ **Docker Native** | Production-ready with zero configuration || π **n8n Integration** | Community nodes for workflow automation |+| π§© **Community Adapters** | Third-party integrations (e.g. ioBroker) β see [docs](./docs/23-community-integrations.md) |---diff --git a/docs/07-api-collection.md b/docs/07-api-collection.mdindex 98cf903fb..400e8f722 100644--- a/docs/07-api-collection.md+++ b/docs/07-api-collection.md@@ -435,40 +435,16 @@ curl -X POST http://localhost:2785/api/sessions/default/messages \}```-**Request Body - Buttons (Interactive):**-```json-{- "phone": "[email protected]",- "type": "buttons",- "body": "Please choose an option:",- "buttons": [- { "id": "btn1", "text": "Option 1" },- { "id": "btn2", "text": "Option 2" },- { "id": "btn3", "text": "Option 3" }- ],- "footer": "Powered by OpenWA"-}-```--**Request Body - List (Interactive):**-```json-{- "phone": "[email protected]",- "type": "list",- "body": "Please select from the menu:",- "buttonText": "View Menu",- "sections": [- {- "title": "Category 1",- "rows": [- { "id": "item1", "title": "Item 1", "description": "Description 1" },- { "id": "item2", "title": "Item 2", "description": "Description 2" }- ]- }- ],- "footer": "Powered by OpenWA"-}-```+**Interactive messages (Buttons / List): not supported**++> β οΈ **Buttons and List (interactive) messages are not available** through OpenWA's+> whatsapp-web.js engine. WhatsApp stopped honoring the interactive-message payload+> for unofficial web clients around 2021β2022 β the `Buttons` / `List` classes still+> exist in the library, but messages built with them are **silently dropped and never+> delivered** to recipients. OpenWA therefore does not expose `type: "buttons"` or+> `type: "list"` endpoints; sending interactive messages requires the official+> WhatsApp Business Cloud API. (The earlier examples here were speculative and never+> implemented β see #158.)**Response:**```jsondiff --git a/docs/23-community-integrations.md b/docs/23-community-integrations.mdnew file mode 100644index 000000000..b718fbbfb--- /dev/null+++ b/docs/23-community-integrations.md@@ -0,0 +1,15 @@+# Community Integrations++Third-party integrations and adapters built by the community on top of OpenWA's REST API.++> β οΈ **These projects are community-maintained and are not affiliated with or endorsed by OpenWA.**+> They may lag behind OpenWA API changes. Verify compatibility and review the source before using+> them in production.++| Integration | Platform | Source | Notes |+| --- | --- | --- | --- |+| **ioBroker.openwa** | [ioBroker](https://www.iobroker.net/) (home / IoT automation) | [ThorstenBoettler/ioBroker.openwa](https://github.com/ThorstenBoettler/ioBroker.openwa) Β· [npm](https://www.npmjs.com/package/iobroker.openwa) | Send text / image / video / audio / document messages to chats and groups through OpenWA's REST API, with Blockly blocks. Early stage. |++Maintain an integration built on OpenWA and want it listed here? Open a PR adding a row (or an issue), including the repository link and a one-line description.++For the first-party n8n nodes, see [n8n Integration](./22-n8n-integration.md).diff --git a/docs/README.md b/docs/README.mdindex d90ae0a1f..6c43a450e 100644--- a/docs/README.md+++ b/docs/README.md@@ -55,6 +55,7 @@| 20 | [Community Guidelines](./20-community-guidelines.md) | Contribution and governance || 21 | [Glossary](./21-glossary.md) | Terms and definitions || 22 | [n8n Integration](./22-n8n-integration.md) | n8n community nodes for OpenWA |+| 23 | [Community Integrations](./23-community-integrations.md) | Third-party adapters built on the OpenWA API |## Quick Startdiff --git a/src/engine/adapters/message-mapper.spec.ts b/src/engine/adapters/message-mapper.spec.tsnew file mode 100644index 000000000..b237a76f3--- /dev/null+++ b/src/engine/adapters/message-mapper.spec.ts@@ -0,0 +1,40 @@+import { buildIncomingMessageBase, RawMessageFields } from './message-mapper';++describe('buildIncomingMessageBase', () => {+ const base: RawMessageFields = {+ id: { _serialized: 'MSG1' },+ from: '[email protected]',+ to: '[email protected]',+ body: 'hi',+ type: 'chat',+ timestamp: 1700000000,+ fromMe: false,+ };++ it('maps the core fields and flags 1:1 chats as non-group', () => {+ const r = buildIncomingMessageBase(base);+ expect(r.id).toBe('MSG1');+ expect(r.chatId).toBe('[email protected]');+ expect(r.isGroup).toBe(false);+ expect(r.author).toBeUndefined();+ expect(r.contact).toBeUndefined();+ });++ it('includes author and pushName for a group message', () => {+ const r = buildIncomingMessageBase({+ ...base,+ from: '[email protected]',+ author: '[email protected]',+ _data: { notifyName: 'Alice' },+ });+ expect(r.isGroup).toBe(true);+ expect(r.author).toBe('[email protected]');+ expect(r.contact).toEqual({ pushName: 'Alice' });+ });++ it('omits contact when no push name is present', () => {+ const r = buildIncomingMessageBase({ ...base, author: '[email protected]' });+ expect(r.author).toBe('[email protected]');+ expect(r.contact).toBeUndefined();+ });+});diff --git a/src/engine/adapters/message-mapper.ts b/src/engine/adapters/message-mapper.tsnew file mode 100644index 000000000..7de7a8017--- /dev/null+++ b/src/engine/adapters/message-mapper.ts@@ -0,0 +1,52 @@+import { IncomingMessage } from '../interfaces/whatsapp-engine.interface';++/**+ * The subset of whatsapp-web.js `Message` fields we read synchronously to build+ * the base of an {@link IncomingMessage}. Declared explicitly so the mapping is+ * unit-testable without constructing a full wwebjs `Message`.+ */+export interface RawMessageFields {+ id: { _serialized: string };+ from: string;+ to: string;+ body: string;+ type: string;+ timestamp: number;+ fromMe: boolean;+ /** Set on group messages: the participant WID that actually sent the message. */+ author?: string;+ /** Raw wwebjs payload; `notifyName` carries the sender's push name without an extra lookup. */+ _data?: { notifyName?: string };+}++/**+ * Build the synchronous base of an IncomingMessage from a raw wwebjs message.+ * Async enrichment (media, quoted message, saved-contact name) is layered on by+ * the adapter; this covers the fields available without an await.+ */+export function buildIncomingMessageBase(msg: RawMessageFields): IncomingMessage {+ const incoming: IncomingMessage = {+ id: msg.id._serialized,+ from: msg.from,+ to: msg.to,+ chatId: msg.from,+ body: msg.body,+ type: msg.type,+ timestamp: msg.timestamp,+ fromMe: msg.fromMe,+ isGroup: msg.from.endsWith('@g.us'),+ };++ // In a group, `from` is the group JID, so `author` is the only way to know the real sender.+ if (msg.author) {+ incoming.author = msg.author;+ }++ // Push name is available synchronously on the raw payload β no contact lookup needed.+ const pushName = msg._data?.notifyName;+ if (pushName) {+ incoming.contact = { pushName };+ }++ return incoming;+}diff --git a/src/engine/adapters/whatsapp-web-js.adapter.ts b/src/engine/adapters/whatsapp-web-js.adapter.tsindex acc853a12..4fa038fcf 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.ts@@ -35,6 +35,7 @@ import {WwjsChannelData,GroupCreateResult,} from '../types/whatsapp-web-js.types';+import { buildIncomingMessageBase } from './message-mapper';export interface WhatsAppWebJsConfig {sessionId: string;@@ -143,17 +144,21 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin// eslint-disable-next-line @typescript-eslint/no-misused-promisesthis.client.on('message', async msg => {try {- const incomingMessage: IncomingMessage = {- id: msg.id._serialized,- from: msg.from,- to: msg.to,- chatId: msg.from,- body: msg.body,- type: msg.type,- timestamp: msg.timestamp,- fromMe: msg.fromMe,- isGroup: msg.from.endsWith('@g.us'),- };+ const incomingMessage: IncomingMessage = buildIncomingMessageBase(msg);++ // Enrich the sender contact with the saved name (best-effort, from the WhatsApp Web cache).+ // `author`/`from` resolve to the actual sender for group and 1:1 messages respectively.+ try {+ const contact = await msg.getContact();+ if (contact?.name || contact?.pushname) {+ incomingMessage.contact = {+ name: contact.name || incomingMessage.contact?.name,+ pushName: contact.pushname || incomingMessage.contact?.pushName,+ };+ }+ } catch (error) {+ this.logger.error('Error getting message contact', String(error));+ }// Handle locationif (msg.type === MessageTypes.LOCATION && msg.location) {diff --git a/src/engine/interfaces/whatsapp-engine.interface.ts b/src/engine/interfaces/whatsapp-engine.interface.tsindex 4dd6f6eff..d9c1ea8d8 100644--- a/src/engine/interfaces/whatsapp-engine.interface.ts+++ b/src/engine/interfaces/whatsapp-engine.interface.ts@@ -32,6 +32,13 @@ export interface IncomingMessage {timestamp: number;fromMe: boolean;isGroup: boolean;+ /** For group messages, the WID of the participant who actually sent it (`from` is the group JID there). */+ author?: string;+ /** Sender display info, best-effort from the WhatsApp Web contact cache. */+ contact?: {+ name?: string;+ pushName?: string;+ };media?: {mimetype: string;filename?: string;