Asterisk Gateway Interface (Agi) Scripting in Python: Muhammad Morshed Alam Amberit LTD
Asterisk Gateway Interface (Agi) Scripting in Python: Muhammad Morshed Alam Amberit LTD
Scripting in Python
• Case Study-1: Querying numeric data from DB using AGI scripting in python
• Case Study-2: Inserting / Updating data into the DB using AGI scripting
Note: all done by user over a live connected sip call session
Into. to AGI scripting
Requirements:
• You should have: Asterisk install machine (Asterisk 1.8) with a SIP Trunk (/etc/asterisk/sip.conf) and
some routing scripts at (/etc/asterisk/extension.conf)
Case-1: Querying Student Result Database against the ID/ Roll Number
• Call to the number 09611000196 give the entry of Student Roll number through DTMF
• AGI will return particular student registration number and current CGPA
• Asterisk will playback the return numeric value in live connected channel
Result DB:
<= AGI will return the registration_no (789123) & CGPA (3.5)
AGI scripts in python <Data Query from DB>
#!/usr/bin/python
import agi
import MySQLdb
import sys
agi = agi.AGI()
MYSQL_HOST='127.0.0.1'
MYSQL_USER='root'
MYSQL_PASS=‘morshedtest' rolno=sys.argv[1]
MYSQL_DB='admissions‘
mysql = MySQLdb.connect(host=MYSQL_HOST , user=MYSQL_USER, passwd=MYSQL_PASS, db=MYSQL_DB)
db = mysql.cursor()
db.execute("""SELECT registration_no,sgpa FROM academics WHERE roll_no='%(rolno)s' """ %vars())
result = db.fetchall()
db.close()
regno = int(result[0][0])
cgpa = float(result[0][1])
agi.set_variable("REGNO", regno)
agi.set_variable("CGPA", cgpa)
Asterisk Applications:
• SayDigits (123) => reads a specified number one digit at a time ( one two three and so on)
• SayNumber (numeric data) => reads the whole number (123, one hundred and twenty
three)
Writing Dial Plan in extension.conf for AGI script launching:
Dial Plan at /etc/asterisk/extension.conf:
• Off course verified by the contact center Agent and then transfer to an special
extension to set/reset the pin
exten => 09611000196,n,Read(atmcardno,enteratmcardno,16) ;take the 16 digit atm card no through dtmf
exten => 09611000196,n,AGI(bankpin.py,${atmcardno}, ${pinno})) ;launch the AGI to insert new pin to db
against the given atm card no