SergeyMoroz
SergeyMoroz

Reputation: 137

How to set AGI variable in Python?

I have next agi script test_agi.py: This script make checking is called number is Really number or not using HLR request (API)

import urllib.request
import json
from pyagi.pyagi import AGI
agi = AGI()

dst = agi.env["agi_dnid"]

url = 'https://www.hlrlookup.com/api/hlr/?apikey=blabla&password=blabla&personalcache=5&msisdn=' + dst

req = urllib.request.Request(url)
r = urllib.request.urlopen(req).read()
cont = json.loads(r)


live = cont["error_text"]
agi.set_variable('live_dst',live)

And have extension in asterisk:

[HLR_check]
exten => _XXXXXX.,1,Set(fname=${UNIQUEID})
exten => _XXXXXX.,2,AGI(/root/hlr/test_agi.py)
exten => _XXXXXX.,3,NoOp(${live_dst})
exten => _XXXXXX.,4,GoToIf($["${live_dst}" != "Live"]?6:5)
exten => _XXXXXX.,5,GoTo(agiexten,${EXTEN},1)
exten => _XXXXXX.,6,Hangup(42)
same => n,Hangup(42)

After call i see in agi debug:

[May 18 09:02:17] VERBOSE[8194][C-00048437] netsock2.c:   == Using SIP RTP CoS mark 5
[May 18 09:02:17] VERBOSE[11945][C-00048437] pbx.c:     -- Executing [380633958833@HLR_check:1] Set("SIP/CC-000429e6", "fname=1495098137.288333") in new stack
[May 18 09:02:17] VERBOSE[11945][C-00048437] pbx.c:     -- Executing [380633958833@HLR_check:2] AGI("SIP/CC-000429e6", "/root/hlr/test_agi.py") in new stack
[May 18 09:02:17] VERBOSE[11945][C-00048437] res_agi.c:     -- Launched AGI Script /root/hlr/test_agi.py
[May 18 09:02:17] VERBOSE[11945][C-00048437] res_agi.c:     -- <SIP/CC-000429e6>AGI Script /root/hlr/test_agi.py completed, returning 0
[May 18 09:02:17] VERBOSE[11945][C-00048437] pbx.c:     -- Executing [380633958833@HLR_check:3] NoOp("SIP/CC-000429e6", "") in new stack

SO! Agi returned 0. I want make check - agi must return me value of variable 'live_dst' and i want make call if live_dst = Live and Hangup call if live_dst !=Live

Upvotes: 2

Views: 3404

Answers (2)

Fuad Fouad
Fuad Fouad

Reputation: 452

You are getting 0 because you are not returning any results from your script you need to return an object to the dialplan like this

return response

Upvotes: 0

arheops
arheops

Reputation: 15259

Use agi debug, you will see what your script do.

Most likly asterisk just have no permission to script and script do nothing.

To enable agi debug use

asterisk -r
agi set debug on

Upvotes: 0

Related Questions