aboutsummaryrefslogtreecommitdiff
path: root/lib/controllers/main_menu.js
blob: 461cff819e8a31ef11fb3ee817968e0c6981d809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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();
    };
  }
};