freeswitch获取手机号码状态

匿名 (未验证) 提交于 2019-12-02 23:05:13

本脚本是用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() 

文章来源: https://blog.csdn.net/qq_31755183/article/details/86503430
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!