SALESFORCE INTEGRATION FROM TWILIO USING REST API
Integration with twilio using rest api.
Using twilio service we can send message and make call to any numbers(Numbers should be verified from twilio). For doing this we should have little knowledge of rest api.
Here i share snippet of the code of Visualforce page and the Apex controller for both sending message and making call :
For sending message :
Visualforce Page:
<apex:page controller="SendMessage" tabStyle="Send_Message__tab">
<apex:form id="frm">
<apex:outputText value="{!error}" style="color:green;font-weight:50px"/>
<apex:pageBlock title="Message Sender" >
<apex:pageBlockSection columns="2" title="Information" collapsible="false">
<apex:outputText label="Reciever Number"/>
<apex:inputText value="{!recievernum}" />
<apex:outputText label="Message"/>
<apex:inputTextarea value="{!msg}" />
<apex:commandButton action="{!messag}" value="Send Message" reRender="frm"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
<apex:page controller="SendMessage" tabStyle="Send_Message__tab">
<apex:form id="frm">
<apex:outputText value="{!error}" style="color:green;font-weight:50px"/>
<apex:pageBlock title="Message Sender" >
<apex:pageBlockSection columns="2" title="Information" collapsible="false">
<apex:outputText label="Reciever Number"/>
<apex:inputText value="{!recievernum}" />
<apex:outputText label="Message"/>
<apex:inputTextarea value="{!msg}" />
<apex:commandButton action="{!messag}" value="Send Message" reRender="frm"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
<apex:form id="frm">
<apex:outputText value="{!error}" style="color:green;font-weight:50px"/>
<apex:pageBlock title="Message Sender" >
<apex:pageBlockSection columns="2" title="Information" collapsible="false">
<apex:outputText label="Reciever Number"/>
<apex:inputText value="{!recievernum}" />
<apex:outputText label="Message"/>
<apex:inputTextarea value="{!msg}" />
<apex:commandButton action="{!messag}" value="Send Message" reRender="frm"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Controller :
public with sharing class SendMessage {
public String recievernum{get;set;}
public String msg{get;set;}
public String error{get;set;}
public void messag() {
String account = 'your twilio account id';
String token = 'Your twilio security token';
HttpRequest req = new HttpRequest();
req.setHeader('X-Twilio-Client', 'salesforce-' + '2010-04-01');
req.setHeader('User-Agent', 'twilio-salesforce-' + '2010-04-01');
req.setHeader('Accept','Application/JSON');
//req.setHeader('Content-Type', 'Application/JSON');
req.setHeader('Authorization', 'Basic '+ EncodingUtil.base64Encode(Blob.valueOf(account + ':' +token )));
req.setEndpoint('https://api.twilio.com'+'/'+'2010-04-01'+'/Accounts/'+account+'/SMS/Messages.json');
req.setMethod('POST');
req.setBody(''
+ 'From=' + EncodingUtil.urlEncode('+14242752341', 'UTF-8')
+ '&To=' + EncodingUtil.urlEncode(recievernum, 'UTF-8')
+ '&Body=' + EncodingUtil.urlEncode(msg, 'UTF-8')
);
HttpResponse res = new Http().send(req);
//check response
if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
error = 'Message Send Successfully....!!';
//System.debug('This is the response '+res.getBody());
String response = res.getBody();
}
public with sharing class SendMessage {
public String recievernum{get;set;}
public String msg{get;set;}
public String error{get;set;}
public void messag() {
String account = 'your twilio account id';
String token = 'Your twilio security token';
HttpRequest req = new HttpRequest();
req.setHeader('X-Twilio-Client', 'salesforce-' + '2010-04-01');
req.setHeader('User-Agent', 'twilio-salesforce-' + '2010-04-01');
req.setHeader('Accept','Application/JSON');
//req.setHeader('Content-Type', 'Application/JSON');
req.setHeader('Authorization', 'Basic '+ EncodingUtil.base64Encode(Blob.valueOf(account + ':' +token )));
req.setEndpoint('https://api.twilio.com'+'/'+'2010-04-01'+'/Accounts/'+account+'/SMS/Messages.json');
req.setMethod('POST');
req.setBody(''
+ 'From=' + EncodingUtil.urlEncode('+14242752341', 'UTF-8')
+ '&To=' + EncodingUtil.urlEncode(recievernum, 'UTF-8')
+ '&Body=' + EncodingUtil.urlEncode(msg, 'UTF-8')
);
HttpResponse res = new Http().send(req);
//check response
if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
error = 'Message Send Successfully....!!';
//System.debug('This is the response '+res.getBody());
String response = res.getBody();
For making call :
Visualforce Page:
<apex:page Tabstyle="Make_Call__tab" controller="MakeCallAndMessage" >
<apex:form id="frm">
<apex:pageBlock title="Caller Page">
<apex:pageBlockSection columns="2" title="Call Info" collapsible="false">
<apex:outputText label="Reciever Number"/>
<apex:inputText value="{!recievernum}" />
<apex:commandButton action="{!call}" value="Make Call" reRender="frm"/><br/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
<apex:page Tabstyle="Make_Call__tab" controller="MakeCallAndMessage" >
<apex:form id="frm">
<apex:pageBlock title="Caller Page">
<apex:pageBlockSection columns="2" title="Call Info" collapsible="false">
<apex:outputText label="Reciever Number"/>
<apex:inputText value="{!recievernum}" />
<apex:commandButton action="{!call}" value="Make Call" reRender="frm"/><br/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
<apex:form id="frm">
<apex:pageBlock title="Caller Page">
<apex:pageBlockSection columns="2" title="Call Info" collapsible="false">
<apex:outputText label="Reciever Number"/>
<apex:inputText value="{!recievernum}" />
<apex:commandButton action="{!call}" value="Make Call" reRender="frm"/><br/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Controller:
public with sharing class MakeCallAndMessage {
public String sendernum{get;set;}
public String recievernum{get;set;}
public String error{get;set;}
public void call() {
//these two tokens will appears on your twilio console home page
String account = 'Your twilio account id';
String token = 'Your twilio authentication token';
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.twilio.com' + '/' + '2010-04-01' + '/Accounts/' + account + '/Calls.json');
req.setMethod('POST');
req.setHeader('X-Twilio-Client', 'salesforce-' + '2010-04-01');
req.setHeader('User-Agent', 'twilio-salesforce-' + '2010-04-01');
//req.setHeader('Content-Type','application/JSON');
req.setHeader('Accept', 'application/JSON');
req.setHeader('Authorization', 'Basic '+ EncodingUtil.base64Encode(Blob.valueOf(account+':'+token)));
req.setBody(''
+ 'From=' + EncodingUtil.urlEncode('+91978463****', 'UTF-8')
+ '&To=' + EncodingUtil.urlEncode(recievernum, 'UTF-8')
+ '&Url=' + EncodingUtil.urlEncode('http://yourdomain-developer-edition.ap4.force.com/newmakecall', 'UTF-8')
);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
error = 'Message Send Successfully....!!';
//System.debug('This is the response '+res.getBody());
String response = res.getBody();
}
public with sharing class MakeCallAndMessage {
public String sendernum{get;set;}
public String recievernum{get;set;}
public String error{get;set;}
public void call() {
//these two tokens will appears on your twilio console home page
String account = 'Your twilio account id';
String token = 'Your twilio authentication token';
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.twilio.com' + '/' + '2010-04-01' + '/Accounts/' + account + '/Calls.json');
req.setMethod('POST');
req.setHeader('X-Twilio-Client', 'salesforce-' + '2010-04-01');
req.setHeader('User-Agent', 'twilio-salesforce-' + '2010-04-01');
//req.setHeader('Content-Type','application/JSON');
req.setHeader('Accept', 'application/JSON');
req.setHeader('Authorization', 'Basic '+ EncodingUtil.base64Encode(Blob.valueOf(account+':'+token)));
req.setBody(''
+ 'From=' + EncodingUtil.urlEncode('+91978463****', 'UTF-8')
+ '&To=' + EncodingUtil.urlEncode(recievernum, 'UTF-8')
+ '&Url=' + EncodingUtil.urlEncode('http://yourdomain-developer-edition.ap4.force.com/newmakecall', 'UTF-8')
);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
error = 'Message Send Successfully....!!';
//System.debug('This is the response '+res.getBody());
String response = res.getBody();
Note : You should follow some instruction for make call
make site in the salesforce or host the below code on any web server
<Response>
<Dial record="record-from-ringing-dual"> <Number>+91978463****</Number>
</Dial>
</Response>
make site in the salesforce or host the below code on any web server
<Response> <Dial record="record-from-ringing-dual"> <Number>+91978463****</Number> </Dial> </Response>
in our case the url is : http://yourdomain-developer-edition.ap4.force.com/newmakecall
Comments
Post a Comment