diff --git a/handlers/facebookapp/facebookapp.go b/handlers/facebookapp/facebookapp.go index 054ddf4de..bec96a440 100644 --- a/handlers/facebookapp/facebookapp.go +++ b/handlers/facebookapp/facebookapp.go @@ -475,6 +475,7 @@ func (h *handler) processCloudWhatsAppPayload(ctx context.Context, channel couri text := "" mediaURL := "" + contacts := "" if msg.Type == "text" { text = msg.Text.Body @@ -507,12 +508,21 @@ func (h *handler) processCloudWhatsAppPayload(ctx context.Context, channel couri 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) + contacts = `{"contacts":[` + for i, phone := range msg.Contacts { + contacts += fmt.Sprintf(`{"formatted_name":"%s","numbers":[`, phone.Name.FormattedName) + for j, p := range phone.Phones { + contacts += fmt.Sprintf(`"%s"`, p.Phone) + if j != len(phone.Phones)-1 { + contacts += `,` + } + } + contacts += `]}` + if i != len(msg.Contacts)-1 { + contacts += `,` + } } - text = strings.Join(phones, ", ") + contacts += `]}` } else { // we received a message type we do not support. courier.LogRequestError(r, channel, fmt.Errorf("unsupported message type %s", msg.Type)) @@ -527,6 +537,10 @@ func (h *handler) processCloudWhatsAppPayload(ctx context.Context, channel couri courier.LogRequestError(r, channel, err) } + if contacts != "" { + event.WithAttachment(contacts) + } + if mediaURL != "" { event.WithAttachment(mediaURL) } diff --git a/handlers/facebookapp/facebookapp_test.go b/handlers/facebookapp/facebookapp_test.go index 94a080f0b..231eaba57 100644 --- a/handlers/facebookapp/facebookapp_test.go +++ b/handlers/facebookapp/facebookapp_test.go @@ -279,7 +279,7 @@ var testCasesWAC = []ChannelHandleTestCase{ 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}, + Text: Sp(""), Attachment: Sp(`{"contacts":[{"formatted_name":"Test Contact","numbers":["+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},