diff options
| author | Rubén Beltrán del Río <ben@nsovocal.com> | 2017-02-18 23:14:20 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-02-18 23:14:20 -0600 |
| commit | 7404eac982a0d928866aeddaa1a0ea4d1f2d3870 (patch) | |
| tree | 3834c528f1f88b52fcb33cc5025c6c7522bf21c7 /lib/controllers/main_menu.js | |
| parent | 5e981bca19ace2a7704f267175b6aa368e63bda0 (diff) | |
Handle Menus and Store / Recall recordings (#2)
* Update calls for menu parsing in paw
* Add twilio and bodyparser dependencies
* Integrate menu routes
* Add redirects to all menu selections.
* Add pause to menu message
* Update paw to handle all recordings cases
* Add rec menu handler, recordings dummy handler
* Add redis related configs
* Add redis node module
* Add missing comma to config
* Remove dangling comma from config
* Update paw to validate input
* Add post validation dependencies
* Update yarn lock
* Store recordings in redis
* Say post id in spanish
* Use the word for the pound sign
* Use a timestamp shifted by 2 and truncated to 10
Diffstat (limited to 'lib/controllers/main_menu.js')
| -rw-r--r-- | lib/controllers/main_menu.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/controllers/main_menu.js b/lib/controllers/main_menu.js new file mode 100644 index 0000000..461cff8 --- /dev/null +++ b/lib/controllers/main_menu.js @@ -0,0 +1,96 @@ +'use strict'; + +const Twilio = require('twilio'); + +const internals = {}; + +internals.kMenuTimeout = 10; // timeout in seconds +internals.kMenuDigits = 1; // number of digits in the menu +internals.kContentType = 'application/xml'; // The content type used to respond +internals.kMenuLanguage = 'es-mx'; // the language to use +internals.kTimeoutMessage = 'Oh... está bien. Adios!'; +internals.kMainMenuRoute = '/menus/main'; +internals.kRecordingMenuRoute = '/menus/recording'; +internals.kRandomMessageRoute = '/recordings/0'; +internals.kLeaveMessageRoute = '/recordings'; +internals.kMenuMessage = 'Para dejar un mensaje, presiona 1. ' + + 'Para escuchar un mensaje al azar, presiona 2. ' + + 'Para escuchar un mensaje específico, presioan 3.'; // the message that will be shown +internals.kMenuInvalidResponseMessage = 'No entendí... Volviendo al menu principal.'; // invalid selection message +internals.kMenuOptions = { + leaveMessage: 1, + listenToRandomMessage: 2, + listenToSpecificMessage: 3 +}; // the menu options + +/** + * Handles the HTTP requests for the main menu + * + * @class MainMenuController + */ +module.exports = internals.MainMenuController = class MainMenuController { + + /** + * Serves the menu + * + * @function serveMenu + * @memberof MainMenuController + * @instance + * @return {generator} a koa compatible handler generator function + */ + serveMenu() { + + return function * () { + + const response = new Twilio.TwimlResponse(); + + // the action will default to post in the same URL, so no change + // required there. + response.gather({ + timeout: internals.kMenuTimeout, + numDigits: internals.kMenuDigits + }, function nestedHandler() { + + this.say(internals.kMenuMessage, { language: internals.kMenuLanguage }); + }).say(internals.kTimeoutMessage, { language: internals.kMenuLanguage }); + + this.type = internals.kContentType; + this.body = response.toString(); + }; + } + + /** + * Parses the selected main menu response + * + * @function parseMenuSelection + * @memberof MainMenuController + * @instance + * @return {generator} a koa compatible handler generator function + */ + parseMenuSelection() { + + return function * () { + + const menuSelection = parseInt(this.request.body.Digits); + + const response = new Twilio.TwimlResponse(); + + if (menuSelection === internals.kMenuOptions.leaveMessage) { + response.redirect(internals.kLeaveMessageRoute, { method: 'GET' }); + } + else if (menuSelection === internals.kMenuOptions.listenToRandomMessage) { + response.redirect(internals.kRandomMessageRoute, { method: 'GET' }); + } + else if (menuSelection === internals.kMenuOptions.listenToSpecificMessage) { + response.redirect(internals.kRecordingMenuRoute, { method: 'GET' }); + } + else { + response.say(internals.kMenuInvalidResponseMessage, { language: internals.kMenuLanguage }) + .redirect(internals.kMainMenuRoute, { method: 'GET' }); + } + + this.type = internals.kContentType; + this.body = response.toString(); + }; + } +}; |