Earlier today I presented a session at TwilioCon: Twilio and Salesforce: Building SMS and Voice into Force.com, covering two simple Twilio/Force.com integrations: SMS lead generation and call routing. Since I blogged the SMS lead generation sample back in April, here is the call routing case.
The code addresses a simple user story: “As a customer service manager, I want callers to be greeted by name and then automatically connected to their account representative.” There are four challenges here:
- Connecting a voice call with Salesforce
- Locating the records for the caller and their account rep
- Responding with the caller’s and rep’s names
- Redirecting the call to the account rep
Let’s look at each of them in turn.
Connecting a voice call with Salesforce
Twilio provides a markup language for controlling calls: TwiML (Twilio Markup Language). TwiML looks similar to HTML but, instead of marking up paragraphs and headers, the tags tell Twilio what to do when you receive an incoming call – for example, <say>Hello</say> will read the word ‘Hello’ to the caller. We can use a Visualforce page to render TwiML, with a controller handling the underlying logic. Looking at the controller first, Twilio performs an HTTP GET on a URL, with data such as the incoming caller ID, call ID and account ID sent as query parameters, so our controller’s constructor starts by using the Twilio library to verify the parameters:
Locating the records for the caller and their account rep
Now our controller’s constructor can use the incoming caller ID to look up a contact and, from there, the account rep. If we can’t find the matching contact or a matching rep, we use the ‘switchboard’ user.
Responding with the contact’s and rep’s names
Now that the controller has set up its rep property, the Visualforce page can render TwiML back to Twilio, greeting the caller by name, then explaining that the call with be connected to the named account rep:
I’m exposing the caller name as a String to avoid the need to configure Contacts for access from the site.
Connecting call with account rep
TwiML provides the <Dial> tag to connect the caller to the given number:
I’ve dropped the Visualforce page and controller code into a gist. If you want to try it out, you’ll need to install the Twilio Library for Salesforce, create a Twilio account (if you don’t already have one), and configure the TwilioConfig custom setting with your account credentials.
With the Twilio library installed and configured, and the
Connector page and
ConnectorConfig class in place, you’ll need to configure a site and follow the instructions here to make the
Connector page publicly visible. Create a phone number at Twilio, set your new page as its Voice Request URL, and dial away…