Introduction
In my previous blog, I have explained how to send SMS from CRM dynamics using plugin C# code. In today's blog, I am going to explain how to track incoming SMS in CRM custom Activity SMS entity.
We will add Direction with Two options Incoming and Outgoing field on Custom Activity SMS entity to identify Incoming and Outgoing SMS.
When someone sends a text message to your Twilio number, Twilio can call a webhook you create in ASP.NET MVC from which you can send a reply back using TwiML. Twilio can send your web application an HTTP request when certain events happen, such as an incoming text message to one of your Twilio phone numbers. These requests are called webhooks, or status callbacks.
Steps to achieve above requirement,
- Get a Twilio trial account and get a Twilio number. Follow my previous blog
- Create MVC project in visual studio. Follow step by step article explained here
- Create controller class in MVP project to expose webhook which will be used by Twilio to post the request whenever someone send SMS to the Twilio number.
- Publish MVC application to Azure as web app and configure MVC webhook URL in Twilio which is explained here
Add below code to create SMS Custom Activity entity record from MVC
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor
public class SmsController: TwilioController {
static IOrganizationService _orgservice;
// GET: Sms
public TwiMLResult Index(SmsRequest incomingMessage) {
ConnectToMSCRM("CRMOnlineusername", "CRMOnlinePWD", "https://CRMORG.api.crm.dynamics.com/XRMServices/2011/Organization.svc");
Entity objSMS = new Entity("new_sms");
objSMS["new_from"] = incomingMessage.From;
objSMS["new_to"] = incomingMessage.To;
objSMS["new_messagetext"] = incomingMessage.Body + incomingMessage.AccountSid + incomingMessage.FromCity + incomingMessage.FromCountry + incomingMessage.FromState + incomingMessage.FromZip + incomingMessage.MessageStatus + incomingMessage.SmsSid + incomingMessage.ToCity + incomingMessage.ToCountry + incomingMessage.ToState + incomingMessage.ToZip;
objSMS["new_direction"] = false;
_orgservice.Create(objSMS);
var messagingResponse = new MessagingResponse();
messagingResponse.Message("The copy cat says: " + incomingMessage.Body);
return TwiML(messagingResponse);
}
public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri) {
try {
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;
Uri serviceUri = new Uri(SoapOrgServiceUri);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_orgservice = (IOrganizationService) proxy;
} catch (Exception ex) {
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();
}
}
}
Conclusion
In today's blog, we learned how we can easily track incoming SMS in Dynamics CRM.