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

Because the database integration here becomes much more natural, Asterisk dialplans
               becomes much more expressive as well. Below is a sample dialplan of a service provider
               that imposes a time limit on outgoing calls using information from the database. To
               remain simple:

                   # Let's assume we're offering VoIP service to customers
                   # whom we can identify with their callerid.
                   service {
                     # The line of code below performs an SQL SELECT
                     # statement on our database. The find_by_phone_number()
                     # method was created automatically because ActiveRecord
                     # found a phone_number column in the database. Adhearsion
                     # creates the "callerid" variable for us.
                     caller = Customer.find_by_phone_number callerid

                     usage = caller.usage_this_month
                     if usage >= 100.hours
                       play "sorry-cant-let-you-do-that"
                     else
                       play %w'to-hear-your-account-balance press-1
                               otherwise wait-moment'
                       choice = wait_for_digit 3.seconds
                       p choice
                       if choice == 1
                         charge = usage / 60.0 * caller.group.hourly_rate
                         play %W"your-account will-reflect-charge-of $#{charge}
                                 this month for #{usage / 60} minutes and
                                 #{usage % 60} seconds"
                       end
                       # We can also write back to the "usage_this_month"
                       # property of "caller". When the time method finishes,
                       # the database will be updated for this caller.
                       caller.usage_this_month += time do
                         # Code in this block is timed.
                         dial IAX/'main-trunk'/extension
                       end
                       caller.save
                     end
                   }
               Robust database integration like this through Adhearsion brings new ease to developing
               for and managing a PBX. Centrally persistent information allows Asterisk to integrate
               with other services cleanly while empowering more valuable services whose needs are
               beyond that of traditional Asterisk development technologies.

               Distributing and reusing code
               Because an Adhearsion application resides within a single folder, completely copying
               the VoIP application is as simple as zipping the files. For one of the first times in the
               Asterisk community, users can easily exchange and build upon one another’s successful


                                                           Asterisk Development with Adhearsion | 239
   262   263   264   265   266   267   268   269   270   271   272