luaasterisk

Asterisk Hangup Handler using lua


I am trying to Add a hangup handler in my dial plan to catch unanswered calls from dial with multiple extensions. But unfortunately I my hangup handlers are not executing at all. Below is my extensions.lua

CONSOLE = "Console/dsp" -- Console interface for demo
IAXINFO = "guest"       -- IAXtel username/password
TRUNK = "SIP"

GSMTRUNK = "Dongle/dongle0/"
PRITRUNK = "DAHDI/g0/"
TRUNKMSD = 1
local inspect = require ('inspect')
--from here onwards our logic is there Rajesh
function dialtomobind(number)
        app.Dial(GSMTRUNK..number, 50,'egb(add-hangup-handler^'..number..'^1)')
        local dialstatus = channel['DIALSTATUS']:get()
        app.NoOp('DIAL STATUS:'..dialstatus)
        app.Hangup(16)
end

function add_hangup_handler(context, extension)
        channel['hangup_handler_push']='hangup-handlers,s,1'
        app.NoOp('pre-dial handler, Adding Hangup Handler'..channel['hangup_handler_push']:get())
        app.Return()
end

function h_handler(context, extension)
        app.NoOp('After Hangup:'..extension)
        --app.DumpChan(3)
end

function directdialing(c,e)
        app.Answer(5)
        app.Playback('tt-monkeys')
        dialtomobind(string.sub(e, 4))
        app.Hangup(16)
end


function hangup_handler_1(c,e)
        --channel['hangup_handler_push']
        app.NoOp('Hangup Handler 1 Executed')
        app.Return()
end

extensions = {
        ['from-internal-sip']={
                ['h']=h_handler;
                ['_500XXXXXXXXXX']=directdialing;
        };
        ['dongle_incoming']={
                ['_.']=gsm_dongle_handler;
        };
        ['add-hangup-handler']={
                ['_.']=add_hangup_handler;
        };
        ['hangup-handlers']={
                ['_.']=hangup_handler_1;
        };
}

ASTERISK CLI OUT PUT:

    -- Executing [5008884882772@from-internal-sip:1] Answer("SIP/8884882772-00000047", "5")
       > 0x7f14c80072a0 -- Probation passed - setting RTP source address to 192.168.2.30:54032
[Aug 29 18:24:17] NOTICE[26470][C-000000c5]: res_rtp_asterisk.c:4478 ast_rtp_read: Unknown RTP codec 95 received from '192.168.2.30:54032'
    -- Executing [5008884882772@from-internal-sip:1] Playback("SIP/8884882772-00000047", "tt-monkeys")
    -- <SIP/8884882772-00000047> Playing 'tt-monkeys.gsm' (language 'en')
    -- Executing [5008884882772@from-internal-sip:1] Dial("SIP/8884882772-00000047", "Dongle/dongle0/8884882772,50,egb(add-hangup-handler^8884882772^1)")
    -- Dongle/dongle0-010000000e Internal Gosub(add-hangup-handler,8884882772,1) start
    -- Executing [8884882772@add-hangup-handler:1] NoOp("Dongle/dongle0-010000000e", "pre-dial handler, Adding Hangup Handlerhangup-handlers,s,1")
    -- Executing [8884882772@add-hangup-handler:1] Return("Dongle/dongle0-010000000e", "")
  == Spawn extension (dongle_incoming, 5008884882772, 1) exited non-zero on 'Dongle/dongle0-010000000e'
    -- Dongle/dongle0-010000000e Internal Gosub(add-hangup-handler,8884882772,1) complete GOSUB_RETVAL=
    -- Called Dongle/dongle0/8884882772
    -- Dongle/dongle0-010000000e is making progress passing it to SIP/8884882772-00000047
  == Spawn extension (from-internal-sip, 5008884882772, 1) exited non-zero on 'SIP/8884882772-00000047'
    -- Executing [h@from-internal-sip:1] NoOp("SIP/8884882772-00000047", "After Hangup:h")

Solution

    1. hangup_handler_push is not at all a channel variable. So Can't access using below methods.

      channel['hangup_handler_push']='hangup-handlers,s,1'
      

      or

      channel['hangup_handler_push']='hangup-handlers,s,1'
      
    2. To setup a hangup handler we have to use dial plan function CHANNEL(https://wiki.asterisk.org/wiki/display/AST/Hangup+Handlers). So below methods will be working.

      channel.CHANNEL('hangup_handler_push'):set('hangup-handlers,s,1')
      
    3. As Asterisk Set Application Can be used to set a channel Variables as well as can call a asterisk Functions also. So below code also working.

      app.Set('CHANNEL(hangup_handler)=hangup-handlers,s,1');
      

    Reference: Dial Plan Functions In Lua https://wiki.asterisk.org/wiki/pages/viewpage.action?pageId=16548029