Skip to content

Instantly share code, notes, and snippets.

@nikmartin
Created November 9, 2015 21:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikmartin/239b58140366642233ef to your computer and use it in GitHub Desktop.
Save nikmartin/239b58140366642233ef to your computer and use it in GitHub Desktop.
Google Apps Script to produce Twilio TWIML
function doGet(e) {
var toNum = e.parameter.PhoneNumber;
var fromNum = e.parameter.CallerId;
var action = e.parameter.Direction;
var record = e.parameter.Record;
Logger.log(e);
var output = ContentService.createTextOutput();
var xml = createDialTWIML(fromNum, toNum, action);
output.setMimeType(ContentService.MimeType.XML);
output.setContent(xml);
return output;
}
function createDialTWIML(fromNum, toNum, action, record) {
var root = XmlService.createElement('Response');
var say = XmlService.createElement('Say')
.setAttribute('voice', 'alice')
.setAttribute('language', 'en-US')
.setText('This call will be recorded');
root.addContent(say);
var dial = XmlService.createElement('Dial')
.setAttribute('callerId', fromNum)
.setAttribute('timeout','60')
.setAttribute('record','record-from-answer');
var number = XmlService.createElement('Number').setText(toNum);
dial.addContent(number);
root.addContent(dial);
var document = XmlService.createDocument(root);
var xml = XmlService.getPrettyFormat().format(document);
Logger.log(xml);
return xml;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment