2
votes

How to count the duration of call? When a user join 3200 and wait for 10 seconds and nobody yet joined, 3200 then i want to play audio file.

But how do i count the duration any idea please? I have tried following but its not working cause it only triggers after the call hang up. but i need the counter starts when call was started.


/usr/local/freeswitch/script/wait.py

import os
from freeswitch import *
def hangup_hook(session, what):
    consoleLog("info","hangup hook for %s!!\n\n" % what)
    return
def input_callback(session, what, obj):
    if (what == "dtmf"):
        consoleLog("info", what + " " + obj.digit + "\n")
    else:
        consoleLog("info", what + " " + obj.serialize() + "\n")
    return "pause"

def handler(session, args):
    new_api_obj = API()
    new_api_obj.executeString("pyrun postprocessing " + session.getVariable('caller_id_number'))
    session.answer()
    session.setHangupHook(hangup_hook)
    session.setInputCallback(input_callback)
    session.execute("conference", "$1-${domain_name}@ultrawideband")
    session.hangup()

/usr/local/freeswitch/script/postprocessing.py

import os, sys, time
from freeswitch import *
def runtime(arg1):
    time.sleep(10)
    # is there 2 person or 1 person?
    # if 1 person after 10 second play 
    #session.streamFile("/var/tmp/ivr/sara4.wav")
    # if 2 person after 10 second do nothing
    consoleLog( "info", "Caller: %s hung up 10s ago!\n" % arg1 )

Now the postprocessing.py is running when the call end

1

1 Answers

1
votes

You can use sched_api command.

Before you create conference, execute something like this (this is Javascript example):

    const conferenceName = "test@conference";
    apiExecute("sched_api", `+10 none jsrun /etc/freeswitch/countConferenceMembers.js 
    ${conferenceName }`);

In countConferenceMembers.js script check conference member count. If is less than 2, play sound and then hangup everybody.

    const conferenceName = argv[0];
    const count = apiExecute("conference", `${conferenceName } count`);
    if (Number(count) < 2) { 
        apiExecute("conference", `${conferenceName} play SOUND_FILE_PATH`)
        apiExecute("conference", `${conferenceName} hup all`)
    }

Freeswitch commands [https://freeswitch.org/confluence/display/FREESWITCH/mod_commands]

Freeswitch mod_conference [https://freeswitch.org/confluence/display/FREESWITCH/mod_conference]