Skip to main content
 首页 » 编程设计

python之计算自由切换通话的持续时间,如果超过 10 秒无人加入则播放 IVR

2025年05月04日8EasonJim

如何计算通话时长? 当一个用户加入 3200 并等待 10 秒并且没有人加入时,3200 然后我想播放音频文件。

但是请问我该如何计算持续时间?我试过跟随但它不起作用,因为它只在挂断电话后触发。但我需要在调用开始时启动计数器。


/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 在调用结束时运行

请您参考如下方法:

您可以使用sched_api 命令。

在您创建 session 之前,执行如下操作(这是 Javascript 示例):

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

在 countConferenceMembers.js 脚本中检查 session 成员数量。如果小于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`) 
    } 

自由切换命令 [ https://freeswitch.org/confluence/display/FREESWITCH/mod_commands]

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