简体   繁体   English

如何在Python中设置AGI变量?

[英]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) 我有下一个agi脚本test_agi.py:此脚本使检查称为数字是真的是数字或不使用HLR请求(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: 打电话后,我在agi调试中看到:

[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 Agi返回0。我要进行检查-agi必须返回变量'live_dst'的值,如果live_dst = Live,则要进行调用;如果live_dst!= Live,则要进行挂断电话

Use agi debug, you will see what your script do. 使用agi debug,您将看到脚本的作用。

Most likly asterisk just have no permission to script and script do nothing. 最幸运的星号只是没有脚本的权限,而脚本什么也不做。

To enable agi debug use 启用AGI调试使用

asterisk -r
agi set debug on

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 您将获得0,因为您没有从脚本中返回任何结果,因此需要将一个对象返回到拨号计划,如下所示

return response 返回响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM