skip to Main Content

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/[email protected]"/>
  </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 segsn" % 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?

3

Answers


  1. Chosen as BEST ANSWER

    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!


  2. 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")
    
    Login or Signup to reply.
  3. You should use application export with data nolocal:execute_on_answer=python script.

    This way your python start after 200ok received.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search