Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: save wac message context in metadata #159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,35 @@ func (h *handler) processCloudWhatsAppPayload(ctx context.Context, channel couri
event.WithAttachment(mediaURL)
}

// Add to the existing metadata, the message context
if msg.Context != nil {
metadata := event.Metadata()
if metadata == nil {
newMetadata := make(map[string]interface{})
newMetadata["context"] = msg.Context

metadata, err = json.Marshal(newMetadata)
if err != nil {
courier.LogRequestError(r, channel, err)
}
} else {
newMetadata := make(map[string]interface{})
err := json.Unmarshal(metadata, &newMetadata)
if err != nil {
courier.LogRequestError(r, channel, err)
}

newMetadata["context"] = msg.Context

metadata, err = json.Marshal(newMetadata)
if err != nil {
courier.LogRequestError(r, channel, err)
}
}

event.WithMetadata(metadata)
}

err = h.Backend().WriteMsg(ctx, event)
if err != nil {
return nil, nil, err
Expand Down
20 changes: 20 additions & 0 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,26 @@ var testCasesWAC = []ChannelHandleTestCase{
{Label: "Receive Empty Changes", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/emptyChangesWAC.json")), Status: 200, Response: `"Events Handled"`, PrepRequest: addValidSignatureWAC},
{Label: "Receive Empty Contacts", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/emptyContactsWAC.json")), Status: 400, Response: `"no shared contact"`, PrepRequest: addValidSignatureWAC},
{Label: "Receive Unsupported Message Type", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/invalidTypeMsgWAC.json")), Status: 200, Response: `"Events Handled"`, PrepRequest: addValidSignatureWAC},
{Label: "Receive Message WAC with Context", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/helloWithContextWAC.json")), Status: 200, Response: "Handled", NoQueueErrorCheck: true, NoInvalidChannelCheck: true,
Text: Sp("Hello World"), URN: Sp("whatsapp:5678"), ExternalID: Sp("external_id"), Date: Tp(time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC)), Metadata: Jp(map[string]interface{}{"context": map[string]interface{}{
"forwarded": false,
"frequently_forwarded": false,
"from": "5678",
"id": "9876",
}}),
PrepRequest: addValidSignatureWAC},
{Label: "Receive NFM Reply With Context WAC", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/flowWithContextWAC.json")), Status: 200, Response: "Handled", NoQueueErrorCheck: true, NoInvalidChannelCheck: true,
URN: Sp("whatsapp:5678"), ExternalID: Sp("external_id"), Date: Tp(time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC)), Metadata: Jp(map[string]interface{}{"context": map[string]interface{}{
"forwarded": false,
"frequently_forwarded": false,
"from": "5678",
"id": "9876",
},
"nfm_reply": map[string]interface{}{
"name": "Flow Wpp",
"response_json": map[string]interface{}{"flow_token": "<FLOW_TOKEN>", "optional_param1": "<value1>", "optional_param2": "<value2>"},
}}),
PrepRequest: addValidSignatureWAC},
}

func TestHandler(t *testing.T) {
Expand Down
50 changes: 50 additions & 0 deletions handlers/facebookapp/testdata/wac/flowWithContextWAC.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "8856996819413533",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "+250 788 123 200",
"phone_number_id": "12345"
},
"contacts": [
{
"profile": {
"name": "Kerry Fisher"
},
"wa_id": "5678"
}
],
"messages": [
{
"from": "5678",
"id": "external_id",
"timestamp": "1454119029",
"type": "interactive",
"interactive": {
"type": "nfm_reply",
"nfm_reply": {
"name": "Flow Wpp",
"response_json": "{\"flow_token\": \"<FLOW_TOKEN>\", \"optional_param1\": \"<value1>\", \"optional_param2\": \"<value2>\"}"
}
},
"context": {
"forwarded": false,
"frequently_forwarded": false,
"from": "5678",
"id": "9876",
"referred_product": {}
}
}
]
},
"field": "messages"
}
]
}
]
}
46 changes: 46 additions & 0 deletions handlers/facebookapp/testdata/wac/helloWithContextWAC.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "8856996819413533",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "+250 788 123 200",
"phone_number_id": "12345"
},
"contacts": [
{
"profile": {
"name": "Kerry Fisher"
},
"wa_id": "5678"
}
],
"messages": [
{
"from": "5678",
"id": "external_id",
"timestamp": "1454119029",
"text": {
"body": "Hello World"
},
"type": "text",
"context": {
"forwarded": false,
"frequently_forwarded": false,
"from": "5678",
"id": "9876",
"referred_product": {}
}
}
]
},
"field": "messages"
}
]
}
]
}
Loading