计算FreeSWITCH通话时长,若超过10秒无人加入则播放IVR

2 投票
1 回答
1223 浏览
提问于 2025-04-17 21:35

如何计算通话的持续时间?

当用户加入3200后,如果等了10秒钟还没有其他人加入,那么我想播放一个音频文件。

但是,我该如何计算这个持续时间呢?我试过一些方法,但它们都不奏效,因为这些方法只在通话结束后才会触发。但我需要的是在通话开始时就开始计时。


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

现在,postprocessing.py 只在通话结束时运行。

1 个回答

1

你可以使用 sched_api 命令。

在你创建会议之前,可以执行类似下面的代码(这是一个JavaScript的例子):

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

countConferenceMembers.js 脚本中检查会议成员的数量。如果少于2个,就播放声音,然后把所有人挂断电话。

    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 命令 [https://freeswitch.org/confluence/display/FREESWITCH/mod_commands]

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

撰写回答