Macro returning value back to original context

I know that I have seen this done somewhere before. Basically, I call a macro, set a variable, and I would like to use that variable later in the channel after the macro has returned. (I want it to work the way a function does). Also, It cannot be a global variable because I will have multiple concurrent calls and I don’t want to risk it being set by another channel.

Is this possible

Example

exten => 555,1,Answer()
same => n,Macro(foo)
same => n,NoOp(testVariable)

[macro-foo]
exten => s,Set(testVariable=“test”)

[macro-foo]
exten => s,Set(__testVariable=“test”)

Not used it the way you propose but worth trying

Ian
www.cyber-cottage.co.uk

Hm interesting. I tried setting one of these in my dialplan and then editing it in a macro but that did not seem to work. I just need some way to modify a variable in a macro in a way that the parent context will be able to use it.

Why not use a subroutine?

Because I actually need to call the macro from the Dial() function usng the M() parameter. I’m asking the person being called if they want to accept or decline.

Hi

Worked for me as expect on 1.4

exten => 555555,1,Answer()
exten => 555555,n,Macro(foo,testVariable)
exten => 555555,n,NoOp(${testVariable})

[macro-foo]

exten => s,1,Set(__${ARG1}=test)
exten => s,n,Noop(${testVariable})
 -- Executing [555555@international:1] Answer("SIP/2206-ab2ad6d0", "") in new stack
    -- Executing [555555@international:2] Macro("SIP/2206-ab2ad6d0", "foo|testVariable") in new stack
    -- Executing [s@macro-foo:1] Set("SIP/2206-ab2ad6d0", "__testVariable=test") in new stack
    -- Executing [s@macro-foo:2] NoOp("SIP/2206-ab2ad6d0", "test") in new stack
    -- Executing [555555@international:3] NoOp("SIP/2206-ab2ad6d0", "test") in new stack

ianplain, your solution is a good one. Unfortunately it does not seem to work while using the Dial function

Example snippet from my dialplan

same => n,Set(__FOOBAR=fest)
same => n,Dial(SIP/${dialNumber}@baldwintelecom,25,rM(callScreening,${dialNumber},FOOBAR)gL(180000:60000:30000))
same => n,NoOp(${FOOBAR})
[macro-callScreening]

exten => s,1,Answer()
        same => n,Set(dialNumber=${ARG1})
        same => n,Set(__${ARG2}=test)
        same => n,NoOp(From Macro: ${FOOBAR})
        same => n,AGI(agi://localhost/callScreening.agi?dialNumber=${dialNumber})

and the CLI

-- Executing [s@macro-callScreening:3] Set("SIP/baldwintelecom-00000057", "__FOOBAR=test") in new stack -- Executing [s@macro-callScreening:4] NoOp("SIP/baldwintelecom-00000057", "From Macro: test") in new stack -- Executing [s@macro-callScreening:5] AGI("SIP/baldwintelecom-00000057", "agi://localhost/callScreening.agi?dialNumber=7157814987") in new stack -- <SIP/baldwintelecom-00000057> Playing 'receivecallfrom.gsm' (language 'en') -- <SIP/baldwintelecom-00000057>AGI Script agi://localhost/callScreening.agi?dialNumber=7157814987 completed, returning 0 -- Executing [s@macro-startCall:9] NoOp("SIP/1274206-00000056", "fest") in new stack

This is nothing to do with contexts. The problem is that the macro is being run on a different channel. It is also a channel that has already been created before the macro is run, so there will be no inheritance on channel creation. For 1.4, I think you have no choice but to use globals or astdb.

Hi

From the snippet of cli output you have macros calling macros and then they are calling agi’s

You might want to sanitize your code a bit.

Ian