From 3e8948261f9a08e2ff3c9d3d5285ebbbaffe2708 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 7 Jun 2024 14:42:00 +0200 Subject: [PATCH] Make sure TWA channels send the set messaging service SID when sending messages --- handlers/twiml/handlers.go | 15 +++++++++++++-- handlers/twiml/handlers_test.go | 23 ++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/handlers/twiml/handlers.go b/handlers/twiml/handlers.go index 45cd43324..c6dbf8e6e 100644 --- a/handlers/twiml/handlers.go +++ b/handlers/twiml/handlers.go @@ -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)) @@ -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()} } diff --git a/handlers/twiml/handlers_test.go b/handlers/twiml/handlers_test.go index 290fced93..3992d9bec 100644 --- a/handlers/twiml/handlers_test.go +++ b/handlers/twiml/handlers_test.go @@ -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"}, @@ -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"}, @@ -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"}, @@ -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"}, @@ -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."), @@ -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, @@ -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")}, @@ -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, @@ -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", @@ -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", }, )