How To: Triggering a Phone Call from SharePoint using Microsoft Flow and Twilio

Today, I wanted to share with you how you can trigger a phone call, based on events happening in your SharePoint.

Recently we built a functionality for one of our customers where a number of people were registering for an in-person event. Registrants needed to provide their phone #, among other things, and the company wanted to verify that this is a valid number for cancellation and exception handling purposes. Obviously having someone dial hundreds of numbers was not an option.

We’ll take a look at a shorter version of this particular scenario just to keep things nice and short.

Create a list in SharePoint

I’m going go ahead and create a list on my SharePoint site. We aren’t going to go into details how information gets into this list, in our case, it came from online form, you can use some other process as long as data ends up being recorded in our SP list.

All of the events that are happening from now on are going to happen around this list. So, I’m going to add one more field to this list, [Single line of Text] type, called [Verified]. This one will indicate whether this particular person had their number verified by our system.

Microsoft Flow

Next, I’m going to switch to my flow, and create a new flow sequence. Flow has a variety of triggers, several of them are SharePoint related triggers.

I’m going to pick a trigger that says [when a new item is created] and in here I’m going to provide my SharePoint site, and pick my newly created list.

Now, we'll add an [Action] to our Flow. If I was to pick [Twilio] as a Action you'll see that Twilio doesn’t have the ability to make a phone call from Flow, not yet :). It only has the ability to send a text message, list text message, or get messages, so how do I make a phone call?

Well, one of the things Flow provides is the ability to make HTTP requests. So, I’m going to pick HTTP as my template with HTTP Action and this will allow me to perform a remote call to some service and have that service make a phone call.

I’m going to use Microsoft Azure and specifically use [Azure Function App] to make my call using Twilio.

Here is how my HTTP Action looks like per-filed:

In here, our [Method] is going to be [POST], the URL of the service is going to be my Azure service (we'll create that in a moment). Next is the body of the request.

  • To - number to call to (your own number for the demo purposes)
  • From - number to call from (Twilio # you purchase)
  • SID - Twilio Account SID (this comes from Twilio)
  • Token - Twilio Account Token (this comes from Twilio)

I’m going to go ahead and create my flow by clicking the [>] button.

Azure Function

Lets take a look at Azure function that facilitates making the phone call.

In this post, we’re going to take a look at the first part which is making a phone call to a registrant. In the next post, we’ll take a look at how we can actually modify SharePoint lists from Azure function to record recipients response.

Basically, Function App is essentially like running your class in a cloud. You don’t need to worry about IIS, compiling web application, pages - you don’t have any pages. If its just a function that executes some logic and Azure can facilitate that for you. It's not just Azure that’s out there that has these capabilities, Amazon also provides functionality called function apps.

To create a new Function App from Azure:

  • Click [new] and new, and search for "function app"
  • Next, we provide the app name which is going to get its own URL, in my case "phonesurvey"
  • Pick your Azure subscription, the resource group, all the standard things that you create for any Azure subscription, and click create.

This will take a minute to create. Once done, go ahead and fire up the app. Now, this is just a basic frame. Next, is we create the function itself so I’m going to Webhook in C#. Once done, we'll see a demo class.

I’m going to copy and paste the code I already have and kind of walk through it

#r "System.Runtime"

using System.Net;
using System.Text;
using Twilio;
using Twilio.TwiML;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, 
    TraceWriter log)
{

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("+", " "));

  var accountSid = formValues["SID"];
  var authToken = formValues["Token"];
  var toNum = new PhoneNumber(formValues["To"]);
  var fromNum = new PhoneNumber(formValues["From"]);
  TwilioClient.Init(accountSid, authToken);
  var call = CallResource.Create(
            toNum,
            fromNum,
            url: new Uri("https://phonesurvey.azurewebsites.net/api/Switchboard?code=0W8XNvmdhJ/5kVmi8nFl2EOoIyf6VhN0Oz8AQaI6uTxu45YIeSGH2w==")
        );

    var twiml = call.ToString();
    twiml = twiml.Replace("utf-16", "utf-8");

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

First things first, you can see that I’m using a couple of .net references here and also, I’m using Twilio references. Obviously, those need to come from somewhere so I need to reference them in my so-called function project.

I’m going to click on View Files and create a new file called project.json.

{
  "frameworks": {
    "net46": {
      "dependencies": {
        "Twilio": "5.0.2"
      }
    }
  }
}

I’m going to paste the following:

What this piece of code really does is kind of like you adding DLLs to your project. In this case, we're saying that this project is going to be using Twilio DLL and .net framework.

 

 

 

Back in to my function. We have our async task where we read the body of the request which was passed to us from Flow and collect accountSid (coming from Twilio), and authentication token that’s also coming from Twilio. So, to get these two you basically need to sign up for Twilio. Then, I’m also collecting the number from which the phone call was made and the number to which phone call will be made to. We can read that information from our SharePoint list and pass it as a parameter here, but in my case it's hard-coded. Finally we're going to use our Twilio class which preforms the phone with few parameters: numbers being called from and to and how we want the request to be handled.

In next post, we'll take a look at how to write our own handler where we can actually answer the phone call and provide our users with some sort of messaging and handle what buttons they press or what they say to the phone and save it back to SharePoint.

And that’s it! Let's test this and see if I can trigger a phone call from Microsoft Flow to my number by creating a list item in my list.

Testing it all

Switch back to your SharePoint list and create a new item and I’m going to provide my name, remember this information would have ordinarily have come from some other form etc. We wouldn’t necessarily want show our SharePoint list with other fields in it.

Now as this item has been created, Flow is monitoring this list and is going to make a call to Twilio and confirm whether this is a verified username or not.

And here is our phone call!

Twilio: ‘Do you confirm registration for SPS New York?  To confirm press 1.’
Me: I press 1
Twilio: ‘Thank you for confirming, see you there.’

Perfect! And the system automatically hangs up. In next post I'll also show you how to wire customer's answers back to the list so we know exactly who's verified:

Now the conference organizers know exactly how many people are coming and who’s verified, valid phone number.

Pretty cool or what? :)

Yaroslav Pentsarskyy has been a Microsoft MVP for 8 years and speaker at many local and worldwide tech events. Several of Yaroslav's books include: Rapid SharePoint 2013 development, Top 60 custom solutions built on SharePoint 2010, SharePoint 2010 branding in practice, and SharePoint and PowerShell Expert Cookbook.

@spentsarsky