Skip to content

Commit

Permalink
Merge branch 'feat/save-wac-message-context' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobernardoaf committed Jan 24, 2025
2 parents f52e236 + 3bd63a6 commit 0f3980c
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
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"
}
]
}
]
}

0 comments on commit 0f3980c

Please sign in to comment.