SMS from Salesforce Using Twilio API

Share on facebook
Share on twitter
Share on linkedin
Twilio is a cloud communication company that allows software developers to programmatically make and receive phone calls, and send and receive text messages using its web service APIs

Twilio API

          Twilio is a cloud communication company that allows software developers to programmatically make and receive phone calls, and send and receive text messages using its web service APIs

In this article, I have explained the steps for sending an SMS from Salesforce to mobile phones.

Step1: Twilio API Credentials

      First, create a trial account in Twilio.com and then get the API Credentials for making the API calls, and also create a phone number for your account from which you can send the SMS. In the trial account, you can send the SMS to only the verified numbers; so, you must verify all the phone numbers you are going to send the SMS.

Step 2: Remote Site Settings

Create a Remote Site Settings in Salesforce for the below URL

https://api.twilio.com

Step 3: Create an Apex class (Sendsms)

In this class, we need to replace the value of the AccountSid variable with the ACCOUNT SID value, also replace the value of the token variable with the AUTH TOKEN, and replace the value of the fromPhNumber variable with the From phone number you got from the Twilio Account.

public class Sendsms

{

   public String phNumber{get;set;}

   public String smsBody{get;set;}

   String accountSid;

   string token;

   String fromPhNumber;

   errorResponseWrapper erw;

   public sendsms(){

      phNumber ='+'+Apexpages.currentpage().getparameters().get('phNumber');

       accountSid = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';

       token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

       fromPhNumber = 'xxxxxxxx';

   }

 public void processSms(){

       HttpRequest req = new HttpRequest();

       req.setEndpoint('https://api.twilio.com/2010-04-01/Accounts/'+accountSid+'/SMS/Messages.json');

       req.setMethod('POST');

       String VERSION  = '3.2.0';

       req.setHeader('X-Twilio-Client', 'salesforce-' + VERSION);

       req.setHeader('User-Agent', 'twilio-salesforce/' + VERSION);

       req.setHeader('Accept', 'application/json');

       req.setHeader('Accept-Charset', 'utf-8');

       req.setHeader('Authorization','Basic '+EncodingUtil.base64Encode(Blob.valueOf(accountSid+':' +token)));

       req.setBody('To='+EncodingUtil.urlEncode(phNumber,'UTF-8')+'&From='+EncodingUtil.urlEncode(fromPhNumber,'UTF-8')+'&Body='+smsBody);

       Http http = new Http();

       HTTPResponse res = http.send(req);

       System.debug(res.getBody());

       if(res.getStatusCode()==201)

           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'SMS Sent Successfully'));

       else{

           erw =(errorResponseWrapper)json.deserialize(res.getBody(),errorResponseWrapper.class);

           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,erw.message));

       }

   }

   public class errorResponseWrapper{

       String code;

       String message;

       String moreInfo;

       String status;    

   }

}

Step 4: Create a Visualforce Page (To send the SMS)

<apex:page controller="Sendsms" sidebar="false" showHeader="false" title="Send SMS">

<apex:pagemessages />

<apex:form >

<br/>

<center><b>Send SMS</b> <br/><br/>

<b>To</b> &nbsp;&nbsp;&nbsp;&nbsp;<apex:inputtext value="{!phNumber}"/> <br/><br/><br/>

<b>Body</b>&nbsp;<apex:inputtext value="{!smsBody}"  /> &nbsp;<br/>(160 Char Max)<br/><br/><br/>

<apex:commandButton value="Send Sms" action="{!processSms}"/>

</center>

</apex:form>

</apex:page>

Step 5: Create a custom button (To send the SMS) in the Lead object, and add it to the page Layout.

Step 6: Execute the Visualforce Page

            Click the Send SMS button on the Lead object detail page. A Mobile number should be present in that lead record.

Step 7: Send SMS from Salesforce

Now, a popup window will appear for you to enter the SMS body and click the Send SMS button to send the SMS.

Steps 8: Receiving the SMS

Conclusion

                      By using the Twilio account, we can send SMS to any country. In the trial account, Twilio provides only the SMS for free. If you want to make a call using the API, You need to upgrade your account.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.