I’ve been doing a lot of with chatbots over the last few years. Recently I’ve been working with voice enabled solutions. At the time of writing it can be tricky to integrate voice and telephony solutions. After some exploratory research and a call with Twilio, I found a collection of APIs and endpoints with features that can pave the way for telephony and chatbot integrations.
One of the first things you need is the ability to programmatically answer a phone call and that’s where Twilio comes into play.
In this blog post I’ll show you how to setup the required services. You’ll also learn how to respond to the phone call in real-time using C#.
What is Twilio?
Twilio is a cloud communication platform that lets you connect to users or customers over a collection of channels such as SMS and voice using their suite of APIs.
What are the main Components?
A few components are involved when integrating programmatic phone answering capabilities with Twilio and C#.
- Twilio Account – to get the phone number
- ngrok – to securely expose your development machine over the web
- Twilio Phone Number Webhook – to tell Twilio where to route calls to
- Twilio NuGet Packages – for programmatic access to Twilio APIs
- ASP.NET Controller endpoint – code to run when Twilio routes your call
Let’s look at in more detail.
Twilio Account
You need is an account with Twilio and to create a phone number using the Twilio Console. You can get a free trial account which what that I used.
You’ll see from the screenshot below I’ve blurred the Voice POST URL. This is because it contains a publicly available endpoint that connects directly to my laptop. To get a publicly available endpoint you need to run ngrok (and code the webhook itself).
ngrok
This exposes your local development machine or webserver to the public internet over a secure connection. To run ngrok you use the following command:
ngrok http -host-header=”localhost:58821″ 58821
Note – you’ll substitute the port for your own host and port. When you run ngrok you’ll be assigned a dynamic address. This is what you can see here:
We need these as Twilio needs to be able to send an event to us at a configured webhook. Part of the webhook address includes values generated by ngrok.
Twilio Phone Number Webhook
The Twilio Phone Number configuration lets you can define what happens when an incoming call is received. One option you can is to tell Twilio to forward on the event to a webhook. This is simply an HTTP request.
Here you can see I’ve set my webhook to send a POST request to {ngrok values}/phone/receivecall:
When calls are received by our Twilio number they will be forwarded onto this address. The phone/receive elements on the POST URL point to code in your development environment.
Twilio .NET Core NuGet Packages
Two NuGet packages are used to give you access programmatic access to the Twilio API ecosystem. Twilio and Twilio.AspNet.Core:
ASP.NET Controller Endpoint
This contains the actual code for our webhook. Our example is simple. The webhook speaks to the callers on the phone then hangs up:
public class PhoneController : TwilioController { [HttpPost] public TwiMLResult ReceiveCall() { var response = new VoiceResponse(); response.Say("Hello my name is the Terminator. How can I help you?"); return TwiML(response); } } }
One thing to notice the controller inherits from TwilioController.
An Example
I can’t demo this in action on a blog so I’ve recorded a 90 second video which you can see here.
In this video I’ve streamed my Android phone to my laptop. You can see me calling my Twilio number which is turn execute code on my local machine. Grab your popcorn!
Summary
In this blog post we’ve introduced Twilio. We’ve seen how to setup a phone number with Twilio and create a webhook that can answer phone calls.
We used ngrok to make our machine publicly available and discoverable by Twilio.
Are you doing anything with telephone or building voice solutions?
https://vaithuhayho.blogspot.com/
Some really interesting information, well written and generally user genial.