Last active
December 23, 2015 00:49
-
-
Save metadaddy/6556403 to your computer and use it in GitHub Desktop.
Visualforce TwiML page and Apex REST method for transcribing a voice message in Twilio and saving is as an Activity to a Lead.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestResource(urlMapping='/messagetotask') | |
global class MessageToTask { | |
@HttpPost | |
global static void incomingMessage() { | |
String expectedSignature = | |
RestContext.request.headers.get('X-Twilio-Signature'); | |
String url = 'https://' + RestContext.request.headers.get('Host') + | |
'/services/apexrest' + RestContext.request.requestURI; | |
Map <String, String> params = RestContext.request.params; | |
// Validate signature | |
if (!TwilioAPI.getDefaultClient().validateRequest(expectedSignature, url, params)) { | |
RestContext.response.statusCode = 403; | |
RestContext.response.responseBody = Blob.valueOf('Failure! Rcvd '+expectedSignature+'\nURL '+url/*+'\nHeaders'+RestContext.request.headers*/); | |
return; | |
} | |
// Twilio likes to see something in the response body, otherwise it reports | |
// a 502 error in https://www.twilio.com/user/account/log/notifications | |
RestContext.response.responseBody = Blob.valueOf('ok'); | |
// Extract useful fields from the incoming transcription | |
String leadNumber = params.get('Caller'); | |
String campaignNumber = params.get('To'); | |
String transcription = params.get('TranscriptionText'); | |
// Look for a matching lead | |
List<Lead> leads = [SELECT Id FROM Lead WHERE Phone = :leadNumber LIMIT 1]; | |
Lead lead; | |
if (leads.size() > 0) { | |
// We found a match! | |
lead = leads[0]; | |
} else { | |
// Create and insert a new Lead | |
lead = new Lead(LastName = 'From Transcription', | |
Company = 'From Transcription', | |
Phone = leadNumber); | |
try { | |
insert lead; | |
} catch (DmlException dmle) { | |
reply(campaignNumber, leadNumber, 'An error occurred. Sorry.'); | |
return; | |
} | |
} | |
// Create and insert a new Task | |
Task task = new Task( | |
ActivityDate = Date.today(), | |
CallDisposition = 'Lead left a message', | |
CallType = 'Inbound', | |
Description = transcription + | |
'\n\nMessage recording at https://api.twilio.com/2010-04-01/Accounts/'+params.get('AccountSid')+'/Recordings/'+params.get('RecordingSid'), | |
Status = 'Completed', | |
Subject = 'Phone Message from '+leadNumber, | |
Type = 'Call', | |
WhoId = lead.Id | |
); | |
insert task; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<apex:page sidebar="false" showHeader="false" contentType="application/xml"> | |
<Response> | |
<Say>Hello! Please leave a message.</Say> | |
<Record maxLength="120" transcribe="true" transcribeCallback="/services/apexrest/messagetotask"/> | |
</Response> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do i test this class and page
Do i need to open this vf page in mobile.
Please let me know how to test this