Salesforce Integration with Plivo using Rest Api
Visualforce Page:
<apex:page controller="PilvoCntrl" tabStyle="Account">
<apex:form id="frm">
<apex:outputText value="{!error}" style="color:green;font-weight:50px"/>
<apex:pageBlock title="Call From Plivo">
<apex:pageBlockSection collapsible="false" columns="2" title="Call Information">
<apex:commandButton value="Call" action="{!callPlivo}" reRender="frm"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Controller:
public class PilvoCntrl{
public String error{get;set;}
public PilvoCntrl(){}
public void callPlivo() {
String auth_id = 'Your Authentication id';
String auth_token = 'Your Authentication Token';
HttpRequest req = new HttpRequest();
String jsonString = '{"from": 9185599*****,"to": 919784******, "answer_url":"https://yourdomain-developer-edition.ap5.force.com/newCall"}';
req.setHeader('Authorization', 'Basic '+ EncodingUtil.base64Encode(Blob.valueOf(auth_id + ':' +auth_token )));
req.setHeader('Content-Type','application/json');
req.setEndpoint('https://api.plivo.com/v1/Account/'+auth_id+'/Call/');
req.setMethod('POST');
req.setBody(jsonString);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
error = 'Call Send Successfully....!!';
String response = res.getBody();
}
else{
String response = res.getBody();
error = 'Call Could not be completed '+response;
}
}
}
Note:
- This is the demo integration code of plivo from salesforce.
- You don't need to buy number from plivo for this code snippet but it should be verified from plivo
- In trail account phone numbers should be verified from plivo.
- You can get your auth_id and auth_token on your plivo dashboard
- you need to host the plivo xml for call redirect. It is good to use the salesforce site to host
that xml
here in code : https://yourdomain-developer-edition.ap5.force.com/newCall
Create new page newCall and copy paste the xml :
<apex:page showHeader="false" contentType="text/xml">
<Response>
<Dial >
<Number>918559******
</Dial>
</Response>
</apex:page>
Comments
Post a Comment