text
stringlengths
0
152
#!/usr/bin/env python
# An example for AGI (Asterisk Gateway Interface).
import sys
def agi_command(cmd):
'''Write out the command and return the response'''
print cmd
sys.stdout.flush() #clear the buffer
return sys.stdin.readline().strip() # strip whitespace
asterisk_env = {} # read AGI env vars from Asterisk
while True:
line = sys.stdin.readline().strip()
if not len(line):
break
var_name, var_value = line.split(':', 1)
asterisk_env[var_name] = var_value
# Fake "database" of accounts.
ACCOUNTS = {
'12345678': {'balance': '50'},
'11223344': {'balance': '10'},
'87654321': {'balance': '100'},
}
response = agi_command('ANSWER')
# three arguments: prompt, timeout, maxlength
response = agi_command('GET DATA enter_account 3000 8')
if 'timeout' in response:
Example: Account Database Access
|
327
response = agi_command('STREAM FILE goodbye ""')
sys.exit(0)
# The response will look like: 200 result=<digits>
# Split on '=', we want index 1
account = response.split('=', 1)[1]
if account == '-1': # digits if error
response = agi_command('STREAM FILE astcc-account-number-invalid ""')
response = agi_command('HANGUP')
sys.exit(0)
if account not in ACCOUNTS: # invalid
response = agi_command('STREAM FILE astcc-account-number-invalid ""')
sys.exit(0)
balance = ACCOUNTS[account]['balance']
response = agi_command('STREAM FILE account-balance-is ""')
response = agi_command('SAY NUMBER %s ""' % (balance))
sys.exit(0)
Development Frameworks
There have been a number of efforts to create frameworks or libraries that make AGI
programming easier. You will notice that several of these frameworks also appeared in
Chapter 17. Just as with AMI, when evaluating a framework, we recommend you find
one that meets the following criteria:
Maturity
Has this project been around for a few years? A mature project is far less likely to
have serious bugs in it.
Maintenance
Check the age of the latest update. If the project hasn’t been updated in a few
years, there’s a strong possibility it has been abandoned. It might still be usable,
but you’ll be on your own. Similarly, what does the bug tracker look like? Are
there a lot of important bugs being ignored? (Be discerning here, since often the
realities of maintaining a free project require disciplined triage—not everybody’s
features are going to get added.)
Quality of the code
Is this a well-written framework? If it was not engineered well, you should be
aware of that when deciding whether to trust your project to it.
Community
Is there an active community of developers using this project? It’s likely you’ll
need help; will it be available when you need it?
328
|
Chapter 18: Asterisk Gateway Interface
Documentation
The code should be well commented, but ideally, a wiki or other official docu‐