GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
fix(webhook): expose top-level InteractiveMessage buttons in payload.buttons (#877)
Elio Bonfim Júnioraca8afc9200c
diff --git a/docs/webhook-payload.md b/docs/webhook-payload.mdindex b1c4deab..07a03010 100644--- a/docs/webhook-payload.md+++ b/docs/webhook-payload.md@@ -1291,7 +1291,7 @@ Sent by WhatsApp Business / Cloud API senders (order confirmations, OTPs, bookin### Buttons Message-Same shape as a template, under `buttons`. `body` renders it the same way (`Buttons message` when there is no text):+Same shape as a template, under `buttons`. `body` renders it the same way (`Buttons message` when there is no text). A top-level interactive message with native-flow buttons (Cloud API reply buttons, CTAs, single select) also fills `buttons`, next to its `interactive` text — so a client can tell it apart and answer it as a native-flow reply:```json"buttons": {diff --git a/src/pkg/utils/business_message.go b/src/pkg/utils/business_message.goindex 8c28ea19..bb637161 100644--- a/src/pkg/utils/business_message.go+++ b/src/pkg/utils/business_message.go@@ -147,6 +147,14 @@ func hydratedTemplateButton(button *waE2E.HydratedTemplateButton) (BusinessButtofunc BuildButtonsPayload(msg *waE2E.Message) *BusinessMessage {bm := msg.GetButtonsMessage()if bm == nil {+ // Cloud API reply buttons, CTA and single-select arrive as a top-level+ // InteractiveMessage. Without this branch they reached webhooks only as the+ // text summary, and a client could not offer the buttons back.+ if im := msg.GetInteractiveMessage(); im != nil {+ if out := interactiveAsBusinessMessage(im); len(out.Buttons) > 0 || len(out.Cards) > 0 {+ return out+ }+ }return nil}out := &BusinessMessage{@@ -178,8 +186,8 @@ func BuildButtonsPayload(msg *waE2E.Message) *BusinessMessage {return out}-// interactiveAsBusinessMessage maps an InteractiveMessage wrapped in a-// template onto the same shape. Carousel cards are mapped recursively.+// interactiveAsBusinessMessage maps an InteractiveMessage — top-level or wrapped+// in a template — onto the same shape. Carousel cards are mapped recursively.func interactiveAsBusinessMessage(im *waE2E.InteractiveMessage) *BusinessMessage {header := im.GetHeader()out := &BusinessMessage{diff --git a/src/pkg/utils/business_message_test.go b/src/pkg/utils/business_message_test.goindex 9bac1349..bbab7364 100644--- a/src/pkg/utils/business_message_test.go+++ b/src/pkg/utils/business_message_test.go@@ -449,3 +449,31 @@ func TestBusinessMessageLinesFallsBackToButtonType(t *testing.T) {t.Fatalf("unexpected lines %q", got)}}++// Cloud API reply buttons arrive as a top-level InteractiveMessage; the webhook+// must carry them as buttons, not only as the text summary.+func TestBuildButtonsPayloadTopLevelInteractive(t *testing.T) {+ reply := &waE2E.Message{InteractiveMessage: &waE2E.InteractiveMessage{+ Body: &waE2E.InteractiveMessage_Body{Text: proto.String("Confirma o horário?")},+ InteractiveMessage: &waE2E.InteractiveMessage_NativeFlowMessage_{NativeFlowMessage: &waE2E.InteractiveMessage_NativeFlowMessage{+ Buttons: []*waE2E.InteractiveMessage_NativeFlowMessage_NativeFlowButton{+ {Name: proto.String("quick_reply"), ButtonParamsJSON: proto.String(`{"display_text":"Sim","id":"sim"}`)},+ {Name: proto.String("quick_reply"), ButtonParamsJSON: proto.String(`{"display_text":"Não","id":"nao"}`)},+ },+ }},+ }}+ buttons := BuildButtonsPayload(reply)+ if buttons == nil || buttons.Body != "Confirma o horário?" {+ t.Fatalf("top-level interactive not exposed: %+v", buttons)+ }+ want := []BusinessButton{{Type: "quick_reply", Text: "Sim", ID: "sim"}, {Type: "quick_reply", Text: "Não", ID: "nao"}}+ if !reflect.DeepEqual(buttons.Buttons, want) {+ t.Fatalf("buttons = %+v, want %+v", buttons.Buttons, want)+ }++ if got := BuildButtonsPayload(&waE2E.Message{InteractiveMessage: &waE2E.InteractiveMessage{+ Body: &waE2E.InteractiveMessage_Body{Text: proto.String("só texto")},+ }}); got != nil {+ t.Fatalf("interactive without buttons should not produce a payload: %+v", got)+ }+}