Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): quote the prompt on a click, and keep one index namespace
Yudhi Armyndharis3155632f365c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4feaf42ce..eb59d3b07 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- An ingress route whose manifest carries a non-string ack body, status or header is answered rather than failing with `500`; a manifest is third-party JSON and the loader does not type-check every leaf.- Uploading a zip whose trailer parses but whose directory does not answers `400`, not `500`.- A `socks5://` or `socks4://` proxy at an IPv6 literal connects: the brackets `URL` keeps were going on the wire as part of a hostname.+- 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 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/src/engine/adapters/baileys-message-mapper.spec.ts b/src/engine/adapters/baileys-message-mapper.spec.tsindex 153a15475..9b6772445 100644--- a/src/engine/adapters/baileys-message-mapper.spec.ts+++ b/src/engine/adapters/baileys-message-mapper.spec.ts@@ -582,6 +582,52 @@ describe('extractBaileysButtons (prompt choices Sim/Não / list rows)', () => {).toEqual([{ id: 'yes', text: 'Sim' }]);});+ 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' } },+ ],+ },+ },+ },+ 'templateMessage',+ ),+ ).toEqual([+ { id: 'yes', text: 'Sim' },+ { id: 'no', text: 'Não' },+ ]);+ });++ it('numbers by array position when the template carries no indices at all', () => {+ expect(+ extractBaileysButtons(+ {+ templateMessage: {+ hydratedTemplate: {+ hydratedButtons: [+ { quickReplyButton: { id: 'yes', displayText: 'Sim' } },+ { quickReplyButton: { id: 'no', displayText: 'Não' } },+ ],+ },+ },+ },+ 'templateMessage',+ ),+ ).toEqual([+ { id: 'yes', text: 'Sim' },+ { id: 'no', text: 'Não' },+ ]);+ });+it('extracts interactiveMessage native-flow quick replies', () => {expect(extractBaileysButtons(diff --git a/src/engine/adapters/baileys-message-mapper.ts b/src/engine/adapters/baileys-message-mapper.tsindex a8886a774..77bab15a4 100644--- a/src/engine/adapters/baileys-message-mapper.ts+++ b/src/engine/adapters/baileys-message-mapper.ts@@ -529,12 +529,20 @@ export function extractBaileysClickableButtons(content.templateMessage?.hydratedTemplate?.hydratedButtons ??content.templateMessage?.hydratedFourRowTemplate?.hydratedButtons ??[];+ // One index namespace per prompt. `selectedIndex` goes back to the business bot verbatim, and a+ // 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.+ const numbered = hydrated.some(entry => typeof entry?.index === 'number');return collectChoices(hydrated.map((entry, position) => {const quick = entry?.quickReplyButton;if (!quick?.displayText?.trim()) return undefined;const label = quick.displayText.trim();- const protoIndex = typeof entry?.index === 'number' ? entry.index : position;+ const protoIndex = numbered ? entry?.index : position;+ if (typeof protoIndex !== 'number') return undefined;return { id: quick.id?.trim() || label, text: label, index: protoIndex };}),);diff --git a/src/modules/message/message-send.service.spec.ts b/src/modules/message/message-send.service.spec.tsindex fcdc8c3d8..a3ef86904 100644--- a/src/modules/message/message-send.service.spec.ts+++ b/src/modules/message/message-send.service.spec.ts@@ -1033,6 +1033,26 @@ describe('MessageSendService', () => {);});+ it('quotes the prompt body so the dashboard renders the answered prompt, not an empty box', async () => {+ // A click IS a reply to the prompt. The quote box is rendered from this metadata, and it was+ // hardcoded empty while the reply path resolved the same field from the stored message.+ (repository.findOne as jest.Mock).mockResolvedValueOnce({ id: 'row-1', body: 'Confirmar o pedido?' });++ await service.clickButton('sess-1', {+ chatId: '[email protected]',+ messageId: 'PROMPT-1',+ buttonId: 'yes',+ });++ expect(repository.create).toHaveBeenCalledWith(+ expect.objectContaining({+ metadata: expect.objectContaining({+ quotedMessage: { id: 'PROMPT-1', body: 'Confirmar o pedido?' },+ }) as unknown,+ }),+ );+ });+it('routes an engine refusal through failSend so the pending row is marked failed', async () => {mockEngine.clickButton.mockRejectedValueOnce(new BadRequestException('message PROMPT-1 is not a WhatsApp Business button/list prompt that can be clicked'),diff --git a/src/modules/message/message-send.service.ts b/src/modules/message/message-send.service.tsindex 9ba0f8b53..236fefca4 100644--- a/src/modules/message/message-send.service.ts+++ b/src/modules/message/message-send.service.ts@@ -523,12 +523,25 @@ export class MessageSendService {const finalDto = await this.applySendingGate(sessionId, 'click-button', dto);const engine = this.getEngine(sessionId);+ // A click IS a reply to the prompt, so resolve the prompt's body the way reply() does: the+ // dashboard renders the quote box from this field, and a hardcoded empty string left every+ // answered prompt showing an empty quote above the choice the user tapped.+ let promptBody = '';+ try {+ const prompt = await this.messageRepository.findOne({+ where: { sessionId, waMessageId: finalDto.messageId },+ });+ promptBody = prompt?.body || '';+ } catch (err) {+ this.logger.warn(`Failed to resolve prompt message ${finalDto.messageId}`, { error: String(err) });+ }+const message = await this.saveOutgoingMessage(sessionId, {chatId: finalDto.chatId,body: finalDto.text || finalDto.buttonId,type: 'text',metadata: {- quotedMessage: { id: finalDto.messageId, body: '' },+ quotedMessage: { id: finalDto.messageId, body: promptBody },button: { id: finalDto.buttonId, text: finalDto.text },},});