Last active
April 3, 2020 11:23
-
-
Save NickHolcombe/e1c583602d4ba804d0462e71fee372bc to your computer and use it in GitHub Desktop.
Twilio function to turn an RSS feed into menu options that play a podcast - from https://www.brightec.co.uk/blog/podcasting-to-those-without-internet-access
This file contains hidden or 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
exports.handler = function(context, event, callback) { | |
let twiml = new Twilio.twiml.VoiceResponse(); | |
let Parser = require('rss-parser'); | |
let parser = new Parser(); | |
// Define the list of sermons and fetch the data | |
const url = '<enter the URL of RSS feed to read>'; | |
parser.parseURL(url, function (err, feed) { | |
if (err) { | |
twiml.say({voice: 'alice', language: 'en-gb'}, 'Sorry an error occurred fetching the sermons'); | |
callback(err, twiml); | |
twiml.hangup(); | |
} | |
// Validate we have some sermon data | |
const sermons = feed.items; | |
if (feed === undefined || sermons === undefined || sermons.length === 0) { | |
twiml.say({voice: 'alice', language: 'en-gb'}, 'Sorry an error occurred fetching the sermons'); | |
callback(null, twiml); | |
twiml.hangup(); | |
} | |
// Produce a menu, based on the sermons found, if no phone key(digit) was pressed... | |
if (event.Digits === undefined || event.Digits === '*' || event.Digits > sermons.length) { | |
const sermonMenu = sermons.map((sermon, index) => { | |
const sermonTitle = sermon.itunes.author+' speaking on '+sermon.title; | |
return 'Press '+index+' for '+sermonTitle | |
}).join(","); | |
const repeatInstructions = ', Press * to repeat these options'; | |
twiml.gather({numDigits: 1}) | |
.say({voice: 'alice', language: 'en-gb'}, 'Welcome to Dial A Sermon, '+sermonMenu+repeatInstructions) | |
} else { | |
// ... otherwise, get the right sermon based on the key pressed. | |
const sermonIndex = event.Digits; | |
const sermon = sermons[sermonIndex]; | |
if (sermon === undefined) { | |
twiml.say({voice: 'alice', language: 'en-gb'}, 'Sorry an error occurred fetching the sermons'); | |
callback(null, twiml); | |
twiml.hangup(); | |
} | |
twiml.say({voice: 'alice', language: 'en-gb'}, 'Please wait whilst we fetch the sermon.'); | |
twiml.play(sermon.enclosure.url); | |
twiml.hangup(); | |
} | |
callback(null, twiml); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment