Search code examples
c#wcftwiliowebhooks

Proper way to asynchronously respond to twilio webhook messages


My main application is ASP.NET MVC 5 for the frontend and a Windows wcf service for the backend. When handling the webhook callback in a controller things are fairly stright forward, but I moved the webhook to a webhttp wcf rest service. Which in itself was interesting since Twilio posts with application/x-www-form-urlencoded that isn't supported directly with webHttp rest. I got that working, I have the code if anyone is having the same problem.

I am also processing the webhook messages asynchronously by queuing them to another thread. So now I have 2 questions

  1. what should I be returning from the webhook? I am Currently returning HttpStatusCode.
  2. what is correct way to respond from the async thread? I am currently just using the MessageResource.Create method, which works, but is that the best way to do it?

Solution

  • what should I be returning from the webhook? I am Currently returning HttpStatusCode.

    If you are just acknowledging the webhook send a Status Code 200 (OK) or a Status Code 202 (Accepted). Other codes will cause Twilio to generate Errors in the Debug log and pester you with error notifications.

    what is correct way to respond from the async thread?

    If you are trying to send Twiml back to Twilio, for example to modify a call, then you can't use a queuing system (in another thread) and will need to respond in real-time to Twilio's webhooks.

    If you are trying to modify a call using the Twilio REST Api then sending any valid instruction will work. There are async methods if you want to use those such as MessageResource.CreateAsync to keep everything async.