How can I write a REST API to receive SMS via Twilio Number using ASp.net Web Api

I am new to the Web Api / Rest API, but I plan on using Visual Studio 2012 MVC4 Web Api. How can I create an application to receive text / SMS messages through Twilio and send a response? I searched for several articles here and on the Twilio website, but none of them let me know how to use MVC4 Web Api. Can someone please help me and show / give examples?

+4
source share
2 answers

Twilio the evangelist is here.

All of our Quickstarts are translated into C #, which uses Razor syntax. You just need to select C # from the language located at the top of the page. Here is a direct link to the start of C # speed starts:

http://www.twilio.com/docs/quickstart/csharp/sms

In addition, you might want to check out a series of blog posts I wrote some time ago that I introduce Twilio for .NET developers, including one that uses the Twilio.NET helper library in an ASP.NET MVC application:

In particular, to get information about the response to a text message, you probably will be most interested in parts 1,2,4 and 7.

Finally, if you hit the Twilio.NET helper wiki on GitHub, there are code examples:

https://github.com/twilio/twilio-csharp/wiki

I will also point you to a blog post that the developer of Long Le wrote for our blog that shows how to respond to Twilio requests using the ASp.NET Web API, as this requires a few details:

http://www.twilio.com/blog/2012/11/building-twilio-apps-using-asp-net-mvc-4-web-api.html

Personally, it’s easier for me to just use the standard ASP.NET MVC Controller, but these are just my personal preferences.

Hope this helps.

+2
source

I really don't like any documentation I found related to the web API, so I wanted to answer this question, although it is a bit outdated.

Twilio issues an HTTP request (you can select a GET or POST verb on the Twilio website) to the endpoint that you create, and your HTTP response, if the body is formed correctly, will give an SMS response to the user. Here is a screenshot of the Twilio configuration below.

Usually I only return json from my endpoints, but for Twilio you have to return XML. Configure your routing to have routing with the extension (this allows you to configure the URL that will return the XML for the Twilio request). This is necessary because Twilio does not publish the text / xml in the Accept header. They actually have an Accept header, but it contains * / *, but will work with anything but XML:

config.Routes.MapHttpRoute( name: "DefaultApiWithExtension", routeTemplate: "xml/{controller}/{id}", defaults: new { id = RouteParameter.Optional, ext="xml" } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new { id = RouteParameter.Optional} ); config.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "application/xml"); config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json"); 

Now install the TwiML nuget package using the package manager console. This will only be used for the TwilioResponse class.

 Install-Package Twilio.TwiML 

Add this class if you want, not required, but if you just want to put the parameters in the controller action method.

 public class TwilioRequest { public string MessageSid { get; set; } public string AccountSid { get; set; } public string From { get; set; } public string To { get; set; } public string Body { get; set; } } 

And then the action of your controller looks something like this:

  public virtual HttpResponseMessage Get([FromUri]TwilioRequest request) { TwilioResponse tr = new TwilioResponse(); if (request.Body == "Hello World") tr.Message("Hello back!"); else tr.Message("Text 'Hello World' for a friendly message."); return Request.CreateResponse(HttpStatusCode.OK, tr.Element); } 

or this if you do not want to use the TwilioRequest class that I did:

  public virtual HttpResponseMessage Get(string body) { TwilioResponse tr = new TwilioResponse(); if (body == "Hello World") tr.Message("Hello back!"); else tr.Message("Text 'Hello World' for a friendly message."); return Request.CreateResponse(HttpStatusCode.OK, tr.Element); } 

Finally, to test it, you will need to configure Twilio to execute the GET request to the endpoint. This is what looked like their site at the time of publication:

enter image description here

Note that the configuration URL has the pattern / xml / controllername. You can check this in your browser, postman or violinist before striking Twilio. The reaction body should look something like this if it works correctly:

 <response> <message>Hello Back!</message> </response> 

Concluding remarks:

Technically, you don’t even need a TwiML library, because it provides only a wrapper class that is serialized in XML, and you can write your own in a couple of minutes. It was also useful for me to open a port on the firewall that sent HTTP requests from Twilio directly to my machine so that I could debug Twilio.

+12
source

Source: https://habr.com/ru/post/1492411/


All Articles