Page 250 - Asterisk™: The Future of Telephony
P. 250
The getnumber function calls the GET DATA command to get DTMF input from the caller.
It is used in our program to get the caller’s answers to the subtraction problems.
limit=20
digitcount=2
score=0
count=0
ttanswer=5000
Here, we initialize a few variables that we’ll be using in our program.
starttime = time.time()
t = time.time() - starttime
In these lines we set the starttime variable to the current time and initialize t to 0. We’ll
use the t variable to keep track of the number of seconds that have elapsed since the
AGI script was started.
sayit("subtraction-game-welcome")
Next, we welcome the caller to the subtraction game.
while ( t < 180 ):
big = random.randint(0,limit+1)
big += 10
subt= random.randint(0,big)
ans = big - subt
count += 1
#give problem:
sayit("subtraction-game-next");
saynumber(big);
sayit("minus");
saynumber(subt);
res = getnumber("equals",ttanswer,digitcount);
if (int(res) == ans) :
score+=1
sayit("subtraction-game-good");
else :
sayit("subtraction-game-wrong");
saynumber(ans);
t = time.time() - starttime
This is the heart of the AGI script. We loop through this section of code and give
subtraction problems to the caller until 180 seconds have elapsed. Near the top of the
loop, we calculate two random numbers and their difference. We then present the
problem to the caller, and read in the caller’s response. If the caller answers incorrectly,
we give the correct answer.
pct = float(score)/float(count)*100;
sys.stderr.write("Percentage correct is %d\n" % pct)
sys.stderr.flush()
222 | Chapter 9: The Asterisk Gateway Interface (AGI)