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:
using Twilio;
Following is the controller function that I am using to send SMS:
[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:
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:
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:
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!