Skip to content

Commit

Permalink
Merge pull request #754 from nyaruka/twa-messaging-service-sid
Browse files Browse the repository at this point in the history
Make sure TWA channels send the set messaging service SID when sendin…
  • Loading branch information
rowanseymour authored Jun 7, 2024
2 parents 735b12b + 3e89482 commit c5568b6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
15 changes: 13 additions & 2 deletions handlers/twiml/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,16 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
"To": []string{fmt.Sprintf("%s:+%s", urns.WhatsApp.Prefix, msg.URN().Path())},
"StatusCallback": []string{callbackURL},
"ContentSid": []string{msg.Templating().ExternalID},
"From": []string{fmt.Sprintf("%s:%s", urns.WhatsApp.Prefix, channel.Address())},
}

// set our from, either as a messaging service or from our address
serviceSID := channel.StringConfigForKey(configMessagingServiceSID, "")
if serviceSID != "" {
form["MessagingServiceSid"] = []string{serviceSID}
}

if channel.Address() != "" {
form["From"] = []string{fmt.Sprintf("%s:%s", urns.WhatsApp.Prefix, channel.Address())}
}

contentVariables := make(map[string]string, len(msg.Templating().Variables))
Expand Down Expand Up @@ -339,7 +348,9 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
serviceSID := channel.StringConfigForKey(configMessagingServiceSID, "")
if serviceSID != "" {
form["MessagingServiceSid"] = []string{serviceSID}
} else {
}

if channel.Address() != "" {
form["From"] = []string{channel.Address()}
}

Expand Down
23 changes: 12 additions & 11 deletions handlers/twiml/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ var twaSendTestCases = []OutgoingTestCase{
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"Body": {"Simple Message ☺"}, "To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}},
Form: url.Values{"Body": {"Simple Message ☺"}, "To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "MessagingServiceSid": {"messageServiceSID"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}},
Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="},
}},
ExpectedExtIDs: []string{"1002"},
Expand Down Expand Up @@ -1139,7 +1139,7 @@ var twaSendTestCases = []OutgoingTestCase{
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "MessagingServiceSid": {"messageServiceSID"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="},
}},
ExpectedExtIDs: []string{"1002"},
Expand Down Expand Up @@ -1167,7 +1167,7 @@ var twaSendTestCases = []OutgoingTestCase{
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef: common resto\",\"2\":\"tomorrow\"}"}},
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "MessagingServiceSid": {"messageServiceSID"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef: common resto\",\"2\":\"tomorrow\"}"}},
Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="},
}},
ExpectedExtIDs: []string{"1002"},
Expand Down Expand Up @@ -1195,7 +1195,7 @@ var twaSendTestCases = []OutgoingTestCase{
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"http://example.com/cat2.jpg\",\"2\":\"tomorrow\"}"}},
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "MessagingServiceSid": {"messageServiceSID"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"http://example.com/cat2.jpg\",\"2\":\"tomorrow\"}"}},
Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="},
}},
ExpectedExtIDs: []string{"1002"},
Expand Down Expand Up @@ -1241,7 +1241,7 @@ var twaSendTestCases = []OutgoingTestCase{
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "MessagingServiceSid": {"messageServiceSID"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="},
}},
ExpectedError: courier.ErrFailedWithReason("1001", "Service specific error: 1001."),
Expand Down Expand Up @@ -1269,7 +1269,7 @@ var twaSendTestCases = []OutgoingTestCase{
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "MessagingServiceSid": {"messageServiceSID"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="},
}},
ExpectedError: courier.ErrContactStopped,
Expand Down Expand Up @@ -1297,7 +1297,7 @@ var twaSendTestCases = []OutgoingTestCase{
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "MessagingServiceSid": {"messageServiceSID"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="},
}},
ExpectedLogErrors: []*courier.ChannelError{courier.ErrorResponseValueMissing("sid")},
Expand Down Expand Up @@ -1325,7 +1325,7 @@ var twaSendTestCases = []OutgoingTestCase{
},
},
ExpectedRequests: []ExpectedRequest{{
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Form: url.Values{"To": {"whatsapp:+250788383383"}, "From": {"whatsapp:+12065551212"}, "MessagingServiceSid": {"messageServiceSID"}, "StatusCallback": {"https://localhost/c/twa/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status?id=10&action=callback"}, "ContentSid": {"ext_id_revive_issue"}, "ContentVariables": {"{\"1\":\"Chef\",\"2\":\"tomorrow\"}"}},
Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="},
}},
ExpectedError: courier.ErrResponseStatus,
Expand All @@ -1340,7 +1340,7 @@ func TestOutgoing(t *testing.T) {
configAccountSID: "accountSID",
courier.ConfigAuthToken: "authToken"})

var tmsDefaultChannel = test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56cd", "TMS", "2021", "US",
var tmsDefaultChannel = test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56cd", "TMS", "", "US",
[]string{urns.Phone.Prefix},
map[string]any{
configMessagingServiceSID: "messageServiceSID",
Expand Down Expand Up @@ -1382,8 +1382,9 @@ func TestOutgoing(t *testing.T) {
twaChannel := test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "TWA", "+12065551212", "US",
[]string{urns.WhatsApp.Prefix},
map[string]any{
configAccountSID: "accountSID",
courier.ConfigAuthToken: "authToken",
configAccountSID: "accountSID",
courier.ConfigAuthToken: "authToken",
configMessagingServiceSID: "messageServiceSID",
},
)

Expand Down

0 comments on commit c5568b6

Please sign in to comment.