Managing Incoming SMS from Twilio using Microsoft Flow

msflowIn this article, I would like to show you how to call Microsoft Flow from Twilio Functions and handle incoming SMS from Twilio in Dynamics CE or any other application which Microsoft Flow supports.

First of all, you need to have a number which can send and receive SMS. More information here: How to Search for and Buy a Twilio Phone Number from Console. To process incoming SMS, we will use Twilio Functions. There are several ways how you can handle incoming messages like WebHooks, TwiML bins, but I prefer Twilio Functions. Twilio Function is a serverless environment which empowers developers to quickly and easily create production-grade, event-driven Twilio applications. Twilio Functions are much more flexible comparing to WebHooks as you can put some more logic there.  Like Azure Functions from Microsoft. Using Twilio function, we can quickly process incoming SMS and redirect the request to our Microsoft Flow. Please navigate to the following URL and create a new function:  Twilio Functions. Let’s call this function “Incoming SMS”. The path is equal to “/smsin”. We will leave the code section as blank so far and update it later. Let’s register our function for receiving incoming SMS. Go to your phone number and register the function like on the screenshot below:


Now our function will be fired every time a new SMS arrived. Let’s now switch to Microsoft Flow and create a flow which will be able to handle requests from the Twilio Function. Here is how your flow might look like:


We are using standard HTTP request as input.  Here is how Request Body JSON Schema could look like:

  "type": "object",
  "properties": {
    "type": {
      "type": "string"
    "from": {
      "type": "string"
    "date": {
      "type": "string"
    "body": {
      "type": "string"

Next part is just to handle what we’ve got from an HTTP request in CRM. In this particular flow, we create a lead with some predefined parameters which were taken from the request. Now let’s switch back to Twilio and finish our function. Please turn back to your function. You can navigate to My Functions. Open your function and switch to “Configure” tab. We need to add one dependency from NPM to be able to use static HTTP post requests.


The last part is to create a code which will redirect information from our incoming SMS to the flow. As a part of message processing, you can also answer a user with some “Thank you!” message if needed.

var request = require('sync-request');
exports.handler = function(context, event, callback) {
    var twiml = new Twilio.twiml.MessagingResponse();
    twiml.message("Thank you for your message!");
    var res = request('POST', 'https://your_url:443/' + 'workflows/worflow_number/triggers/manual/paths/' + 'invoke?additionalparams', {
        json: {
            type: "sms",
            from: String(event.From),
            date: String(new Date()),
            body: String(event.Body)
    callback(null, twiml);

As a part of this function, we also return “Thank you for your message!” to the user who sent SMS. Don’t forget to correct the URL before testing. Now you are ready to test the complete procedure. I hope you got the idea of how to use Twilio functions together with Microsoft Flow. With this approach, you can handle complex scenarios and even implement a simple SMS bot if needed.

Leave a Reply

Your email address will not be published. Required fields are marked *