4
votes

I'm trying to get an Azure Web Function to receive a Twilio SMS message - and failing!

I've created a Web Function to successfully send SMS messages - now I want to listen and react to responses.

I've set up a web function as per the below. Its pretty simple at the moment, and is supposed to parrot back the original message:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            var data = await req.Content.ReadAsStringAsync();
            var formValues = data.Split('&')
                .Select(value => value.Split('='))
                .ToDictionary(pair => Uri.UnescapeDataString(pair[0]).Replace("+", " "),
                              pair => Uri.UnescapeDataString(pair[1]).Replace("+", " "));

            // Perform calculations, API lookups, etc. here

            var response = new MessagingResponse()
                .Message($"You said: {formValues["Body"]}");
            var twiml = response.ToString();
            twiml = twiml.Replace("utf-16", "utf-8");

            return new HttpResponseMessage
            {
                Content = new StringContent(twiml, Encoding.UTF8, "application/xml")
            };
        }

In Twilio, I've configured the phone to use web hooks:

enter image description here

I've deployed the Web Function, however when I try testing by sending a message, I get the following error in the Twilio logs:

11200 There was a failure attempting to retrieve the contents of this URL Msg Unsupported Media Type Message: The WebHook request must contain an entity body formatted as JSON.

Does anyone have any experience in how to fix this error?

3

3 Answers

3
votes

I just got this working with Twilio's SMS services. In the Azure Portal, if you go to the function, then go to Integrate, change the mode to Standard. This forces the Azure function to return a normal HTTP response and you can control the content type. If you use application/xml it will work fine.

1
votes

Okay, the current solution to this is .... it can't be done in Azure Web Functions. An Azure Web Function expects a JSON payload. Twilio Webhooks are an XML value/pair. So, the web function will reject the webhook call.

The best/easiest approach is to use a WebAPI or MVC Controller as per the Twilio example. I tried a sample version and had my Twilio Webhooks working to reply to an SMS in about 15 minutes from start to finish.

0
votes

To debug, I'd use a tool such as Postman or Fiddler to manually replay (or create from scratch) an identical request to what you're expecting from Twilio. You can then see what kind of response you get and not have to solely rely on Twilio's error message.

From the error code, I'd imagine that the problem is either:

  • Your URL set up in the Twilio number configuration isn't actually reaching your function.
  • Your function is taking too long to respond. Twilio will throw 11200 if it doesn't get a response in a certain time.
  • Your response is formatted incorrectly. This is where the aforementioned strategy can help you diagnose the issue.