Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET

Send & Receive SMS using TWILIO SMS API

Rate me:
Please Sign up or sign in to vote.
2.17/5 (5 votes)
23 Jun 2016CPOL2 min read 21.8K   7   2
Send & Receive SMS using TWILIO SMS API

I spent a lot of time searching for the right topic for my first post(technically). But as promised, I am here with my new topic.

I found a lot of Twilio API trending these days. And I can say that I use Twilio API a lot whether it is SMS/MMS API or VOICE API. Twilio is coming up with lots of variations in API.

I will try to put it in a simple way. So let's start with TWILIO SMS API.

Prerequisites

I am using .NET Framework 4.5 so I guess you will have it in your computer. And of course, MVC as it is the easiest approach to write the code.

Now I am assuming that you have created a new project in your Visual Studio. So I will come to the point straight away.

Now install twilio from nuget package manager. In Visual Studio menu, go to PROJECT -> Manage Nuget Packages. Nuget package manager window will open. Search for Twilio rest helper library in that window and then click on install.

Use the following namespace to access twilio rest helper library:

C#
using Twilio;

Following is the controller function that I am using to send SMS:

C#
[HttpPost]
public JsonResult SendSMS(string message, string custid)
{
    string AccountSid = "Your Twilio Account SID";
    string AuthToken = "Your Twilio Authentication Token";
    var twilio = new TwilioRestClient(AccountSid, AuthToken);
    var sms = twilio.SendMessage("From Number", "To Number", "Message Body");
    var status = sms.Status;

    return status;
}

This is just a simple controller but when you get the status, it always show "queued" so it is not necessary that it will be sent for sure. It can fail later as well. When I started using twilio, I got stuck here. So all you have to do is save MessageSID along with the status in database. You can get the SID with the following code:

C#
var MessageSID=sms.Sid;

Now after this, you will have to run some kind of backend service or cron job if you want to get the status of the message automatically or you can get it on any event also. Below is the code to do this:

C#
var Status= twilio.GetSMSMessage(MessageSID);

Pass the MessageSID of the SMS message and you will get the status of this message.

Receive Incoming SMS Message

In order to receive SMS from twilio, you will have to create an action method in controller. Let's say you have Home controller. Create the following action in controller:

C#
public ActionResult IncomingMessage(string From, string To, string Body)

{

} 

You can do whatever you want to do with these parameters. You can save them in database, you can send auto response to the user, etc.

Now pass this URL in your dedicated number Message CallBack box (select CONFIGURE WITH "Webhooks/TwiML"):

http://yourdomain.com/Home/IncomingMessage

Along with From(user's number), To(Twilio number), Body(SMS text), twilio passes other parameters like MessageSid, AccountSid, NumMedia, etc. also. You can save these as per your need. NumMedia is for incoming MMS messages, I will cover MMS messaging in my other post.

Hope you like it. If you have any questions, comments or suggestions, then please leave a comment below.

Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I love coding and using new API's

Comments and Discussions

 
QuestionHow to know receive message linked to previous message Pin
Member 1386584627-Aug-18 19:54
Member 1386584627-Aug-18 19:54 
QuestionC# WebApi project to Send & Receive SMS using TWILIO SMS API Pin
juniorMVCc#21-Mar-18 0:42
juniorMVCc#21-Mar-18 0:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.