Apex Rest API – Salesforce Integration with Twilio(Get a Call on your Mobile Number from Salesforce Through Twilio Voice API)

First of all, you need to Create an account on Twilio. If you do not a have an account on Twilio please refer to the link.

If you have Already then just Log in to your Twilio account. After Log in Your Twilio Account, you will See ACCOUNTSID and AUTH TOKEN as shown in the screenshot below.

1st

Copy this ACCOUNTSID and AUTH TOKEN we will use it later.

After this Click on Left Side Icon in Twilio Account and click on # Phone Numbers

3rd

4th

You would see your Twilio number (you would receive calls from this number on your connected Mobile number)

5th

After this Move on to the Verified Caller IDs on Left Side Bar and Enter Your Mobile Number on which you want to receive Call and Verify by Twilio.

6th

Now Login into your Salesforce Account and create a Remote Site Setting of Twilio End Point URL as given in the Twilio API Documentation

2nd

For Beginners Here is the Explanation of Remote Site Setting.

“When you want to access the external sites in your Salesforce application using callouts, web services, etc… You need to add that in the Remote Site Settings. This is just a security that force.com platform is going to check.
Before any Visualforce page, Apex callout, or JavaScript code using XmlHttpRequest in an s-control or custom button can call an external site, that site must be registered in the Remote Site Settings page, or the call will fail”

Now moving on to the Integration part, according to the Twilio voice API . We Need DEFAULT VERSION , ACCOUNTSIDAUTH TOKEN , Twilio Phone Number(From Phone Number) , Twilio Verified Phone Number(To Phone Number), Voice API End Point for Doing this Integration From Salesforce .

7th

 

8th

 

Here is Apex Controller with Apex REST API and Visualforce Page


public with sharing class TwilioVoiceCallExt 
{
 public String mobileNumber {get;set;}
 public String responseBody {get;set;}
 public CallResponse callRes{get;set;}
 public String DEFAULT_VERSION = '2010-04-01';
 public String AccountSID = 'YOUR ACCOUNTSID';
 public String Authtoken = 'YOUR AUTH TOKEN';
 public String FromPhoneNumber = 'YOUR Twilio Phone Number';
 public String VoiceAPIEndPoint = 'https://demo.twilio.com/welcome/voice/';

 public String Body = '';

 public Void VoiceAPI()
 { 
 String Crediential = EncodingUtil.base64Encode(Blob.valueOf(AccountSID + ':' + Authtoken));
 String EndPoint = 'https://api.twilio.com/'+DEFAULT_VERSION+'/Accounts/'+AccountSID+'/Calls.json';

 if(mobileNumber != '')
 {
 Body = 'To='+mobileNumber+'&'+
 'From='+FromPhoneNumber+'&'+
 'Url='+VoiceAPIEndPoint;
 }
 else
 { 
 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Mobile Number is required.'); 
 ApexPages.addMessage(myMsg); 
 return;
 }

 Http h = new Http();
 HttpRequest request = new HttpRequest();
 request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
 request.setHeader('Authorization', 'Basic '+Crediential);
 request.setEndpoint(EndPoint);
 request.setMethod('POST');
 request.setBody(Body);
 HttpResponse res = h.send(request);
 responseBody = res.getBody();

 //------------Deserialize the Reponse in different Class------------------//
 callRes = (CallResponse)JSON.deserialize(responseBody, CallResponse.class);

 if(callRes.Status == 'queued')
 {
 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Call successfully sent..!! Please Check Entered Mobile Number'); 
 ApexPages.addMessage(myMsg); 
 }
 else if(callRes.status == '400'){
 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Mobile number is not valid'); 
 ApexPages.addMessage(myMsg); 
 } 

 }// End of function

 //-----class for get Response---------//
 public class CallResponse 
 {
 public String Status; 
 }
}

<apex:page sidebar="false" controller="TwilioVoiceCallExt">

<apex:sectionHeader title="Voice Call"/>
<apex:form >

<!-- message -->
<apex:pageMessages />

<apex:pageBlock >
In Case of Trail Twilio Account You can Call Only <b>One Mobile Number</b> which is <b>Verified</b> by Your Twilio Account !!
</apex:pageBlock>
<!-- Page block Start-->
<apex:pageBlock >

<!-- Page block Button-->
<apex:pageblockButtons >
<apex:commandButton action="{!VoiceAPI}" value="Click to Call"/>
</apex:pageblockButtons>

<apex:pageblockSection >
<apex:pageBlockSectionItem >
Mobile Number
<apex:inputText value="{!mobileNumber}" />
</apex:pageBlockSectionItem>
</apex:pageblockSection>

</apex:pageBlock>
<!-- Page block End-->
</apex:form>
</apex:page>

Refer Below Links for Complete Details of APEX REST API.

http://www.salesforcetutorial.com/salesforce-integration-rest-api-basics/

http://www.jitendrazaa.com/blog/salesforce/call-salesforce-rest-api-from-apex/

https://trailhead.salesforce.com/en/modules/apex_integration_services/units/apex_integration_rest_callouts

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at sfdcsagarsindhi@gmail.com .

 

Advertisements

2 thoughts on “Apex Rest API – Salesforce Integration with Twilio(Get a Call on your Mobile Number from Salesforce Through Twilio Voice API)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s