Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions webhook/testdata/button_reply.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]
}
16 changes: 14 additions & 2 deletions webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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"`
}
61 changes: 61 additions & 0 deletions webhook/webhook_test.go
Original file line number Diff line number Diff line change
@@ -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, &notification)
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, &notification)
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)
}
}
Loading