Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asterisk AGI Gosub command #15

Open
granjerox opened this issue May 25, 2019 · 0 comments
Open

Asterisk AGI Gosub command #15

granjerox opened this issue May 25, 2019 · 0 comments

Comments

@granjerox
Copy link

granjerox commented May 25, 2019

I've just needed to use gosub from my agi dialplan to reuse an old routine. I realized that context.gosub call resolves imediatly and found it wird. These are my findings and I wonder if its the right way to do it or not. Anyhow i'm sure it will help others.

Preparation:

extensions.ael

context start_agi_test {
	s => {
		AGI("agi://localhost:4001");
	}
}

context myTest {
	s => {
		Wait(2);
		Return(GOSUB_AGI_TEST_OK);
	}
}

gosubTest.js

"use strict;"
const AGIServer = require('ding-dong');            // https://github.com/antirek/ding-dong
const { performance } = require('perf_hooks')
const time = (ms) => { try { return new Date(ms).toISOString().slice(11, -1) } catch (err) {return `${(ms) && ms} --> ${err.message}`}}   // Convert Miliseconds to hh:mm:ss.mmm - Ref: https://zaiste.net/convert_miliseconds_to_time/

(async () => {
    const agi = AGIServer((context) => {
        let t_start
        context.onEvent('variables')
            .then( async function(vars) { 
                t_start = performance.now()
                console.log(`[${time(performance.now() - t_start)}] Conection received`)
                const gosubRes = await context.gosub('myTest','s','1')
                if (gosubRes) {
                    console.log(`[${time(performance.now() - t_start)}] Gosub result ${gosubRes.code} ${gosubRes.result}`)
                } else {
                    console.log(`[${time(performance.now() - t_start)}] Gosub result ${gosubRes}`)
                }
                //const noOp = await context.noop()
                let gosubRetVal = await context.getVariable('GOSUB_RETVAL')
                if (gosubRetVal) {
                    console.log(`[${time(performance.now() - t_start)}] First getVariable GOSUB_RETVAL code={${gosubRetVal.code}} result={${gosubRetVal.result}} value={${gosubRetVal.value}}`)
                } else {
                    console.log(`[${time(performance.now() - t_start)}] getVariable GOSUB_RETVAL ${gosubRetVal}`)
                }

                gosubRetVal = await context.getVariable('GOSUB_RETVAL')
                if (gosubRetVal) {
                    console.log(`[${time(performance.now() - t_start)}] Second getVariable GOSUB_RETVAL code={${gosubRetVal.code}} result={${gosubRetVal.result}} value={${gosubRetVal.value}}`)
                } else {
                    console.log(`[${time(performance.now() - t_start)}] getVariable GOSUB_RETVAL ${gosubRetVal}`)
                }

                gosubRetVal = await context.getVariable('GOSUB_RETVAL')
                if (gosubRetVal) {
                    console.log(`[${time(performance.now() - t_start)}] Third getVariable GOSUB_RETVAL code={${gosubRetVal.code}} result={${gosubRetVal.result}} value={${gosubRetVal.value}}`)
                } else {
                    console.log(`[${time(performance.now() - t_start)}] getVariable GOSUB_RETVAL ${gosubRetVal}`)
                }


                await context.end()
            } )
            .catch((err) => {console.log(`Excepción capturada en el contexto general ${err}`); context.end()})

            context.onEvent('close')
                .then( function(res){ console.log(`[${time(performance.now() - t_start)}] Connection Closed`) } )

            context.onEvent('hangup')
                .then( function(res){ console.log(`[${time(performance.now() - t_start)}] Connection HungUp`) } )
        })
    agi.start(4001)
    console.log('AGI Started')
})();

Execution
on asterisk side

asteriskpbxpre02*CLI> originate local/s@start_agi_test application wait 4
    -- Executing [s@start_agi_test:1] AGI("Local/s@start_agi_test-00000011;2", ""agi://localhost:4001"") in new stack
    -- Called s@start_agi_test
    -- Local/s@start_agi_test-00000011;2 AGI Gosub(myTest,s,1) start
    -- Executing [s@myTest:1] Wait("Local/s@start_agi_test-00000011;2", "2") in new stack
    -- Executing [s@myTest:2] Return("Local/s@start_agi_test-00000011;2", "GOSUB_AGI_TEST_OK") in new stack
  == Spawn extension (start_agi_test, s, 1) exited non-zero on 'Local/s@start_agi_test-00000011;2'
    -- Local/s@start_agi_test-00000011;2 AGI Gosub(myTest,s,1) complete GOSUB_RETVAL=GOSUB_AGI_TEST_OK
    -- <Local/s@start_agi_test-00000011;2>AGI Script agi://localhost:4001 completed, returning 0
    -- Auto fallthrough, channel 'Local/s@start_agi_test-00000011;2' status is 'UNKNOWN'

on server side

~/Projects/acca-agi$ node ./gosubTest.js
AGI Started
[00:00:00.000] Conection received
[00:00:00.017] Gosub result 100 0 Trying...
[00:00:02.019] First getVariable GOSUB_RETVAL code={200} result={0 Gosub complete} value={undefined}
[00:00:02.035] Second getVariable GOSUB_RETVAL code={200} result={1} value={GOSUB_AGI_TEST_OK}
[00:00:02.049] Third getVariable GOSUB_RETVAL code={200} result={1} value={GOSUB_AGI_TEST_OK}
[00:00:02.052] Connection Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant