From cef18fc601dfaa108d631bbc02b04598439a8736 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Tue, 18 Mar 2025 13:59:31 +0200 Subject: [PATCH] Add support for config to shorten URL for TMS --- handlers/twiml/handlers.go | 6 ++++++ handlers/twiml/handlers_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/handlers/twiml/handlers.go b/handlers/twiml/handlers.go index 09885295b..f39a27fc2 100644 --- a/handlers/twiml/handlers.go +++ b/handlers/twiml/handlers.go @@ -36,6 +36,7 @@ const ( configSendURL = "send_url" configBaseURL = "base_url" configIgnoreDLRs = "ignore_dlrs" + configLinkShortening = "link_shortening" signatureHeader = "X-Twilio-Signature" forwardedPathHeader = "X-Forwarded-Path" @@ -348,6 +349,11 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen // set our from, either as a messaging service or from our address serviceSID := channel.StringConfigForKey(configMessagingServiceSID, "") if serviceSID != "" { + linkShortening := channel.BoolConfigForKey(configLinkShortening, false) + if linkShortening { + form["ShortenUrls"] = []string{"true"} + } + form["MessagingServiceSid"] = []string{serviceSID} } diff --git a/handlers/twiml/handlers_test.go b/handlers/twiml/handlers_test.go index 397dfe6e2..37103979e 100644 --- a/handlers/twiml/handlers_test.go +++ b/handlers/twiml/handlers_test.go @@ -822,6 +822,27 @@ var tmsDefaultSendTestCases = []OutgoingTestCase{ }, } +var tmsShortenLinks = []OutgoingTestCase{ + { + Label: "Plain Send", + MsgText: "Simple Message ☺", + MsgURN: "tel:+250788383383", + MockResponses: map[string][]*httpx.MockResponse{ + "https://api.twilio.com/2010-04-01/Accounts/accountSID/Messages.json": { + httpx.NewMockResponse(200, nil, []byte(`{ "sid": "1002" }`)), + }, + }, + ExpectedRequests: []ExpectedRequest{ + { + Headers: map[string]string{"Authorization": "Basic YWNjb3VudFNJRDphdXRoVG9rZW4="}, + Path: "/2010-04-01/Accounts/accountSID/Messages.json", + Form: url.Values{"Body": {"Simple Message ☺"}, "To": {"+250788383383"}, "MessagingServiceSid": {"messageServiceSID"}, "ShortenUrls": {"true"}, "StatusCallback": {"https://localhost/c/tms/8eb23e93-5ecb-45ba-b726-3b064e0c56cd/status?id=10&action=callback"}}, + }, + }, + ExpectedExtIDs: []string{"1002"}, + }, +} + var twDefaultSendTestCases = []OutgoingTestCase{ { Label: "Plain Send", @@ -1348,6 +1369,14 @@ func TestOutgoing(t *testing.T) { configAccountSID: "accountSID", courier.ConfigAuthToken: "authToken"}) + var tmsShortenLinksChannel = test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56cd", "TMS", "", "US", + []string{urns.Phone.Prefix}, + map[string]any{ + configLinkShortening: true, + configMessagingServiceSID: "messageServiceSID", + configAccountSID: "accountSID", + courier.ConfigAuthToken: "authToken"}) + var twDefaultChannel = test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "TW", "2020", "US", []string{urns.Phone.Prefix}, map[string]any{ @@ -1366,6 +1395,7 @@ func TestOutgoing(t *testing.T) { RunOutgoingTestCases(t, defaultChannel, newTWIMLHandler("T", "Twilio", true), defaultSendTestCases, []string{httpx.BasicAuth("accountSID", "authToken")}, nil) RunOutgoingTestCases(t, tmsDefaultChannel, newTWIMLHandler("TMS", "Twilio Messaging Service", true), tmsDefaultSendTestCases, []string{httpx.BasicAuth("accountSID", "authToken")}, nil) + RunOutgoingTestCases(t, tmsShortenLinksChannel, newTWIMLHandler("TMS", "Twilio Messaging Service", true), tmsShortenLinks, []string{httpx.BasicAuth("accountSID", "authToken")}, nil) RunOutgoingTestCases(t, twDefaultChannel, newTWIMLHandler("TW", "TwiML", true), twDefaultSendTestCases, []string{httpx.BasicAuth("accountSID", "authToken")}, nil) RunOutgoingTestCases(t, swChannel, newTWIMLHandler("SW", "SignalWire", false), swSendTestCases, []string{httpx.BasicAuth("accountSID", "authToken")}, nil)