Skip to main content

Sending SMS from Dynamics 365 using Twilio – Part 1

This guide will show you how to implement an SMS activity that will allow you to send text messages from within dynamics using Twilio . This version requires a plugin to be written however i will be showing you how to make a no code version using flow in part 2.

Twilio

First step is to head over to Twilio and sign-up for  a free trial account. After sign up head over to the Twilio dashboard and note down your ACCOUNT SID and AUTH Token. We`ll need those later. Head to the Manage Numbers section and if you do not already have a default number add one and note that  down too. Due to trial restrictions on Twilio you will also need to verify any phone numbers you want to send texts to here .

Dynamics

In dynamics i created an entity called text message ,  as an activity with the following fields.

  • Contact lookup  : The contact being sent the text
  • A text field: number for the contacts mobile number
  • An option set for text message status with the following options Draft, Sending and sent with draft being the default option
  • And finally body for the text message to be sent

I also added a real-time workflow on create which performs the following actions.

If  text message  status is equal to draft,  check if to contact mobile number contains data. If  no number on the contact an error is flagged to the user and the workflow terminates.  If everything’s okay the workflow then updates the text message record, sets the number field to the contact`s mobile number and changes the text message status to sending which then kicks off our send text plugin.

Plugin

We will be using the plugin to send a Rest request to the Twilio API, so you`ll need to download an additional assembly just like we would add the dynamics 365 SDK DLL`s from NuGet called RestSharp. With this additional assembly added, dynamics will throw us an error because it wont be able to find RestSharp on the server so you`ll need IL merge to merge Restsharp into our plugin assembly. Information on how to do that here. We are ready to add the plugin code. All you need to do is modify the code below to include your Twilio account details and add your dynamics entity schema names for your To and Body fields.

//Configure account details and Twilio phone number
string AccountSID = "YOURACCOUNTSID";
string AuthToken = "Auth Token";
//add the number exactly how it appears in twilio including area codes
string FromNumber = "+447415066772";
//Create Rest Client
var client = new RestClient("https://api.twilio.com")
{
  Authenticator = new HttpBasicAuthenticator(AccountSID,AuthToken)
};
var request = new RestRequest(string.Format("/2010-04-01/Accounts/{0}/Messages",AccountSID), Method.POST);
// Request parameters , replace Textmessage. to the schema names of your number and body fields
request.AddParameter("To", entity[TextMessage.Number].ToString());
request.AddParameter("From", FromNumber);
request.AddParameter("Body", entity[TextMessage.Body].ToString());
//execute the reqeuest to Twilio to send the text
IRestResponse response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.Created)
{
     //if text is successful do something
}
else
{
     // if text fails do something
}

Notes/ Additions

I recommend creating a separate configuration entity in dynamics where you read in your Twilio account details into the plugin. This allows you to change your account details on the fly without having to edit the plugin.

You can add in the if statement on success of the text message being sent, change your test message status to sent and vice versa to failed if the text failed to send as well as deactivate the text message activity

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/

2 thoughts to “Sending SMS from Dynamics 365 using Twilio – Part 1”

Leave a Reply

%d bloggers like this: