]>
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.kContentType = 'application/xml'; // The content type used to respond | |
9 | internals.kMenuLanguage = 'es-mx'; // the language to use | |
10 | internals.kMainMenuRoute = '/menus/main'; | |
11 | internals.kListenMessageRoute = '/recordings/'; | |
12 | internals.kMenuMessage = 'Escribe el numero de mensaje y presiona gato para terminar.'; | |
13 | internals.kTimeoutMessage = 'Bueno... volviendo al menú principal.'; | |
14 | internals.kMenuInvalidResponseMessage = 'No entendí... Volviendo al menu principal.'; // invalid selection message | |
15 | ||
16 | /** | |
17 | * Handles the HTTP requests for the recording menu | |
18 | * | |
19 | * @class RecordingMenuController | |
20 | */ | |
21 | module.exports = internals.RecordingMenuController = class RecordingMenuController { | |
22 | ||
23 | /** | |
24 | * Serves the menu | |
25 | * | |
26 | * @function serveMenu | |
27 | * @memberof RecordingMenuController | |
28 | * @instance | |
29 | * @return {generator} a koa compatible handler generator function | |
30 | */ | |
31 | serveMenu() { | |
32 | ||
33 | return function * () { | |
34 | ||
35 | const response = new Twilio.TwimlResponse(); | |
36 | ||
37 | // the action will default to post in the same URL, so no change | |
38 | // required there. | |
39 | response.gather({ | |
40 | timeout: internals.kMenuTimeout | |
41 | }, function nestedHandler() { | |
42 | ||
43 | this.say(internals.kMenuMessage, { language: internals.kMenuLanguage }); | |
44 | }) | |
45 | .say(internals.kTimeoutMessage, { language: internals.kMenuLanguage }) | |
46 | .redirect(internals.kMainMenuRoute, { method: 'GET' }); | |
47 | ||
48 | this.type = internals.kContentType; | |
49 | this.body = response.toString(); | |
50 | }; | |
51 | } | |
52 | ||
53 | /** | |
54 | * Parses the selected recording id | |
55 | * | |
56 | * @function parseMenuSelection | |
57 | * @memberof RecordingMenuController | |
58 | * @instance | |
59 | * @return {generator} a koa compatible handler generator function | |
60 | */ | |
61 | parseMenuSelection() { | |
62 | ||
63 | return function * () { | |
64 | ||
65 | const messageId = parseInt(this.request.body.Digits); | |
66 | ||
67 | const response = new Twilio.TwimlResponse(); | |
68 | ||
69 | if (messageId) { | |
70 | response.redirect(`${internals.kListenMessageRoute}${messageId}`, { method: 'GET' }); | |
71 | } | |
72 | else { | |
73 | response.say(internals.kMenuInvalidResponseMessage, { language: internals.kMenuLanguage }) | |
74 | .redirect(internals.kMainMenuRoute, { method: 'GET' }); | |
75 | } | |
76 | ||
77 | this.type = internals.kContentType; | |
78 | this.body = response.toString(); | |
79 | }; | |
80 | } | |
81 | }; | |
82 |