Integration Twilio with Salesforce

Twilio Integration With Salesforce Part II - How to Receive A Message From A Phone Number in Salesforce

This blog is the second part of the two-part blog on integrating Twilio with Salesforce.

In this blog, I will be displaying how to receive an SMS in your Salesforce Org from your mobile phones.

dont miss out iconCheck out first part of the two-part blog here: Twilio Integration With Salesforce Part - I

To do this, we first need to create an Apex Rest Service class and a Force.com site.

Apex Rest Service class:

@RestResource(urlMapping='/TwilioReceiveSMS/*')
global class TwilioReceiveSMS {
    @HttpGet
    global static void getSMS() {
        // Store the request 
        RestRequest req = RestContext.request;       
        // Store the HTTP parameters from the request in a Map
        Map<String, String> sms = req.params ; 
        String fromPhNumber ;
        String smsBody ;       
        // get the phone number from the response 
        if (sms.containsKey('From')){
            fromPhNumber = sms.get('From') ;
        }
        // get the body of sms from the response 
        if (sms.containsKey('Body')){
            smsBody = sms.get('Body') ;
        }       
        System.Debug(‘This is you msg content you received : ‘ + smsBody);
    }
}

Note: Please perform any authorization checks necessary for this class when implementing it in a production environment.

In the above class TwilioReceiveSMS, a @RestResource annotation is used at the class level that enables you to expose an Apex class as a REST resource. The URL mapping is relative to https://yourinstance.salesforce.com/services/apexrest/ and at the end of your URL, you add the class name as shown above. The access modifier of the class is declared as Global since it will be interacting with the external system. (Note: The URL mapping is case-sensitive.)

Force.com site:

Force.com Sites enables developers to build and deploy public web sites and web applications using Force.com. This is a way to run your own website using the Force.com platform. First, you need to register your domain name. Then go to sites in settings and create a Force.com site. Assign a visual page to it which can act as the homepage for your site. For this example, I have created a custom Vsiualforce page with the controller as our TwilioReceiveSMS.apxc class.

<apex:page controller="TwilioReceiveSMS">
</apex:page>

Twilio will forward any message that comes on the Twilio Number to the website.

Note: This message will be received and processed as an unauthenticated user. So, do use validations to authenticate it.

  1. First, register your domain name. Setup > Quick Find > Sites > Enter a domain prefix and check its availability. Once you find one that is available, register.
  2. On the sites tab below, click new to create a site.
  3. Enter TwilioReceiveSMS as your site label, click the active checkbox, and assign a homepage to it. In this example, we will assign the custom VF page that we created before.
  4. Once this is done and your site is created, click on your site on the Site label column. Now,  click on Public Site Settings, and in the User License field on the Profile Detail section, enter Guest User License.
  5. We also need to give access to the Apex Rest Service class that we created before. In the Apps section of the page, click Apex Class Access > Edit > Choose your apex class (TwilioReceiveSMS.apxc) for the multi-select picklist.

Now, you need to configure your Twilio account to forward any SMS that comes on Twilio number to your Force.com site. On your Twilio console, click on Manage numbers. Click on the phone number assigned to you, Under the section of Messaging, on the message comes in the field, select Webhook and then the complete URL of your Force.com site i.e https://yourSiteDomainName/service/apexrest/TwilioReceiveSMS. Then specify the HTTP method as HTTP GET. Click Save.

Now send an SMS from your phone that is registered on Twilio. On the Twilio Console, under the Messaging Log section, select Incoming messages to see your message on the list. You can click on the entry to see the details.

In Salesforce, as shown in the class, you have the number of the person who sent you the SMS in the fromPhNumber variable and the content of the message in smsBody. Now you can query the leads if the lead is available in your org using fromPhNumber number and associate the message with that lead.

So, this blog gave a quick glimpse into how you can receive SMS from the cell phone in your org which can be a great help for the sales department since this will keep them updated with all the conversations they had with their leads all at one place from just their org without going through the hassle of scrolling conversation on the phone.

Popular Salesforce Blogs