ding-dong
ding-dong copied to clipboard
Asterisk AGI Gosub command
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