freeswitch获取手机号码状态

本脚本是用python写的,通过pythonESL模块连接freeswitch进行外呼并保存录音,然后再用百度语音识别进行录音文件识别

脚本内容如下:

#!/usr/bin/python3
from aip import AipSpeech
import os
import ESL
import time
import threading

#BaiDu APPID AK SK
APP_ID = '*********'
API_KEY = '*********'
SECRET_KEY = '*********'

#FS ip port passwd
fs_ip = '*********'
fs_port = '*********'
fs_passwd = '*********'

#外呼手机号
phone_file = '/root/call_number'
file_name = []
def file(file):
    f = open(file,'r')
    for i in f.readlines():
        _file = i.strip('\n')
        file_name.append(_file)

file(phone_file)

#录音存放路径
YMD_time = time.strftime("/%Y/%m/%d/", time.localtime())
record_path = '/backup/ceshi' + YMD_time

#通话状态
phone_state = ['空号','关机','停机','通话中','无法接通','号码有误','正忙','无人接听','可接通','无状态']

# 读取录音文件
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

#登陆FS socket
def sk(ip,port,passwd):
    conn = ESL.ESLconnection(ip, port, passwd)
    return conn

#录音文件识别
def file_write(call_number,phone_state):
    fw = open('/root/phone.xls','a')
    _str = '{0} {1}\n'.format(call_number,phone_state)
    fw.write(_str)
    fw.close()
def record_asr(call_number):
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    file_path = record_path + call_number+'.wav'
    a = client.asr(get_file_content(file_path), 'pcm', 8000, {'dev_pid': 1536,})
    if 'result' in a:
       _str = str(a['result'])
       for i in range(0,len(phone_state)):
           if _str.count(phone_state[i]) != 0:
              print (call_number,phone_state[i])
              file_write(call_number,phone_state[i])
              break
           elif phone_state[i] == '可接通':
                file_write(call_number,phone_state[i])
                break
    else:
        file_write(call_number,'无状态')
        
              
#读取esl事件
phone_list = []
def esl():
    conn = sk(fs_ip,fs_port,fs_passwd)
    if conn.connected:
       conn.events("plain", "CHANNEL_CALLSTATE")
       while 1:
          e = conn.recvEvent()
          call_uuid = e.getHeader('unique-id')
          call_state = e.getHeader('Channel-Call-State')
          call_number = e.getHeader('Caller-Destination-Number')
          if call_state == 'ACTIVE':
             uuid_kill = 'bgapi uuid_kill {0} &'.format(call_uuid)
             conn.send(uuid_kill)
             print ('-------已接通{0},自动挂断-------'.format(call_number))
          elif call_state == 'HANGUP':
             phone_list.append(call_number)

#遍历手机号并调用record_asr函数进行录音识别
def phone():
    while 1:
     if phone_list:
        for i in phone_list:
          print ('开始识别录音文件',record_path+i+'.wav')
          record_asr(i)
          phone_list.remove(i) 
          time.sleep(3)

#外呼电话
def call_phone():
    for i in range(0,len(file_name)):
        #连接FS
        conn = sk(fs_ip,fs_port,fs_passwd)
        #录音文件名
        record_file = record_path+file_name[i]+'.wav'
        time.sleep(5)
        if os.path.isdir(record_path) == False:
           os.makedirs(record_path)
        call = 'bgapi originate {1}sofia/gateway/sip_call/{0} &record({2})\r'.format(file_name[i],'{proxy_media=true,call_timeout=60}',record_file)
        print ('开始拨打',file_name[i])
        conn.send(call)
        #关闭连接
        conn.disconnect()

#创建线程
t1 = threading.Thread(target=call_phone)
t2 = threading.Thread(target=phone)
t3 = threading.Thread(target=esl)

#启动线程
t1.start()
t2.start()
t3.start()

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FreeSWITCH中,你可以使用Dialplan来配置号码变化前的路由。Dialplan是一种用于定义呼叫路由、转接和其它呼叫处理规则的配置语言。 要在号码变化前进行路由,可以使用`<extension>`标签来定义一个扩展,然后在其中配置相关的路由规则。 以下是一个示例的Dialplan配置,用于在号码变化前进行路由: ```xml <include> <extension name="pre_route"> <condition field="${destination_number}" expression="^(\d{4})$"> <action application="bridge" data="sofia/gateway/gateway1/$1"/> </condition> <condition field="${destination_number}" expression="^(\d{3})(\d{4})$"> <action application="bridge" data="sofia/gateway/gateway2/$1$2"/> </condition> </extension> </include> ``` 在上述示例中,我们定义了一个名为`pre_route`的扩展。在这个扩展中,我们使用正则表达式来匹配不同长度的目标号码。 第一个`<condition>`标签中的正则表达式`^(\d{4})$`匹配4位数字的目标号码。如果匹配成功,将执行`<action>`标签中的`bridge`应用程序,并将呼叫路由到名为`gateway1`的SIP网关,并将目标号码作为参数传递。 第二个`<condition>`标签中的正则表达式`^(\d{3})(\d{4})$`匹配7位数字的目标号码。如果匹配成功,同样会执行`<action>`标签中的`bridge`应用程序,并将呼叫路由到名为`gateway2`的SIP网关,并将目标号码作为参数传递。 你可以根据自己的需求和环境进行自定义配置,添加更多的条件和动作来实现你想要的号码变化前的路由逻辑。 需要注意的是,以上只是一个简单示例,实际配置可能会根据你的具体环境和需求而有所不同。建议参考FreeSWITCH的官方文档或相关资源以获取更详细和准确的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值