]> git.r.bdr.sh - rbdr/dead-drop/blob - lib/controllers/main_menu.js
461cff819e8a31ef11fb3ee817968e0c6981d809
[rbdr/dead-drop] / lib / controllers / main_menu.js
1 'use strict';
2
3 const Twilio = require('twilio');
4
5 const internals = {};
6
7 internals.kMenuTimeout = 10; // timeout in seconds
8 internals.kMenuDigits = 1; // number of digits in the menu
9 internals.kContentType = 'application/xml'; // The content type used to respond
10 internals.kMenuLanguage = 'es-mx'; // the language to use
11 internals.kTimeoutMessage = 'Oh... está bien. Adios!';
12 internals.kMainMenuRoute = '/menus/main';
13 internals.kRecordingMenuRoute = '/menus/recording';
14 internals.kRandomMessageRoute = '/recordings/0';
15 internals.kLeaveMessageRoute = '/recordings';
16 internals.kMenuMessage = 'Para dejar un mensaje, presiona 1. ' +
17 'Para escuchar un mensaje al azar, presiona 2. ' +
18 'Para escuchar un mensaje específico, presioan 3.'; // the message that will be shown
19 internals.kMenuInvalidResponseMessage = 'No entendí... Volviendo al menu principal.'; // invalid selection message
20 internals.kMenuOptions = {
21 leaveMessage: 1,
22 listenToRandomMessage: 2,
23 listenToSpecificMessage: 3
24 }; // the menu options
25
26 /**
27 * Handles the HTTP requests for the main menu
28 *
29 * @class MainMenuController
30 */
31 module.exports = internals.MainMenuController = class MainMenuController {
32
33 /**
34 * Serves the menu
35 *
36 * @function serveMenu
37 * @memberof MainMenuController
38 * @instance
39 * @return {generator} a koa compatible handler generator function
40 */
41 serveMenu() {
42
43 return function * () {
44
45 const response = new Twilio.TwimlResponse();
46
47 // the action will default to post in the same URL, so no change
48 // required there.
49 response.gather({
50 timeout: internals.kMenuTimeout,
51 numDigits: internals.kMenuDigits
52 }, function nestedHandler() {
53
54 this.say(internals.kMenuMessage, { language: internals.kMenuLanguage });
55 }).say(internals.kTimeoutMessage, { language: internals.kMenuLanguage });
56
57 this.type = internals.kContentType;
58 this.body = response.toString();
59 };
60 }
61
62 /**
63 * Parses the selected main menu response
64 *
65 * @function parseMenuSelection
66 * @memberof MainMenuController
67 * @instance
68 * @return {generator} a koa compatible handler generator function
69 */
70 parseMenuSelection() {
71
72 return function * () {
73
74 const menuSelection = parseInt(this.request.body.Digits);
75
76 const response = new Twilio.TwimlResponse();
77
78 if (menuSelection === internals.kMenuOptions.leaveMessage) {
79 response.redirect(internals.kLeaveMessageRoute, { method: 'GET' });
80 }
81 else if (menuSelection === internals.kMenuOptions.listenToRandomMessage) {
82 response.redirect(internals.kRandomMessageRoute, { method: 'GET' });
83 }
84 else if (menuSelection === internals.kMenuOptions.listenToSpecificMessage) {
85 response.redirect(internals.kRecordingMenuRoute, { method: 'GET' });
86 }
87 else {
88 response.say(internals.kMenuInvalidResponseMessage, { language: internals.kMenuLanguage })
89 .redirect(internals.kMainMenuRoute, { method: 'GET' });
90 }
91
92 this.type = internals.kContentType;
93 this.body = response.toString();
94 };
95 }
96 };