Python EAGI with modules

Is it possible to use custom python modules when using EAGI in my dialplan?
I have broken down a script into modules and it no longer works.

if I do a python filename.py I see that the file executes without import errors, crashing at a point where I am trying to read from the file descriptor 3, because that would be available only if it were running as an EAGI script.

Short answer: yes.

I followed this doc: http://www.asteriskdocs.org/en/2nd_Edition/asterisk-book-html-chunk/asterisk-CHP-9-SECT-4.html

and tried to avoid using the while 1 logic here:

while 1:
   line = sys.stdin.readline().strip()

   if line == '':
      break
   key,data = line.split(':')
   if key[:4] <> 'agi_':
      #skip input that doesn't begin with agi_
      sys.stderr.write("Did not work!\n");
      sys.stderr.flush()
      continue
   key = key.strip()
   data = data.strip()
   if key <> '':
      env[key] = data

To

while line != '':
    key, data = line.split(':')
    key = key.strip()
    data = data.strip()

    if 'agi_' != key[:4]:
        __console.err('Did not work for ' + key)
        continue

    env[key] = data
    line = sys.stdin.readline().strip() # I had forgotten to add this line leading to an infinite loop
  • I didn’t add my python code as this would be the wrong place for solving those issues.
  • The script worked when I used python filename.py because there was nothing for the stdin to read.

If your file structure is so:

+module_1
|__module_1.1
|__module_1.2
|__module_2
+agi_script.py

and you have imported any of the modules, then while AGI/EAGI runs the file at root (agi_script.py) the imports just work.

You example code does not show any module usage examples. This also seems more a question about python not AGI/EAGI. Best guess, your picking up some environment variable from the CLI that lets you access the module, and environment variable which Asterisk does not provide.

You probably want to explicitly call a module as a class.

import module_1
import module_1/module_1.1

# your code

At the end, the documentation references pyst, which has not been maintained since 2013. But you should consider using pyst2, instead of reinventing the wheel.

-or-

At a minimum, look to pyst2 for code examples. pyst2 should solve most of your problems.

1 Like