]>
Commit | Line | Data |
---|---|---|
7404eac9 RBR |
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'; | |
e5fdb2d4 RBR |
16 | internals.kMenuMessage = 'Marca 1 para grabar mensaje. ' + |
17 | 'Marca 2 para escuchar mensaje al azar. ' + | |
18 | 'Marca 3 si conoces el número de mensaje'; // the message that will be shown | |
7404eac9 RBR |
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 | }; |