Salesforce Twilio Integration

In this blog, I am going to explain how to establish the two-way communication between Salesforce and Twilio. As you all know, communication between any two systems always involves Sends “Request” and receive “Response”. Now we will see how this happens between Salesforce and Twilio
Step 1:
Create a trail account with Twilio. Go to https://www.twilio.com/ and sign up for free. After that generate your own Twilio number to send SMS to Salesforce and Receive SMS from Salesforce
Step 2:
Create an Apex Controller Class and the VF Page that sends the message to any number by using Twilio REST API. The sample UI can be found below.

The code snippet for the same is available at  https://github.com/rajamohanvakati/Salesforce-Twilo-Integration-
Code Walkthrough:
Here is the Twilio Configuration detail


     VERSION = '3.2.0';
     Endpoint = 'https://api.twilio.com';
     DEFAULT_VERSION = '2010-04-01';

   String finalURL = endpoint + '/' + DEFAULT_VERSION + '/Accounts/' + this.accountSid + '/SMS/Messages.json';

Here is the Sudo code

 Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setHeader('X-Twilio-Client', 'salesforce-' + VERSION);
        request.setHeader('User-Agent', 'twilio-salesforce/' + VERSION);
        request.setHeader('Accept', 'application/json');
        request.setHeader('Accept-Charset', 'utf-8');
        request.setHeader('Authorization', 'Basic '+EncodingUtil.base64Encode(Blob.valueOf(this.accountSid + ':' + this.authToken)));
        request.setEndpoint(uri.toExternalForm());
        request.setMethod('POST');
        request.setBody(reqBody);
        HttpResponse res = h.send(request);

Step 3: Receiving Inbound message from the Twilio to Salesforce
 To do this simply I build a publicly accessible Rest-based API. In order to build the publicly based API, I created a public facing site and then added the API as public acceptable without any authentication.

Here is the code for the same.

  @RestResource(urlMapping = '/SMStoApexReply/')

global class TwillioIncomingMessages {
    @HttpPost
    global static String readIncoingSMS() {  
        RestRequest request = RestContext.request;  
        RestResponse response = RestContext.response; 
        system.debug('request'+request);
        system.debug('response'+response);

        request.addHeader('Accept', '*/*');
        Map<String,String> headers = request.headers ; 
        Map<String,String> reqParams = request.params ;

        String fromNumber = reqParams.get('FROM');
        fromNumber.replaceAll('(','');
        fromNumber.replaceAll(')','');
        fromNumber.replaceAll('-','');
        String toNumberNumber = reqParams.get('TO');
        toNumberNumber.replaceAll('(','');
        toNumberNumber.replaceAll(')','');
        toNumberNumber.replaceAll('-','');

        SMS_Reply__c  rq = new SMS_Reply__c ();
        rq.SMS__c =[Select Id from SMS__c where From_Number__c =:fromNumber AND To_Number__c =:toNumberNumber order by CreatedDate].Id ;
        rq.Reply_Message__c= reqParams.get('Body');
        insert rq ; 
        return 'Success';  
    }  

}

Step 4: Configure the above rest based URI with Twilio.
go to https://www.twilio.com/console/sms/dashboard  and Add new message services after that update Twilio outbound message URL with the Salesforce public rest API to receive incoming SMS from Twilio to Salesforce. same is shown in this picture

Step 5:  Explore Twilio Cookies to understand how to map both Sent message and replied messages.
https://twilio.radicalskills.com/projects/sms-respond/2.html

Here is the Git URL for entire project
https://github.com/rajamohanvakati/Salesforce-Twilo-Integration-