AGI script consume API

Hello!
I need to consume an API to check a number against a service from Google and based on the return, make a decision in the dialplan.

I tried calling PHP, Shell and even Python scripts via the AGI and System in the dialplan, but it doesn’t work. While the channel is “up”, if I check the AGISTATUS, it’s always “APPERROR”.

When I call the script from the terminal and inform the parameters, it works perfectly. When I call the script via dialplan, it doesn’t.

What am I missing?

Typical problems are file permissions (need read and execute for the user running Asterisk, also SELINUX could cause problems), different environments, and in particular different PATH settings, #! lines with relative file names for the interpreter.

Less likely is that you have run Asterisk in a change rooted environment.

Thanks david.
I’m running asterisk as root and the folders and files are set with the correct permissions. I’m not executing SELINUX and I don’t think there are PATH differences. I’ll show some parts of my dialplan and script. If I could at least check the errors somewhere, but I don’t think that asterisk or python are writing the errors in any file:

DIALPLAN:

same=>n,System(python3 /var/lib/asterisk/agi-bin/python/bv-vcall-python-command-line-examples/check_vcall_device_reachable_sample.py ${DESTINO} ${CANAL})

I tried with and without the python3 at the beggining, the result is the same.

SCRIPT:

from api_helper import ApiHelper

from client_library import businesscalls_v1_client as client
from client_library import businesscalls_v1_messages as messages

import argparse
import os

def get_Arguments():
  parser = argparse.ArgumentParser()
  parser.add_argument("number")
  parser.add_argument("channel")
  args = parser.parse_args()
  return args

def check_vcall_device_reachable(device_number: str):
  request = messages.GoogleCommunicationsBusinesscallsV1CheckVcallDeviceReachableRequest()
  request.deviceNumber = device_number
  return ApiHelper().get_api_client().CheckVcallDeviceReachable(request)

if __name__ == '__main__':
    args = get_Arguments()
    device_reachability = check_vcall_device_reachable(args.number)
    if 'UNREACHABLE' in str(device_reachability):
        os.system('asterisk -rx "dialplan set chanvar "'+args.channel+'" resultado UNREACHABLE"')
    else:
        os.system('asterisk -rx "dialplan set chanvar "'+args.channel+'" resultado REACHABLE"')

I’d start with a “hello world” script to make sure that the sucker gets called at all. You generally do not need a full path and your syntax is wrong. Parameters wish to be comma separated here. Your example would only work in case the variables contain them (nor sure about the space).

Thanks!
I’ve created a new script and started testing line by line.
The problem is inside the function, in the return line. When I put that line, I got the error.

#!/usr/bin/python3

from api_helper import ApiHelper

from client_library import businesscalls_v1_client as client
from client_library import businesscalls_v1_messages as messages

import argparse
import os

parser = argparse.ArgumentParser()
parser.add_argument("number")
parser.add_argument("channel")
args = parser.parse_args()

print ("Hello World " + args.number + " " + args.channel)
os.system('asterisk -rx "dialplan set chanvar "'+args.channel+'" resultado HELLO"')

def check_vcall_device_reachable(device_number: str):
    request = messages.GoogleCommunicationsBusinesscallsV1CheckVcallDeviceReachableRequest()
    request.deviceNumber = device_number
    return ApiHelper().get_api_client().CheckVcallDeviceReachable(request)
    
device_reachability = check_vcall_device_reachable(args.number)

This line:
return ApiHelper().get_api_client().CheckVcallDeviceReachable(request)

Perhaps the problem is when I try to call a function outside the script?

Try it to check environment errors:

sudo -u asterisk -c /usr/bin/python3 /var/lib/asterisk/agi-bin/python/bv-vcall-python-command-line-examples/check_vcall_device_reachable_sample.py ${DESTINO} ${CANAL}

Hey everyone, I found a solution.

At first, I was trying to achieve my goal using shell script and after a lot of testing with shell, PHP and Python, I found the error, fixed and now it’s working using shell script.

I export an environment variable at the system startup and when I execute it from the terminal, it works, but since the shell script runs in a separate environment, I needed to export the variable inside the script. Now it’s working just fine.

Thanks guys

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.