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

result = re.search('result=(\d+)',params)
                         if (not result):
                            sys.stderr.write("FAIL ('%s')\n" % params)
                            sys.stderr.flush()
                            return -1
                         else:
                            result = result.group(1)
                            #debug("Result:%s Params:%s" % (result, params))
                            sys.stderr.write("PASS (%s)\n" % result)
                            sys.stderr.flush()
                            return result
                      else:
                         sys.stderr.write("FAIL (unexpected result '%s')\n" % params)
                         sys.stderr.flush()
                         return -2
               The checkresult function is almost identical in purpose to the checkresult subroutine
               in the sample Perl AGI script we covered earlier in the chapter. It reads in the result of
               an Asterisk command, parses the answer, and reports whether or not the command
               was successful.
                   def sayit (params):
                      sys.stderr.write("STREAM FILE %s \"\"\n" % str(params))
                      sys.stderr.flush()
                      sys.stdout.write("STREAM FILE %s \"\"\n" % str(params))
                      sys.stdout.flush()
                      result = sys.stdin.readline().strip()
                      checkresult(result)
               The sayit function is a simple wrapper around the STREAM FILE command.

                   def saynumber (params):
                      sys.stderr.write("SAY NUMBER %s \"\"\n" % params)
                      sys.stderr.flush()
                      sys.stdout.write("SAY NUMBER %s \"\"\n" % params)
                      sys.stdout.flush()
                      result = sys.stdin.readline().strip()
                      checkresult(result)
               The saynumber function is a simple wrapper around the SAY NUMBER command.
                   def getnumber (prompt, timelimit, digcount):
                      sys.stderr.write("GET DATA %s %d %d\n" % (prompt, timelimit, digcount))
                      sys.stderr.flush()
                      sys.stdout.write("GET DATA %s %d %d\n" % (prompt, timelimit, digcount))
                      sys.stdout.flush()
                      result = sys.stdin.readline().strip()
                      result = checkresult(result)
                      sys.stderr.write("digits are %s\n" % result)
                      sys.stderr.flush()
                      if result:
                         return result
                      else:
                         result = -1





                                                                 Writing AGI Scripts in Python | 221
   244   245   246   247   248   249   250   251   252   253   254