In the constantly changing and ever-growing market of mobile technology, businesses face the challenge of finding methods to effectively communicate and connect with their target customers who are always on the move.
In this post, I’ll outline how you can integrate Twilio’s SMS service into a .NET web application in a few simple steps. We’ve recently integrated Twilio into a client’s web application as a means to communicate with employees after-hours, when employees have left for the day but a situation arises that needs further attention.
Before we get into the coding, let’s take a quick look at other methods that are used to send SMS messages from applications.
Email-to-SMS Gateway functionality has existed for a while and offers some of the same benefits, but with a few caveats.
- What if you don’t know the customer’s carrier? You need this in order to utilize it (for example, if your customer’s number is 555-777-8888 and is a Verizon customer, you can text them by sending an email to firstname.lastname@example.org)
- You cannot control who the message is sent from, for some carriers is the email address of the send, for some it’s the email address of the server
- You cannot control what number delivers the message, so the customer may not be able to respond back to the message
Twilio’s SMS service overcomes these hurdles in simplistic fashion. Twilio’s SMS service allows web applications to communicate with mobile customers with a few simple lines of code.
Let’s get started. First, you’ll need to add the Twilio service into your project by following these steps (note that this is for .NET 4.0 application). The installation process can be found here: https://www.twilio.com/docs/csharp/install
Once you’ve installed the service, you’re ready to add your SMS code.
In this case, our function is setup to receive a Phone Number that we are going to send a message and the Body of that message.
Public Sub SendSMS(ByVal receivePhoneNumber As String, ByVal messageBody As String)
Dim accountSID As String = ""
Dim authToken As String = ""
Dim objTwilio As Twilio.TwilioRestClient = Nothing
Dim objMessage As Twilio.Message = Nothing
Dim sendPhoneNumber As String = ""
When you sign up for your Twilio account, you’ll receive the following credentials within their application.
authToken = "111111"
accountSID = "222222"
sendPhoneNumber = "5553211212"
Instantiate your Twilio object
objTwilio = New TwilioRestClient(accountSID, authToken)
objMessage = twTwilio.SendMessage(sendPhoneNumber, receivePhoneNumber, messageBody, "")
So now we’ve sent our message. It’s sent to the end user via a telephone number, just as if you were sending a text message from your phone. What if the end user wants to respond back? They can, with no additional effort on their end. The message will be sent back to the Twilio number, and Twilio will redirect the response to your web application.
Within the Twilio client, you can setup a Messaging URL, which will define where Twilio POSTs the response to (for example , http://www.yoursite.com/receiveTwilioResponse.aspx). Let’s setup that page.
When we setup receiveTwilioResponse.aspx, we’ll need to capture the Page Request and parse out the response message.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim fromPhoneNumber As String = ""
Dim messageBody As String = ""
First, we’ll get the Phone Number of the end user who is responding back to us
If Not Request.Form("From") Is Nothing Then
fromPhoneNumber = Request.Form(“From”)
Next, we’ll get the Message Body from the page request
If Not Request.Form("Body") Is Nothing Then
messageBody = Request.Form("Body")
And that’s it! From here, we can handle this data how we see fit.
This is simplified for example purposes. You will want to setup error handling and logging in conjunction with sending the message, and will need to clearly define the business logic of when and who to send messages to.
Learn more about the service here : https://www.twilio.com/sms