-1

I'm trying to run a Python script using "mod_python" in my freeswitch server in order to check a local REDIS db. So far i was able to run the script from the dialplan like this:

  <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>

The python script is something like this:

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")

It is working ok. What i need to do is "check/capture" if the call is answered to make changes in the REDIS DB. So far i have tried adding to the script something like this:

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

without any luck. Is there a way to capture if the call is answered in the python script?

May.D
  • 1,832
  • 1
  • 18
  • 34
Ricardo
  • 121
  • 1
  • 1
  • 6

3 Answers3

0

I think you should try invoke "bridge" in the python script instead of xml, like:

dn = session:getVariable("destination_number")
session.execute("bridge", "sofia/external/" + dn + "@10.X.X.1")
Lin Yu Cheng
  • 667
  • 9
  • 21
0

Thanks for the tip. I finally found more access to the freeswitch states and variables using LUA scripting.... i resolved this writing a small LUA script... the command i was looking for was :

if session:answered() then

with this command i was able to capture the "200 OK" in case a call was ansered. Thanks!

Ricardo
  • 121
  • 1
  • 1
  • 6
0

You should use application export with data nolocal:execute_on_answer=python script.

This way your python start after 200ok received.

Tom
  • 11
  • 2