Skip to content

Commit

Permalink
Add support for receiving contact type messages in WAC
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Apr 26, 2023
1 parent f0bb2c4 commit 11c85dd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
24 changes: 24 additions & 0 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ type moPayload struct {
Title string `json:"title"`
} `json:"list_reply,omitempty"`
} `json:"interactive,omitempty"`
Contacts []struct {
Name struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
FormattedName string `json:"formatted_name"`
} `json:"name"`
Phones []struct {
Phone string `json:"phone"`
WaID string `json:"wa_id"`
Type string `json:"type"`
} `json:"phones"`
} `json:"contacts"`
} `json:"messages"`
Statuses []struct {
ID string `json:"id"`
Expand Down Expand Up @@ -483,6 +495,18 @@ func (h *handler) processCloudWhatsAppPayload(ctx context.Context, channel couri
text = msg.Interactive.ButtonReply.Title
} else if msg.Type == "interactive" && msg.Interactive.Type == "list_reply" {
text = msg.Interactive.ListReply.Title
} else if msg.Type == "contacts" {

if len(msg.Contacts) == 0 {
return nil, nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, errors.New("no shared contact"))
}

// put phones in a comma-separated string
var phones []string
for _, phone := range msg.Contacts[0].Phones {
phones = append(phones, phone.Phone)
}
text = strings.Join(phones, ", ")
} else {
// we received a message type we do not support.
courier.LogRequestError(r, channel, fmt.Errorf("unsupported message type %s", msg.Type))
Expand Down
2 changes: 2 additions & 0 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ var testCasesWAC = []ChannelHandleTestCase{
{Label: "Receive Valid Interactive List Reply Message", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/listReplyWAC.json")), Status: 200, Response: "Handled", NoQueueErrorCheck: true, NoInvalidChannelCheck: true,
Text: Sp("Yes"), URN: Sp("whatsapp:5678"), ExternalID: Sp("external_id"), Date: Tp(time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC)),
PrepRequest: addValidSignatureWAC},
{Label: "Receive Valid Contact Message", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/contactWAC.json")), Status: 200, Response: "Handled", NoQueueErrorCheck: true, NoInvalidChannelCheck: true,
Text: Sp("+1 415-858-6273, +1 415-858-6274"), URN: Sp("whatsapp:5678"), ExternalID: Sp("external_id"), Date: Tp(time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC)), PrepRequest: addValidSignatureWAC},
{Label: "Receive Invalid JSON", URL: wacReceiveURL, Data: "not json", Status: 400, Response: "unable to parse", PrepRequest: addValidSignatureWAC},
{Label: "Receive Invalid JSON", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/invalidFrom.json")), Status: 400, Response: "invalid whatsapp id", PrepRequest: addValidSignatureWAC},
{Label: "Receive Invalid JSON", URL: wacReceiveURL, Data: string(courier.ReadFile("./testdata/wac/invalidTimestamp.json")), Status: 400, Response: "invalid timestamp", PrepRequest: addValidSignatureWAC},
Expand Down
51 changes: 51 additions & 0 deletions handlers/facebookapp/testdata/wac/contactWAC.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"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": "contacts",
"contacts": [{
"name": {
"first_name": "Test",
"last_name": "Contact",
"formatted_name": "Test Contact"
},
"phones": [{
"phone": "+1 415-858-6273",
"wa_id": "14158586273",
"type": "CELL"
},
{
"phone": "+1 415-858-6274",
"wa_id": "14158586272",
"type": "CELL"
}]
}]
}]
},
"field": "messages"
}
]
}
]
}

0 comments on commit 11c85dd

Please sign in to comment.