summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/dialplan/ivr.lua
blob: f8b8a2d2c41ed7879a8b39add40553e30d8889b3 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-- Gemeinschaft 5 module: ivr class
-- (c) AMOOMA GmbH 2013
-- 

module(...,package.seeall)

Ivr = {}

-- create ivr ivr ;)
function Ivr.new(self, arg)
  arg = arg or {}
  ivr = arg.ivr or {}
  setmetatable(ivr, self);
  self.__index = self;
  self.class = 'ivr';
  self.log = arg.log;
  self.caller = arg.caller;
  self.dtmf_threshold = arg.dtmf_threshold or 500;

  return ivr;
end


function Ivr.ivr_phrase(self, phrase, keys, timeout, ivr_repeat)
  ivr_repeat = ivr_repeat or 3;
  timeout = timeout or 30;
  self.digit = '';
  self.exit = false;

  self.break_keys = {};
  for index=1, #keys do
    self.break_keys[keys[index]] = true;
  end

  global_callback:callback('dtmf', 'ivr_ivr_phrase', self.ivr_phrase_dtmf, self);

  for index=0, ivr_repeat do
    self.caller.session:sayPhrase(phrase, table.concat(keys, ':'));
    self.caller:sleep(timeout * 1000);
    if self.exit then
      break;
    end
  end

  global_callback:callback_unset('dtmf', 'ivr_ivr_phrase');

  return self.digit;
end


function Ivr.ivr_phrase_dtmf(self, dtmf)
  if self.break_keys[dtmf.digit] then
    self.digit = dtmf.digit;
    self.exit = true;
    return false;
  end
end


function Ivr.read_phrase(self, phrase, phrase_data, max_keys, min_keys, timeout, enter_key)
  self.max_keys = max_keys or 64;
  self.min_keys = min_keys or 1;
  self.enter_key = enter_key or '#';
  self.digits = '';
  self.exit = false;
  timeout = timeout or 30;

  global_callback:callback('dtmf', 'ivr_read_phrase', self.read_phrase_dtmf, self);
  self.caller.session:sayPhrase(phrase, phrase_data or enter_key or '');
  self.caller:sleep(timeout * 1000);
  global_callback:callback_unset('dtmf', 'ivr_read_phrase');

  return self.digits;
end


function Ivr.read_phrase_dtmf(self, dtmf)
  if dtmf.duration < self.dtmf_threshold then
    return nil;
  end

  if self.enter_key == dtmf.digit then
    self.exit = true;
    return false;
  end

  self.digits = self.digits .. dtmf.digit;
end


function Ivr.check_pin(self, phrase, pin, pin_timeout, pin_repeat, key_enter)
  if not pin then
    return nil;
  end

  pin_timeout = pin_timeout or 30;
  pin_repeat = pin_repeat or 3;
  key_enter = key_enter or '#';

  local digits = '';
  for i = 1, pin_repeat do
    if digits == pin then
      self.caller:send_display('PIN: OK');
      break
    elseif digits ~= "" then
      self.caller:send_display('PIN: wrong');
    end
    self.caller:send_display('Enter PIN');
    digits = ivr:read_phrase(phrase, nil, 0, pin:len() + 1, pin_timeout, key_enter);
  end

  if digits ~= pin then
    self.caller:send_display('PIN: wrong');
    return false
  end
  self.caller:send_display('PIN: OK');

  return true;
end