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

CHAPTER 9

                   The Asterisk Gateway Interface (AGI)
















                                                Even he, to whom most things that most people would
                                                 think were pretty smart were pretty dumb, thought it
                                                                           was pretty smart.
                                                         —Douglas Adams, The Salmon of Doubt

               The Asterisk Gateway Interface, or AGI, provides a standard interface by which external
               programs may control the Asterisk dialplan. Usually, AGI scripts are used to do ad-
               vanced logic, communicate with relational databases (such as PostgreSQL or MySQL),
               and access other external resources. Turning over control of the dialplan to an external
               AGI script enables Asterisk to easily perform tasks that would otherwise be difficult or
               impossible.
               This chapter covers the fundamentals of AGI communication. It will not teach you how
               to be a programmer—rather, we’ll assume that you’re already a competent program-
               mer, so that we can show you how to write AGI programs. If you don’t know how to
               do computer programming, this chapter probably isn’t for you, and you should skip
               ahead to the next chapter.
               Over the course of this chapter, we’ll write a sample AGI program in each of the Perl,
               PHP, and Python programming languages. Note, however, that because Asterisk pro-
               vides a standard interface for AGI scripts, these scripts can be written in almost any
               modern programming language. We’ve chosen to highlight Perl, PHP, and Python be-
               cause they’re the languages most commonly used for AGI programming.

               Fundamentals of AGI Communication

               Instead of releasing an API for programming, AGI scripts communicate with Asterisk
               over  communications  channels  (file  handles,  in  programming  parlance)  known  as
               STDIN, STDOUT, and STDERR. Most computer programmers will recognize these channels,
               but just in case you’re not familiar with them, we’ll cover them here.



                                                                                     207
   230   231   232   233   234   235   236   237   238   239   240