From 17515992b6b2449233546994d8c9eeb87cc45763 Mon Sep 17 00:00:00 2001 From: CrazyBolillo Date: Sun, 28 Sep 2025 21:32:56 -0600 Subject: [PATCH] feat: add context to message entries --- webhook/testdata/button_reply.json | 47 +++++++++++++++++++++++ webhook/webhook.go | 16 +++++++- webhook/webhook_test.go | 61 ++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 webhook/testdata/button_reply.json create mode 100644 webhook/webhook_test.go diff --git a/webhook/testdata/button_reply.json b/webhook/testdata/button_reply.json new file mode 100644 index 0000000..270c3b2 --- /dev/null +++ b/webhook/testdata/button_reply.json @@ -0,0 +1,47 @@ +{ + "object": "whatsapp_business_account", + "entry": [ + { + "id": "102290129340398", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "15550783881", + "phone_number_id": "106540352242922" + }, + "contacts": [ + { + "profile": { + "name": "Sheena Nelson" + }, + "wa_id": "16505551234" + } + ], + "messages": [ + { + "context": { + "from": "15550783881", + "id": "wamid.HBgLMTQxMjU1NTA4MjkVAgASGBQzQUNCNjk5RDUwNUZGMUZEM0VBRAA=" + }, + "from": "16505551234", + "id": "wamid.HBgLMTY1MDM4Nzk0MzkVAgASGBQzQUFERjg0NDEzNDdFODU3MUMxMAA=", + "timestamp": "1749854575", + "type": "interactive", + "interactive": { + "type": "button_reply", + "button_reply": { + "id": "bug", + "title": "bug in code" + } + } + } + ] + }, + "field": "messages" + } + ] + } + ] +} diff --git a/webhook/webhook.go b/webhook/webhook.go index 3cff251..98c64d6 100644 --- a/webhook/webhook.go +++ b/webhook/webhook.go @@ -41,6 +41,7 @@ type Message struct { ID string `json:"id"` Timestamp string `json:"timestamp"` Type string `json:"type"` + Context *Context `json:"context"` Text Text `json:"text"` Interactive Interactive `json:"interactive"` } @@ -50,11 +51,22 @@ type Text struct { } type Interactive struct { - Type string `json:"type"` - NfmReply NfmReply `json:"nfm_reply"` + Type string `json:"type"` + NfmReply *NfmReply `json:"nfm_reply,omitempty"` + ButtonReply *ButtonReply `json:"button_reply,omitempty"` } type NfmReply struct { Name string `json:"name"` ResponseJson string `json:"response_json"` } + +type ButtonReply struct { + ID string `json:"id"` + Title string `json:"title"` +} + +type Context struct { + From string `json:"from"` + ID string `json:"id"` +} diff --git a/webhook/webhook_test.go b/webhook/webhook_test.go new file mode 100644 index 0000000..c015720 --- /dev/null +++ b/webhook/webhook_test.go @@ -0,0 +1,61 @@ +package webhook + +import ( + _ "embed" + "encoding/json" + "testing" +) + +//go:embed testdata/button_reply.json +var buttonReply []byte + +func TestParseButtonReply(t *testing.T) { + var notification Notification + err := json.Unmarshal(buttonReply, ¬ification) + if err != nil { + t.Fatalf("unable to unmarshall notification: %v", err) + } + + message := notification.Entry[0].Changes[0].Value.Messages[0] + if message.Type != "interactive" { + t.Errorf("message type - want %s, got %s", "interactive", message.Type) + } + + if message.Interactive.ButtonReply == nil { + t.Fatalf("expected ButtonReply to be non nil") + } + buttonReply := message.Interactive.ButtonReply + + expectedID := "bug" + expectedTitle := "bug in code" + if buttonReply.ID != expectedID { + t.Errorf("button id - got: %s, want: %s", buttonReply.ID, expectedID) + } + + if buttonReply.Title != expectedTitle { + t.Errorf("button title- got: %s, want: %s", buttonReply.Title, expectedTitle) + } +} + +func TestParseContext(t *testing.T) { + var notification Notification + err := json.Unmarshal(buttonReply, ¬ification) + if err != nil { + t.Fatalf("unable to unmarshall notification: %v", err) + } + + message := notification.Entry[0].Changes[0].Value.Messages[0] + if message.Context == nil { + t.Fatalf("expected message context to be non nil") + } + + wantFrom := "15550783881" + if message.Context.From != wantFrom { + t.Errorf("context.From - got: %s, want: %s", message.Context.From, wantFrom) + } + + wantId := "wamid.HBgLMTQxMjU1NTA4MjkVAgASGBQzQUNCNjk5RDUwNUZGMUZEM0VBRAA=" + if message.Context.ID != wantId { + t.Errorf("context.ID - got: %s, want: %s", message.Context.ID, wantId) + } +}