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

Format string as json from contacts #18

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
24 changes: 19 additions & 5 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ func (h *handler) processCloudWhatsAppPayload(ctx context.Context, channel couri

text := ""
mediaURL := ""
contacts := ""

if msg.Type == "text" {
text = msg.Text.Body
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down