aboutsummaryrefslogtreecommitdiff
path: root/lib/controllers/recording_menu.js
blob: 55f2836ed358f034fe0bb7f44410c97442586408 (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
'use strict';

const Twilio = require('twilio');

const internals = {};

internals.kMenuTimeout = 10; // timeout in seconds
internals.kContentType = 'application/xml'; // The content type used to respond
internals.kMenuLanguage = 'es-mx'; // the language to use
internals.kMainMenuRoute = '/menus/main';
internals.kListenMessageRoute = '/recordings/';
internals.kMenuMessage = 'Escribe el numero de mensaje y presiona gato para terminar.';
internals.kTimeoutMessage = 'Bueno... volviendo al menú principal.';
internals.kMenuInvalidResponseMessage = 'No entendí... Volviendo al menu principal.'; // invalid selection message

/**
 * Handles the HTTP requests for the recording menu
 *
 * @class RecordingMenuController
 */
module.exports = internals.RecordingMenuController = class RecordingMenuController {

  /**
   * Serves the menu
   *
   * @function serveMenu
   * @memberof RecordingMenuController
   * @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
      }, function nestedHandler() {

        this.say(internals.kMenuMessage, { language: internals.kMenuLanguage });
      })
      .say(internals.kTimeoutMessage, { language: internals.kMenuLanguage })
      .redirect(internals.kMainMenuRoute, { method: 'GET' });

      this.type = internals.kContentType;
      this.body = response.toString();
    };
  }

  /**
   * Parses the selected recording id
   *
   * @function parseMenuSelection
   * @memberof RecordingMenuController
   * @instance
   * @return {generator} a koa compatible handler generator function
   */
  parseMenuSelection() {

    return function * () {

      const messageId = parseInt(this.request.body.Digits);

      const response = new Twilio.TwimlResponse();

      if (messageId) {
        response.redirect(`${internals.kListenMessageRoute}${messageId}`, { method: 'GET' });
      }
      else {
        response.say(internals.kMenuInvalidResponseMessage, { language: internals.kMenuLanguage })
            .redirect(internals.kMainMenuRoute, { method: 'GET' });
      }

      this.type = internals.kContentType;
      this.body = response.toString();
    };
  }
};