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:
#!/bin/bash #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” email@domain.com
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/script.py,$1,$2,$3)
#/path/to/script.py
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/script.py)
#/path/to/script.py
day = pyst2.get_variable('day')
month = pyst2.get_variable('month')
year = pyst2.get_variable('year')
Okay,I think I have to be clear what I want to achieve.
I have a bash script in /bin folder named callrecordings.sh that holds the following code:
#!/bin/bash #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
python pythonScript.py
And so I have the pythonScript.py that holds the following code:
#!/usr/bin/python
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]
try:
Execute the SQL command
cursor.execute(sql,val)
Commit your changes in the database
db.commit()
print val
print “inserted”
except:
Rollback in case there is any error
db.rollback()
disconnect from server
db.close()
I wanted that callrecordings.sh 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/callrecording.sh ^{YEAR} ^{MONTH} ^{DAY} ^{TIMESTR} ^{FROMEXTEN} ^{CALLFILENAME}.^{MIXMON_FORMAT} ^{ARG3}.
But I want that when I call this .sh script ,the pythonScript.py 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.
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?
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?