Page 251 - Asterisk™: The Future of Telephony
P. 251

sayit("subtraction-game-timesup")
                   saynumber(score)
                   sayit("subtraction-game-right")
                   saynumber(count)
                   sayit("subtraction-game-pct")
                   saynumber(pct)
               After the user is done answering the subtraction problems, she is given her score.
               As you have seen, the basics you should remember when writing AGI scripts in Python
               are:

                 • Flush the output buffer after every write. This will ensure that your AGI program
                   won’t hang while Asterisk is waiting for the buffer to fill and Python is waiting for
                   the response from Asterisk.
                 • Read data from Asterisk with the sys.stdin.readline command.
                 • Write commands to Asterisk with the sys.stdout.write command. Don’t forget
                   to call sys.stdout.flush after writing.

               The Python AGI Library

               If you are planning on writing lot of Python AGI code, you may want to check out Karl
               Putland’s Python module, Pyst. You can find it at http://www.sourceforge.net/projects/
               pyst/.


               Debugging in AGI

               Debugging AGI programs, as with any other type of program, can be frustrating. Luck-
               ily, there are two advantages to debugging AGI scripts. First, since all of the commu-
               nications between Asterisk and the AGI program happen over STDIN and STDOUT (and,
               of course, STDERR), you should be able to run your AGI script directly from the operating
               system. Second, Asterisk has a handy command for showing all of the communications
               between itself and the AGI script: agi debug.


               Debugging from the Operating System
               As mentioned above, you should be able to run your program directly from the oper-
               ating system to see how it behaves. The secret here is to act just like Asterisk does,
               providing your script with the following:
                 • A list of variables and their values, such as agi_test:1.
                 • A blank line feed (\n) to indicate that you’re done passing variables.
                 • Responses to each of the AGI commands from your AGI script. Usually, typing
                   200 response=1 is sufficient.





                                                                        Debugging in AGI | 223
   246   247   248   249   250   251   252   253   254   255   256