在 Freeswitch python 脚本中捕获 SIP“200 OK”

问题描述

我正在尝试在我的 freeswitch 服务器中使用“mod_python”运行 Python 脚本,以检查本地 REdis 数据库。到目前为止,我能够像这样从拨号计划运行脚本:

  <condition field="destination_number" expression="^(.*)$" break="on-true">
    <action application="log" data="ORIGIN : ${caller_id_number}"/>
    <action application="set" data="did=$1"/>
    <action application="log" data="DID = $1"/>
    <action application="python" data="test"/>
    <action application="bridge" data="sofia/external/$1@10.X.X.1"/>
  </condition>

python 脚本是这样的:

import redis
from datetime import timedelta
import freeswitch

def handler(session,args):
    did = session.getvariable("did")
    llave = "k"+did
    cliente = redis.Redis(host='localhost',port=6379,db=0)
    resultado = cliente.get(llave)
    if resultado is None:
        freeswitch.consoleLog("INFO","DID no esta en cache,agregando a la BD + TTL 10 minutos")
        cliente.set(llave,did)
        cliente.expire(llave,timedelta(minutes=20))
    else:
        ttl = cliente.ttl(llave)
        freeswitch.consoleLog("INFO","DID en cache,TTL= %s segs\n" % ttl)
        freeswitch.consoleLog("INFO","Colgando llamada")
        session.hangup("21")

一切正常。 我需要做的是“检查/捕获”如果呼叫被应答以在 REdis 数据库中进行更改。 到目前为止,我已经尝试在脚本中添加如下内容

if session.ready():
    // some work to do in REdis
else:
    // no changes in REdis DB

没有任何运气。 如果在python脚本中接听电话,有没有办法捕获?

解决方法

我认为您应该尝试在 python 脚本中调用“bridge”而不是 xml,例如:

dn = session:getVariable("destination_number")
session.execute("bridge","sofia/external/" + dn + "@10.X.X.1")
,

谢谢你的提示。我终于找到了更多使用 LUA 脚本来访问 freeswitch 状态和变量的方法......我解决了这个问题,写了一个小的 LUA 脚本......我正在寻找的命令是:

if session:answered() then

使用此命令,我能够捕获“200 OK”,以防呼叫被解析。 谢谢!