Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Call Routing with Twilio and Force.com
<?xml version="1.0" encoding="UTF-8" ?>
<apex:page sidebar="false"
showHeader="false"
controller="ConnectorController"
contentType="application/xml">
<Response>
<Say>Hello {!callerName}. Connecting you with {!rep.Name}.</Say>
<Pause/>
<Dial>{!rep.Phone}</Dial>
</Response>
</apex:page>
public class ConnectorController {
public User rep {get; set;}
public String callerName {get; set;}
public ConnectorController() {
// Check the incoming params!
String accountSid = System.currentPageReference().getParameters().get('AccountSid');
if (accountSid != TwilioAPI.getDefaultAccount().getSid()) {
throw new TwilioRestException('Account SID mismatch',1);
}
String callSid = System.currentPageReference().getParameters().get('CallSid');
TwilioRestClient client = TwilioAPI.getDefaultClient();
TwilioCall call = new TwilioCall(client, callSid);
call.setRequestAccountSid(TwilioAPI.getDefaultAccount().getSid());
if (call.getStatus() != 'ringing') {
throw new TwilioRestException('Call SID mismatch',1);
}
// Look up contact from incoming caller id
String fromNumber = System.currentPageReference().getParameters().get('From');
List<Contact> callers = (fromNumber == null)
? null
: [SELECT Name, OwnerId
FROM Contact
WHERE Phone = :fromNumber OR MobilePhone = :fromNumber];
Contact caller = (callers != null && callers.size() == 1)
? callers[0]
: null;
callerName = caller.Name;
// Look up rep from contact
List<User> reps = (caller == null)
? null
: [SELECT Name, Phone
FROM User
WHERE Id = :caller.OwnerId];
// If we don't have a unique rep, connect with switchboard
rep = (reps != null && reps.size() == 1)
? reps[0]
: [SELECT Name, Phone
FROM User
WHERE LastName = 'Switchboard'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment