Twilioforce – Click to Call Demo | Appirio Tech Blog

To help you with your entries for the Twilio Developer Challenge that runs until Dreamforce I’ve put together a quick demo of a Click To Call function for Salesforce CRM. Basically, the demo adds a link to the Contact Page Layout and will connect your phone to the Contact’s Phone with a simple click.

First, download the Twilioforce library. It’s a good set of starter code. I had to fix a few defects to get this demo working, so the SVN trunk has been updated with the latest code. In addition to the code, you’re going to need a few custom fields to get this to run. I created an unmanaged package to make that easier.

Before you get started, create a Force.com Sites entry for the Visualforce Page called TwilioDialer. Add the controller and the Twilioforce classes to the public access list.

Take a look at the controller class before we fill in the configuration data. We’re using the TwilioRestClient and TwilioRestResponse classes from the Twilioforce library to make our calls a bit more efficiently.

public PageReference ClickToCallAction() {
TwilioRestClient client = new TwilioRestClient(CONFIG.AccountSID, CONFIG.AuthToken, null);
Map params = new Map();
params.put('Caller', CONFIG.OutGoingCallerID);
params.put('Called', TwilioTarget);
params.put('Url', CONFIG.ClickToCallURL + '?Called=' + EncodingUtil.urlEncode(contact.phone, 'UTF-8'));

TwilioRestResponse response;
try {
response = client.request(‘/’+CONFIG.APIVERSION+’/Accounts/’+CONFIG.AccountSID+’/Calls’, ‘POST’, params);
ReturnCode = ‘Called Placed: Connecting ‘ + TwilioTarget + ‘ with ‘ + contact.Phone;
return null;
} catch (Exception e) {
ReturnCode = ‘ERROR: ‘ + e;
return null;
}
}

1) Click on the TwilioConfig tab and create a new record. (The demo has a LIMIT 1 query so you can only have a single working config record).
2) Enter your Twilio developer info. Add an OutGoing Caller ID number that Twilio has validated.
3) Enter the URL for the Force.com Sites entry you created above.
4) Go to your User record and add your preferred phone number for Twilio calls

The package has a new page layout for Contact. If everything was done correctly you should see something that resembles the following illustration:

Clicking on the number will kick off the following events:
1) Make a REST call to Twilio’s /Calls method requesting an outbound call to the phone number you entered on your User account record. (Sending the Force.com Sites URL for callback)
2) Twilio places the call (and greets you w/ the demo script if you haven’t set up your paid account yet) and fetches the TwiML in the Force.com Sites Page.
3) The <dial> element is read and the Contact’s Phone (which was passed in the parameter string) is dialed from your phone.

Hopefully, this will give you a headstart on your developer challenge application. Good luck and let me know if you have any questions.


Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s