Using Python for AGI scripting

Hi

I am new to FreePBX and I am looking to create some automation on inbound routes.
I have written a Python script that does a REST lookup into our service management software and extracts an extension number based on the incoming caller ID, of a queue for the team that services the customer. The script works, but I was not sure how to integrate it into the system. It looks like I need to use AGI for this, but I cannot see any way to add the agi module to Python. I am using the Freepbx ISO which has Python installed, yet there is no Asterisk module, or AGI module and I can’t seem to see a way to get it installed. I could then use the get and set variable functions to parse the caller ID in and the destination number back.

Hoping someone can help.

Cheers
Mark Dutton

Welcome to the forum!

Some background reading for you:

Yes thanks for this. I read this and figured this was the way. I struggle with Perl and PHP, but I can get around Python.

I have written a script in python and tested it. The script needs to make a few successive rest calls and parse the json, so it needs to be done externally. It looks like there are modules for python to import for AGI, but they don’t seem to exist on the native Freepbx ISO. I installed pip, and from there pyst, but I still didn’t seem to get the AGI functions to read and write Asterisk variables. I’ll reread again tomorrow and see if I can figure it out. The script takes the caller ID, does some rest calls and returns an extension number to forward the call to, so I think I have done the hard part.

Cheers

Mark

yum install perl-CPAN is where I would start.

Then within CPAN shell, install Asterisk::AGI - Simple Asterisk Gateway Interface Class - metacpan.org

Sorry, I had a brain fart. I meant python, not perl. I have edited the OP. Can’t believe I did that.

I took another look this morning. I used the PHP example to simply grab the caller ID and return it. I figure if I get this working, I can either figure out how to do rest calls and json extraction in PHP, or simply call my Python script from within the PHP script.

I’ll see if I can do the REST calls and JSON directly for neatness and learn a new programming language as well.

Thanks for the help!

I think you can do what you want in the dialplan directly, without AGI. Asterisk has curl and JSON parsing functions available.

Take a look at CURL, CURLOPT, and JSON_DECODE : Dialplan Functions - Asterisk Documentation

Here’s a small sample written by @lgaetz : Asterisk JSON_DECODE function

Yes I saw this in the docos. However there is some branching logic required. I have to do a lookup first on contact. If there is no return, I have to search company. On either of these, I get a company id. I then need to search the territory record based on the company, to pull out the appropriate target queue number.

I think this needs to be scripted. I have it working with PHP calling python as I can’t seem to get an AGI module for python, but I am now learning PHP syntax and writing the script entirely in PHP.

Oh, you underestimate the power and gnarliness of Asterisk Dialplan… :slight_smile:

2 Likes

Well I knuckled down and started to get ofay with PHP. It wasn’t too tricky and I have a tidy script to do what I want and I have begun to learn a new programming language.
Very happy with the outcome. Thanks again all.

2 Likes