summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/dialplan/dtmf.lua
blob: 4dbd35fe93de6598e1c664c48be52dd3f248f66b (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
-- Gemeinschaft 5 module: dtmf class
-- (c) AMOOMA GmbH 2013
-- 

module(...,package.seeall)

Dtmf = {}

-- create dtmf object
function Dtmf.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.class = 'Dtmf';
  self.log = arg.log;
  self.bleg = arg.bleg
  self.digit_timeout = arg.digit_timeout or 5;
  self.router = arg.router;

  return object;
end


function Dtmf.detect(self, caller, sequence, digit, duration, calee)
  local timestamp = os.time();
  if timestamp - sequence.updated > self.digit_timeout then
    sequence.digits = digit;
  else
    sequence.digits = sequence.digits .. digit;
  end

  caller.dtmf_digits = sequence.digits;

  if calee then
    self.log:debug('DTMF_RECEIVER callee - digit: [', digit, '][', duration, '], sequence: ', sequence.digits);
  else
    self.log:debug('DTMF_RECEIVER caller - digit: [', digit, '][', duration, '], sequence: ', sequence.digits);
  end

  local route = self.router:route_run('dtmf', true);
  sequence.updated = timestamp;

  if not route then
    return;
  end

  if route.type == 'dialplanfunction' or route.type == 'phonenumber' or route.type == 'unknown' then
    self:transfer(caller, route.destination_number, calee)
  else
    self.log:notice('DTMF_RECEIVER - unhandled destination: ', route.type, '=', route.id);
  end
end


function Dtmf.transfer(self, caller, destination, calee)
  require 'common.fapi'
  local fapi = common.fapi.FApi:new{ log = log };
  local callee_uuid = caller:to_s('bridge_to');
  
  self.log:notice('DTMF_RECEIVER_TRANSFER - destination: ', destination, ', uuid: ', caller.uuid, ', callee_uuid: ', callee_uuid, ', callee_initiated: ', calee);
  if calee then
    caller:execute('transfer', destination);
    fapi:execute('uuid_kill', callee_uuid);
  else
    fapi:execute('uuid_transfer', callee_uuid .. ' ' .. destination);
    caller.session:hangup();
  end
end