Skip to main content

Receiving SMS in Dynamics using Twilio Part 1

This guide will be following on from the sending SMS from dynamics posts using a plugin and also the flow method . We will re-use the same dynamics entity as well as Microsoft flow to receive our texts from Twilio.

In dynamics add another option in your text message status option set in your activity entity for “received”, we`ll use this to mark all incoming texts when created later on.

Head to Microsoft Flow and create a new blank flow . Add an HTTP request trigger “When an HTTP Request is received”. And copy and paste in the JSON schema below into the request body JSON schema.

{
    "type": "object",
    "properties": {
        "$content-type": {
            "type": "string"
        },
        "$content": {
            "type": "string"
        },
        "$formdata": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "key": {
                        "type": "string"
                    },
                    "value": {
                        "type": "string"
                    }
                },
                "required": [
                    "key",
                    "value"
                ]
            }
        }
    }
}

When a new text message is sent to our number, Twilio will make an HTTP request to our flow , the body of that request will be a JSON message.The above is the JSON schema for the message we`ll be receiving. There is more information here about the request from Twilio.

Your trigger should look similar to the above with the schema pasted in. We also need to send a response back to Twilio pretty much saying, everything is good we have received your request or there will be an error in our Twilio Console. Add a new action , search for response and add the action named Request ,response. Add in the status code of 204 and a body message.Save your flow and a URL will be generated. This will be our okay signal back to Twilio.

initialize two variables by adding new step, search for “Initialize variable”. We`ll use these variables for storing details of the incoming message. You`ll need two string variables name TextFrom and TextBody include quotation marks in your default values.

Because of the layout of the Message received from Twilio, we`ll need to loop through the entire message and grab the values in the specific keys we are interested in, in this case the From and Body.  Add a new step, select more and choose “Apply to each”.  Add the $formdata in to the step, this is the section of the message we want to loop through.

The next few steps are being added inside the “Apply to each” scope. Add a condition step, add the parameter “key is equal to From”. In the yes section add a Set variable action and set your TextFrom  to the value parameter

We have the number, now add another condition under the From condition this time set the parameter “key is equal to Body”. Then again add set variable action in your yes part of the condition to set your TextBody variable to the value parameter. Overall the apply to each section  should look like the below image. We only extracted the number and body of the text but there are bunch of other information sent by Twilio they are described here. You can add more variables and condition operators to gather more variables if you wish.

Last and final step in flow is to create the Text message in dynamics. Add a new “create record in dynamics” step, this time outside of the apply to each action. Connect to your dynamics instance , select your text messaging entity and add your  variables to the corresponding fields in dynamics the From number and your text message body field. Also set your text message status field to the value of your received option. The completed flow should look like below.

With the flow now completed we need to log into our Twilio account and instruct Twilio to send a request to this flow whenever a new text is received. Expand the first step in the flow, the HTTP trigger and copy the URL provided. Go to the Manage Numbers section , select your phone number and scroll down to the messaging section and configure as below .Paste your flow URL into the “a message comes in” webhook and make sure it is set as an HTTP post.

That’s it! any messages sent to the number will now be created as a record in your dynamics text messaging entity. In the second part we`ll look at ways in which we can automatically find the contact that sent the text if they exist in dynamics and automatically set the regarding  to the contact.

 

 

Charles Osei

Dynamics 365 Consultant @ RSM MCSE Business Applications Computer Science – University of Lincoln 2017 Nottingham D365/CRM user group Chapter Leader TDG – Community leader Twitter @charliedevxyz LinedIn https://www.linkedin.com/in/charlesosei/

5 thoughts to “Receiving SMS in Dynamics using Twilio Part 1”

      1. Any chance of you doing part 2? Struggling somewhat on the matching of the phone number to existing ones as Twilio pulls in +44 etc and that may not exist in Dynamics!

        1. Hi Sam, i`l get it done soon, but the trick is comparing only the last 10 digits with numbers in dynamics so this will work on +4407415066772 as well as 07415066772 as you are only checking for 7415066772

Leave a Reply to Charles Osei Cancel reply

%d bloggers like this: