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

else speak "The German office is closed"
                     end
                   when US_NUMBER
                     dial SIP/'us-trunk-out'/extension
                   when /^\d{11,}$/ # Perl-like regular expression
                     # Pass any other long numbers straight to our trunk.
                     dial IAX/'intl-trunk-out'/extension
                   else
                     play %w'sorry invalid extension please-try-again'
                     end
                   }
               With just this small amount of code we accomplish quite a lot. Even with limited or
               no knowledge of Ruby, you can probably infer the following things:
                 • We use a switch-like statement on the “extension” variable (which Adhearsion
                   creates for us) and branch depending on that.
                 • Dialing a number between 10 and 99 routes us to the SIP peer with the dialed
                   numerical username.
                 • Any number dialed between 6000 and 6200 or between 7000 and 7030 goes to a
                   MeetMe conference of that same number. This of course requires meetme.conf to
                   have these conference numbers configured.
                 • The _'21XX' option comes straight from Asterisk’s pattern style. Prepending a
                   String with an underscore in Adhearsion secretly invokes a method that returns a
                   Ruby regular expression. In a Ruby “case” statement, regular expressions can be
                   used in a “when” statement to check against a pattern. The end effect should be
                   very familiar to those with extensions.conf writing experience.
                 • Adhearsion’s syntax for representing channels also comes straight from Asterisk’s
                   traditional format. SIP/123 can be used verbatim to represent the SIP peer 123. If
                   a trunk were involved, SIP/trunkname/username would act as you would expect.
                 • The speak() method abstracts an underlying text-to-speech engine. This can be
                   configured to use most of the popular engines.
                 • A full-blown Perl-like regular can be used in a when statement to perform more
                   sophisticated pattern matching if Asterisk’s patterns do not suffice.
                 • Adhearsion defines a few constants that may be useful to someone writing dia-
                   lplans.  The  US_NUMBER  constant  here  is  a  regular  expression  for  matching  an
                   American number.
                 • If you find the need to play several files in sequence, play() accepts an Array of
                   filenames. By luck, Ruby has a convenient way of creating an Array of Strings.
               This is of course just a simple example and covers only the absolute basics of Adhear-
               sion’s dialplan authoring capabilities.








               236 | Chapter 10: Asterisk Manager Interface (AMI) and Adhearsion
   259   260   261   262   263   264   265   266   267   268   269