Passing asterisk variables to python script

Hello!I am new to asterisk but I have been searching for a while about how to pass the variables passed throw asterisk toa python script.
I have seen the example of sending email with the name of the recorded file but based on this code:
#This script emails the recorded call right after the call is hung up. Below is the code I used to send an email :

$1 - year

$2 - month

$3 - day

$4 - Time String

$5 - Source

$6 - File

$7 - Destination

$dt - Date and Time

dt=$(date ‘+%m/%d/%Y %r’);

echo -e "You have a new call recording to listen to \n\n
The call date and time was $dt \n\n
The call was from $5 \n\n
The call was to $7 \n\n

Please see the attached file \n\n" | mail -a /var/spool/asterisk/monitor/$1/$2/$3/$6 -s “New Call Recording”
What I need is that variables $1,$2,$3,$6 need to be used in a python script.
This python script needs to insert the name of the recorded file in a table of MySQL database.So the problem is that I dont know how to pass these asterisk variables to python script in a manner that the python script will be executed after a call is hungup.I used the GUI freepbx Post Call Recording Script to set the path of the bash script.But I want that python script to be executed and asterisk variables to appear when I execute this pythonScript?

There are a couple approaches you could take. I would use AGI() to call the python script.

exten => h,1,AGI(/path/to/,$1,$2,$3)

one = agi_arg_1 // = $1
two = agi_arg_2 // = $2
three = agi_arg_3 // = $3

The drawback with the above, variables need to be passed in the proper order.

You could try using pyst2 with python. Which will allow you the get variable from the session by name.

 same => n,Set(year=2018)
 same => n,Set(month=08)
 same => n,Set(day=24)
 same => n,Hangup()

exten => h,1,AGI(/path/to/

day = pyst2.get_variable('day')
month = pyst2.get_variable('month')
year = pyst2.get_variable('year')

How to check if this python script was executed after call hangup?

Enable verbose logging in Asterisk. Provide logging in the script itself.

Okay,I think I have to be clear what I want to achieve.
I have a bash script in /bin folder named that holds the following code:

#This script emails the recorded call right after the call is hung up. Below are the variables passed through asterisk

$1 - year

$2 - month

$3 - day

$4 - Time String

$5 - Source

$6 - File

$7 - Destination

$dt - Date and Time


And so I have the that holds the following code:

import sys as system

import MySQLdb

Open database connection

db = MySQLdb.connect(“x.x.x.x”,“user”,“password”,“database” )

print “connected”

prepare a cursor object using cursor() method

cursor = db.cursor()

execute SQL query using execute() method.

cursor.execute(“SELECT VERSION()”)

Prepare SQL query to INSERT a record into the database.

sql = “INSERT INTO recordedCall(fileName) VALUES (%s)”

val = system.argv[1]

Execute the SQL command


Commit your changes in the database


print val

print “inserted”


Rollback in case there is any error


disconnect from server


I wanted that script to be executed after a call was hanged up,so I used Post Script Call Recording field to write the following:
/var/lib/asterisk/bin/ ^{YEAR} ^{MONTH} ^{DAY} ^{TIMESTR} ^{FROMEXTEN} ^{CALLFILENAME}.^{MIXMON_FORMAT} ^{ARG3}.
But I want that when I call this .sh script ,the script that is called in .sh script to save in MySQL database in callRecorded table ,the name of the recorded file(it means a string that holds the full path of the recorded file)?

The examples I provided, show using the Hangup Extension. Where AGI() is used to call python script directly.

h: Hangup extension

When a call is hung up, Asterisk executes the h extension in the current context. This is typically used for some sort of clean-up after a call has been completed.

You could do the same with System(). Except you lose session information, that AGI() allows you to access.

exten => h,1,System(/var/lib/asterisk/bin/ ${YEAR} ${MONTH} ${DAY} ${TIMESTR} ${FROMEXTEN} ${CALLFILENAME}.${MIXMON_FORMAT} ${ARG3})

If you’re asking how to pass variables to the shell script, then on to a python script, you’re on your own. That’s not related to Asterisk.

Anyway I wanted to pass the asterisk variables that are in .sh script to pythonScript ,thankyou however.

That is a question about Python, not Asterisk.

Okay I have searched about ARI and I have activated rest interface,I have tested a program testari,now what I want is how can I record calls using ARI and get the filenames of the recorded calls using a python script?

For the ARI part you can get information on the followings links

The python part there are many ways to pass the file name to the script, but that part you will need to address your self

You helped a lot,thankyou, but I don’t know where ,how to use GET /recording/stored ?

You can use curl to access to GET /recording/stored and from there feed your script, I havent a while without use ARI.

Okay but how to create call recordings that are going to be saved in /recordings/stored because after setting the GET /recordings/stored method it says “message”: “Recording not found”…

And I am doing in this manner because I need to get call recordings filename to be saved in a MySQL table and than get those data from database and show them in a Java web application!
I can make calls with my application using softphone but now I need to get through call recordings.So how can I handle this ,how to save these call recordings names(such as ,a call is made a row is added ) to MySQL database?