SMS messaging has been a widely adopted way of communication over the last decades, not only for people but for organisations as well. Even though nowadays there are many messaging apps that are more popular and flexible than plain SMS, there are still scenarios in which businesses find SMS messaging a valuable way to communicate with their customers, employees or business partners. For instance, there are food chains that allow you to place your favourite order via SMS, some business provide order and shipment tracking via SMS, and there are many confirmation systems that utilise SMS so users can confirm their attendance to a meeting or appointment via a text message.
Twilio is a flexible and extensible PaaS offering that provides, among other communication services, that of SMS. Twilio allows you to send and receive SMS messages and integrate them into your business processes via APIs. Meanwhile, Logic Apps offers the Twilio connector which provides actions to send, list and get messages from Twilio. However, at the time of writing, this connector does not include a trigger, so that a Logic instance can be initiated when an SMS is received on a phone number of a Twilio account.
In this post, I will show how to trigger an Azure Logic App when an SMS message is received on a Twilio phone number.
If you want to implement this scenario, you would need the following:
- A Twilio Account
- A Twilio Phone Number (you have to buy a phone number that can send and receive SMS messages, which has a monthly cost)
- Balance on your Twilio Account to allow for the Programmable SMS costs (receiving and sending SMS messages)
Implementing an SMS triggered Azure Logic App
The steps to implement an SMS triggered Logic App are described below:
1. Create a Logic App with an Http Request Trigger
As mentioned before, the Twilio connector does not offer a trigger to initiate a Logic App instance. However, Twilio provides a Webhook that allows you to subscribe your Http endpoint so that Twilio makes an Http Post whenever an SMS message is received. Therefore, we can create an Http triggered Logic App to receive these calls from the webhook. The quickest way to do is to start with the corresponding HTTP Triggered template.
Once the Logic Apps has been created, we should make sure that POST is selected as the method to trigger the workflow, so Twilio can call the Logic App endpoint and send the corresponding payload.
2. Getting the Http Url
After we save the Logic App containing the Http Request trigger, we get the workflow endpoint Url, including the corresponding SAS token. For your own solution, you might want to implement an intermediate layer and abstract the actual Logic App Url using Azure API Management or an Azure Functions Proxy. For this exercise, I will continue using the generated Url.
3. Configuring the Twilio Webhook to Call the Http endpoint when a message comes in
Once we have the Logic App endpoint Url, on the Twilio Portal we configure the Twilio Phone Number to use a Webhook when a message comes in to call the Logic App endpoint with an Http Post. The Webhook will send the SMS details as form data to the Logic App.
Now, we are all set to trigger our Logic App when an SMS is received.
4. Getting the SMS Message Details
You can inspect the message sent by the Twilio Webhook by sending an SMS to trigger your Logic App, and checking the output body of your run as shown below.
The Twilio Webhook sends Url encoded form data (“$content-type”: “application/x-www-form-urlencoded”). You should expect a request output body similar to the one shown below.
Being this a Form Data Post request, we can use the function @triggerFormDataValue() to get each of the properties sent by Twilio, e.g. @triggerFormDataValue(‘Body’) and @triggerFormDataValue(‘From’)
5. Sending an SMS Response
To make the Logic App a bit more fun, and show more of the Twilio connector capabilities, I’ll add one action to the workflow to reply back to the sender via SMS.
When we add the Twilio - Send Text Message, we first have to configure the API Connection. We have to give the connection a name and provide the Account Id, and Access Token. We can get these details from the Twilio Dashboard. We get the Account Id as the Account SID and the Access Token as the Auth Token.
Once we have configured the API Connection, we are ready to continue configuring the actual action. We should be able to select from the drop down list the From Phone Number in our Twilio account that we want to use to send the response message. In our scenario, we get the To Phone Number using @triggerFormDataValue(‘From’). For this exercise, I’m adding the original Text as part of the Reply Text as shown below.
And, that’s pretty much it. We have implemented a Logic App that is triggered by an SMS which replies back to the sender. In case you are interested on seeing the code behind, you can have a look at it below:
In this post, I’ve shown how to trigger a Logic App workflow by an SMS message using a Twilio account, how to get the message details, and how to reply back to the sender using the Twilio connector. Thanks to the Logic Apps capabilities, this implementation is quite easy and straight forward. Of course, you can extend and enrich your workflow making use of the many different connectors to get or push data from and to diverse apps and LOB systems. I hope you have found this post useful and it comes in handy when you are implementing solutions that require SMS integration. Feel free to add your comments or queries below.
Follow me on @pacodelacruz
Cross-posted on Deloitte Platform Engineering Blog