Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): offer a template choice the prompt left unnumbered

Yudhi Armyndharisb0f0ac84f71b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 17bb8ad3f..d4b4d0874 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -48,7 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A QR code that arrives while the dashboard is asking the server about a disconnect no longer has its modal closed underneath it. The handler blanks the displayed code, re-reads the session list, and closed the modal when the answer said no engine was left; a reconnect completing inside that window had already pushed a fresh, scannable code.
- Escape dismisses the Chats emoji picker instead of closing the whole conversation behind it.
- A click on a WhatsApp Business prompt quotes the prompt in the stored reply, so the dashboard renders the question above the answer instead of an empty quote box.
-- A template prompt that numbers only some of its buttons no longer answers the bot with an index belonging to a different button: a template that carries its own numbering is answered with it, and one that carries none is answered by position.
+- A template prompt that numbers only some of its buttons no longer answers the bot with an index belonging to a different button. A template that carries its own numbering is answered with it, one that carries none is answered by position, and a choice left unnumbered inside a numbered template is answered by id alone, with no index at all. Such a choice is still offered in `buttons[]` and still accepted by the click route.
- A proxy password no longer reaches the log. A failed SOCKS connect carries the whole proxy config as the error's only property, and `BAILEYS_LOG_LEVEL=debug` wrote it to stdout verbatim.
- Baileys sessions list the saved address book again after a process restart. WhatsApp sends neither history nor an app-state snapshot to a device that has synced once, and the contact store is in memory, so a restarted gateway answered `GET /api/sessions/:sessionId/contacts` with only the peers it had seen since. Each session now re-pulls the snapshot of the app-state collection that carries saved contacts, once per engine start. That also repairs a PARTIAL address book, which is the usual shape: during the initial sync the library's event buffer folds the saved-contact updates into the history batch it is already holding, where their names are stripped as chat titles, so the store keeps plenty of contacts with the saved ones missing. The list is also the address book only: a peer known by pushname alone is no longer listed, while `GET .../contacts/:contactId` still resolves it with `isMyContact: false`, and a chat title from history sync no longer overwrites a saved name. Thanks @gabrielmmoraes1999.
- whatsapp-web.js sessions send media again on the WhatsApp Web builds rolled out on 2026-09-17: the library spread the media model into the outgoing message after its id, and the model's private `__x_id` clobbered it, so every image, video, audio, document, sticker and status media send failed with `Data passed to getter must include an id property` while text kept working. An install-time patch carries upstream's one-line fix until a whatsapp-web.js release ships it ([#1643](https://github.com/rmyndharis/OpenWA/issues/1643), [#1636](https://github.com/rmyndharis/OpenWA/issues/1636)). Thanks @15874611923 and @Magnarks for the reports.
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 09d253bc5..5e302ae17 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -2050,7 +2050,7 @@ Tap a choice on a WhatsApp Business button / list prompt by sending the structur
| --------- | ------ | -------- | ------------------- | ---------------------------------------------------------------------------- |
| chatId | string | Yes | non-empty | Chat that holds the prompt |
| messageId | string | Yes | non-empty | WhatsApp id of the inbound prompt (`buttons` on `message.received`) |
-| buttonId | string | Yes | non-empty | Stable id of the choice (`buttons[].id`) |
+| buttonId | string | Yes | non-empty, max 4096 | Stable id of the choice (`buttons[].id`) |
| text | string | No | non-empty, max 4096 | Visible label; resolved from the stored prompt when omitted, else `buttonId` |
```json
diff --git a/openapi.json b/openapi.json
index 8680b71c6..a5cc3e67a 100644
--- a/openapi.json
+++ b/openapi.json
@@ -11976,7 +11976,8 @@
},
"buttonId": {
"type": "string",
- "description": "Stable id of the choice to tap (inbound `buttons[].id`). URL/call CTA buttons cannot be clicked this way."
+ "description": "Stable id of the choice to tap (inbound `buttons[].id`). URL/call CTA buttons cannot be clicked this way.",
+ "maxLength": 4096
},
"text": {
"type": "string",
diff --git a/src/engine/adapters/baileys-message-mapper.spec.ts b/src/engine/adapters/baileys-message-mapper.spec.ts
index dc73f67f9..e197d574f 100644
--- a/src/engine/adapters/baileys-message-mapper.spec.ts
+++ b/src/engine/adapters/baileys-message-mapper.spec.ts
@@ -4,6 +4,7 @@ import {
extractBaileysBody,
extractBaileysButtonReply,
extractBaileysButtons,
+ extractBaileysClickableButtons,
resolveBaileysButtonClick,
toBaileysButtonClickContent,
BUTTONS_MAX_ENTRIES,
@@ -584,32 +585,62 @@ describe('extractBaileysButtons (prompt choices Sim/Não / list rows)', () => {
it('keeps one index namespace for a template that numbers only some of its buttons', () => {
// selectedIndex goes back to the business bot verbatim. Falling back to the array position for
- // an unnumbered entry can hand the bot a number that belongs to a different button, so a
- // template that numbers any of its buttons only offers the ones it numbered.
- expect(
- extractBaileysButtons(
- {
- templateMessage: {
- hydratedTemplate: {
- hydratedButtons: [
- { index: 5, quickReplyButton: { id: 'yes', displayText: 'Sim' } },
- { quickReplyButton: { id: 'maybe', displayText: 'Talvez' } },
- { index: 7, quickReplyButton: { id: 'no', displayText: 'Não' } },
- ],
- },
- },
+ // an unnumbered entry can hand the bot a number belonging to a different button, so an entry the
+ // template did not number carries no index at all. It is still offered: the user can see and tap
+ // it in WhatsApp, and the reply names it by id.
+ const prompt = {
+ templateMessage: {
+ hydratedTemplate: {
+ hydratedButtons: [
+ { index: 5, quickReplyButton: { id: 'yes', displayText: 'Sim' } },
+ { quickReplyButton: { id: 'maybe', displayText: 'Talvez' } },
+ { index: 7, quickReplyButton: { id: 'no', displayText: 'Não' } },
+ ],
},
- 'templateMessage',
- ),
- ).toEqual([
+ },
+ };
+ expect(extractBaileysButtons(prompt, 'templateMessage')).toEqual([
{ id: 'yes', text: 'Sim' },
+ { id: 'maybe', text: 'Talvez' },
{ id: 'no', text: 'Não' },
]);
+ expect(extractBaileysClickableButtons(prompt, 'templateMessage')).toEqual([
+ { id: 'yes', text: 'Sim', index: 5 },
+ { id: 'maybe', text: 'Talvez', index: undefined },
+ { id: 'no', text: 'Não', index: 7 },
+ ]);
+ });
+
+ it('offers the quick replies of a template whose only numbered button is a CTA', () => {
+ // The CTA is never offered, so its index was the only thing making the prompt look numbered.
+ // Requiring an index of every entry emptied the whole prompt, which is how a visible Sim/Não
+ // pair disappeared from buttons[] and was then refused by the click route.
+ const prompt = {
+ templateMessage: {
+ hydratedTemplate: {
+ hydratedButtons: [
+ { index: 1, urlButton: { displayText: 'Open', url: 'https://example.com' } },
+ { quickReplyButton: { id: 'yes', displayText: 'Sim' } },
+ { quickReplyButton: { id: 'no', displayText: 'Não' } },
+ ],
+ },
+ },
+ };
+ expect(extractBaileysButtons(prompt, 'templateMessage')).toEqual([
+ { id: 'yes', text: 'Sim' },
+ { id: 'no', text: 'Não' },
+ ]);
+ expect(extractBaileysClickableButtons(prompt, 'templateMessage')).toEqual([
+ { id: 'yes', text: 'Sim', index: undefined },
+ { id: 'no', text: 'Não', index: undefined },
+ ]);
});
it('numbers by array position when the template carries no indices at all', () => {
+ // Asserted through the clickable list, which keeps the index; extractBaileysButtons projects it
+ // away, so it cannot tell a correct position from any other number.
expect(
- extractBaileysButtons(
+ extractBaileysClickableButtons(
{
templateMessage: {
hydratedTemplate: {
@@ -623,8 +654,8 @@ describe('extractBaileysButtons (prompt choices Sim/Não / list rows)', () => {
'templateMessage',
),
).toEqual([
- { id: 'yes', text: 'Sim' },
- { id: 'no', text: 'Não' },
+ { id: 'yes', text: 'Sim', index: 0 },
+ { id: 'no', text: 'Não', index: 1 },
]);
});
@@ -765,6 +796,38 @@ describe('resolveBaileysButtonClick (API click against a stored prompt)', () =>
});
});
+ it('answers a choice a numbered template left unnumbered with no selectedIndex', () => {
+ // `selectedIndex` has explicit presence on the wire and Baileys drops an undefined field before
+ // encoding, so the bot receives the id and the label and no number at all, which is honest.
+ // Fabricating a position here could name a different button in the template's own numbering.
+ const result = resolveBaileysButtonClick(
+ {
+ templateMessage: {
+ hydratedTemplate: {
+ hydratedButtons: [
+ { index: 5, quickReplyButton: { id: 'yes', displayText: 'Sim' } },
+ { quickReplyButton: { id: 'maybe', displayText: 'Talvez' } },
+ ],
+ },
+ },
+ },
+ 'templateMessage',
+ 'maybe',
+ );
+ expect(result).toEqual({
+ ok: true,
+ payload: {
+ id: 'maybe',
+ text: 'Talvez',
+ index: undefined,
+ content: {
+ buttonReply: { displayText: 'Talvez', id: 'maybe', index: undefined },
+ type: 'template',
+ },
+ },
+ });
+ });
+
it('rejects an unknown buttonId and a non-prompt content type', () => {
expect(
resolveBaileysButtonClick(
diff --git a/src/engine/adapters/baileys-message-mapper.ts b/src/engine/adapters/baileys-message-mapper.ts
index 257b4a231..df90b5c07 100644
--- a/src/engine/adapters/baileys-message-mapper.ts
+++ b/src/engine/adapters/baileys-message-mapper.ts
@@ -397,8 +397,13 @@ const CLICKABLE_NATIVE_FLOW_NAMES = new Set(['quick_reply', 'button_click']);
export interface BaileysClickableChoice {
id: string;
text: string;
- /** `proto.IHydratedTemplateButton.index` when present; otherwise the choice's position. */
- index: number;
+ /**
+ * `proto.IHydratedTemplateButton.index` when the prompt declares one, otherwise the choice's
+ * position. Undefined only for a hydrated template that numbers some of its buttons and not this
+ * one: the reply then omits `selectedIndex` rather than inventing a number that belongs to a
+ * different button. Every other prompt shape numbers by position, so it is always set there.
+ */
+ index?: number;
}
/**
@@ -428,8 +433,11 @@ export type BaileysButtonClickError = 'not_a_prompt' | 'unknown_button';
export interface BaileysButtonClickPayload {
id: string;
text: string;
- /** Index into template hydrated buttons when the prompt is a template; otherwise the choice's position. */
- index: number;
+ /**
+ * Index into template hydrated buttons when the prompt is a template; otherwise the choice's
+ * position. Absent for a choice a numbered template left unnumbered, see {@link BaileysClickableChoice}.
+ */
+ index?: number;
/** `AnyMessageContent` fragment: `{buttonReply,type}` or `{listReply}`. */
content: Record<string, unknown>;
}
@@ -537,8 +545,14 @@ export function extractBaileysClickableButtons(
// hydrated template numbers its buttons itself, so the array position is only a stand-in for a
// template that carries no numbering at all. Falling back per entry mixed the two inside one
// prompt, where a position can collide with another button's declared index and answer the bot
- // with a number belonging to a different choice. An entry missing its index in a template that
- // numbers the others cannot be answered reliably, so it is not offered.
+ // with a number belonging to a different choice.
+ //
+ // An entry the template left unnumbered is still offered, with no index of its own: the field
+ // has explicit presence on the wire, so the reply carries the id and the label and simply omits
+ // `selectedIndex`. Dropping the choice instead would hide a button the user can see and tap in
+ // WhatsApp, and refuse it through the click route, which is a worse answer than one honest
+ // reply that names itself by id. The unnumbered case includes a template whose only numbered
+ // button is a url or call CTA, which is never offered here in the first place.
const numbered = hydrated.some(entry => typeof entry?.index === 'number');
return collectChoices(
hydrated.map((entry, position) => {
@@ -546,8 +560,11 @@ export function extractBaileysClickableButtons(
if (!quick?.displayText?.trim()) return undefined;
const label = quick.displayText.trim();
const protoIndex = numbered ? entry?.index : position;
- if (typeof protoIndex !== 'number') return undefined;
- return { id: quick.id?.trim() || label, text: label, index: protoIndex };
+ return {
+ id: quick.id?.trim() || label,
+ text: label,
+ index: typeof protoIndex === 'number' ? protoIndex : undefined,
+ };
}),
);
}
@@ -590,7 +607,7 @@ export function toBaileysButtonClickContent(
contentType: string,
buttonId: string,
text: string,
- index: number,
+ index: number | undefined,
): Record<string, unknown> {
switch (contentType) {
case 'buttonsMessage':
diff --git a/src/modules/message/dto/message-actions.dto.ts b/src/modules/message/dto/message-actions.dto.ts
index b1c5da51c..ef56ca609 100644
--- a/src/modules/message/dto/message-actions.dto.ts
+++ b/src/modules/message/dto/message-actions.dto.ts
@@ -301,6 +301,7 @@ export class ClickButtonDto {
@ApiProperty({
description:
'Stable id of the choice to tap (inbound `buttons[].id`). URL/call CTA buttons cannot be clicked this way.',
+ maxLength: MESSAGE_TEXT_MAX_LENGTH,
})
@IsString()
@IsNotEmpty()