Get Asterisk's variable in AGI script

I have agi script and I want, when that agi file called (which written in python). I can fetch all asterisk’s variable inside that agi file. Like Extension, Caller ID, Channel, etc.
I have to log all this in one excel file so in future I can refer that for some purpose.

exten => 1001,1,answer()
exten => 1001,n,AGI(AGICode.agi) or 1001,n,AGI(AGICode.agi,$(agi_channel),$(agi_extension),$(agi_callerid)))
exten => 1001,n,hangup()

As you see above dialplan, which one is correct way to pass Asterisk’s Variable?

For example check below Image, you can see this all variable.

Also if I have pass these variables then How to get then inside script?
Like this?

import sys
from asterisk.agi import *
agi = AGI()
channelID = sys.argv[0]
extension = sys.argv[1]
calledID = sys.argv[2]

or if we don’t require to pass variable (direct get them inside script) then how?

Those variables are already been passed to the AGI script. Look to the:

AGI Tx >> agi_channel: PJSIP/1011-00000001

Note: The Tx >> means a line sent by Asterisk to the AGI, and Rx << is from AGI to Asterisk.

You are using a library/SDK called “asterisk.agi”, you need to look to the documentation to know how to retrieve the variables that Asterisk always pass to an AGI at the beginning.

If you want to pass other variables (arguments) to an AGI you can do it this way, but using { } not ( ). Like this:

exten => 1001,n,AGI(AGICode.agi,${varname1},${varname2})

From the python script you will have this variables in the ARGV.

Do you have a specific list of variables or do you really mean ‘all?’

I don’t think there is a way to enumerate through ALL variables.

You can get the value of some variables from the AGI environment passed to your AGI, by asking Asterisk using the AGI commands ‘get variable’ and ‘get full variable,’ and by passing the value to the AGI() application.

Personally, I would retrieve these variables from the AGI environment and then overwrite if specified in the AGI() arguments.

Also, I would specify the AGI() arguments in ‘Unix command line long option’ format.

For example, instead of:

same = n,agi(example,T,F,1001,sedwards,N)

I would code it up like:

same = n,agi(example,--caller-id=sedwards,--debug,--extension=1001,--test,--upload)

and then parse the ‘command line’ using ‘getopt_long()’ in my AGI.

Although ‘wordy,’ this is ‘dead-obvious’ what each argument is used for and is ‘position independent’ so I never have to think 'hey, what was the 5th argument to that AGI I wrote x years ago and I can pass the arguments in whatever order (usually alphabetic) makes the most sense.

It also makes life easier when debugging the AGI.