bot.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. #!/usr/bin/env python3
  2. # encoding:utf-8
  3. import ctypes
  4. import os
  5. import time
  6. import json
  7. import wave
  8. import queue
  9. import threading
  10. import traceback
  11. import sys
  12. import pjsua2 as pj
  13. from enum import Enum
  14. from datetime import datetime
  15. from multiprocessing import Process
  16. from src.core.callcenter import registry
  17. from src.core.callcenter.cache import Cache
  18. from src.core.datasource import SIP_SERVER, SERVE_HOST
  19. from src.core.voip.constant import *
  20. import requests
  21. from src.core.callcenter.api import BotChatRequest,ChatMessage
  22. from src.core import singleton_keys
  23. from src.core.callcenter.snowflake import Snowflake
  24. from src.core.callcenter.data_handler import *
  25. calls = {}
  26. # recording_file = '/code/src/core/voip/incoming_call.wav'
  27. # player_file1 = '/code/src/core/voip/test111.wav'
  28. # player_file2 = '/code/src/core/voip/test222.wav'
  29. class BotStatus(Enum):
  30. # 等待用户讲话,未超时
  31. wait_speaking_not_timeout = 1
  32. # 等待用户讲话,已超时但用户仍在持续讲话
  33. wait_speaking_timeout_user_speaking = 2
  34. # 机器人讲话过程中
  35. speaking = 3
  36. # 等待用户讲话结束
  37. wait_user_speaking_end = 4
  38. # 等待大模型输出
  39. wait_gpt_return = 5
  40. class UserStatus(Enum):
  41. # ASR返回值为sentenceBegin后将状态置为Speaking
  42. speaking = 1
  43. # ASR返回值为sentenceEnd后,将状态置为Silence
  44. silence = 2
  45. class MyAudioMediaPort(pj.AudioMediaPort):
  46. def __init__(self, call, audio_media=None, asr=None):
  47. pj.AudioMediaPort.__init__(self)
  48. # # 打开一个 .pcm 文件来保存音频流(可选:保存为 .wav)
  49. # self.wav = wave.open(f"{recording_file}", "wb")
  50. # self.wav.setnchannels(1)
  51. # self.wav.setsampwidth(2) # 假设每个样本是 16 位(2 字节)
  52. # self.wav.setframerate(16000)
  53. print("MyAudioMediaPort::come in, debugger")
  54. self.call = call
  55. self.audio_media = audio_media
  56. self.asr = asr
  57. self.first = True
  58. self.user_asr_texts = []
  59. def onFrameRequested(self, frame):
  60. print("Request audio frame:", frame)
  61. def onFrameReceived(self, frame):
  62. # self.wav.writeframes(bytes(frame.buf))
  63. if self.asr: # 如果ASR实例存在,则发送音频数据
  64. if self.first:
  65. self.first = False
  66. self.call.logger.info("Received audio frame: %s %s", frame.buf, frame.size)
  67. self.asr.send_audio(frame.buf)
  68. try:
  69. asr_text = self.get_asr_text()
  70. play_complete = self.call.is_play_complete()
  71. current_time = time.time() # 实时当前时间
  72. if self.call.inputType == '1.0':
  73. time_difference = int(current_time - self.call.inputLongStart)
  74. # print('current_time - self.call.inputLongStart:',time_difference > 35, self.call.txtLock , play_complete)
  75. if time_difference > 35 and play_complete:
  76. self.user_asr_texts.append(f"DTMF({self.call.digit})DTMF")
  77. user_asr_text = self.user_asr_texts[0] if len(self.user_asr_texts) == 1 else '###'.join(self.user_asr_texts)
  78. self.user_asr_texts.clear()
  79. self.call.chat(user_asr_text)
  80. # print("测试超长", user_asr_text)
  81. elif asr_text:
  82. self.user_asr_texts.append(asr_text)
  83. if time_difference > int(self.call.wait_time):
  84. self.call.reset_wait_time()
  85. else:
  86. if asr_text and not play_complete:
  87. self.user_asr_texts.append(asr_text)
  88. if (asr_text and play_complete) or (play_complete and self.user_asr_texts):
  89. if asr_text:
  90. self.user_asr_texts.append(asr_text)
  91. user_asr_text = self.user_asr_texts[0] if len(self.user_asr_texts) == 1 else '###'.join(self.user_asr_texts)
  92. self.user_asr_texts.clear()
  93. self.call.chat(user_asr_text)
  94. if self.call.wait_time and self.call.wait_time != "0" and play_complete and not asr_text:
  95. self.call.wait_time_check(current_time, self.call.wait_time)
  96. message_queue_size = self.call.message_queue.qsize()
  97. if (message_queue_size > 0 and not self.call.cur_player_file) or (message_queue_size > 0 and play_complete):
  98. # self.call.logger.info('onFrameReceived:message_queue_size=', message_queue_size, 'play_complete=', play_complete, asr_text)
  99. self.call.cur_player_file, self.call.wait_time, self.call.inputType,self.call.action, self.call.node_id = self.get_player_file()
  100. # 重置播放完成标志和超时计时器,确保新的播放从头开始计时
  101. self.call.reset_wait_time()
  102. self.call.send_bot_speaker(self.call.cur_player_file)
  103. except:
  104. pass
  105. def get_asr_text(self):
  106. try:
  107. asr_text = self.call.user_asr_text_queue.get(block=False)
  108. self.call.logger.info('get_asr_text: %s', asr_text)
  109. return asr_text
  110. except:
  111. pass
  112. def get_player_file(self):
  113. try:
  114. message = self.call.message_queue.get(block=False)
  115. player_file = [item.voice_url for item in message.contents if item.content_type == 'voice']
  116. return player_file, message.wait_time, message.inputType, message.action, message.node_id
  117. except Exception as e:
  118. traceback.print_exc()
  119. class MyAudioMediaPlayer(pj.AudioMediaPlayer):
  120. def __init__(self, player_id, sink, on_complete=None):
  121. pj.AudioMediaPlayer.__init__(self)
  122. self.player_id = player_id
  123. self.sink = sink
  124. self.on_complete = on_complete
  125. def onEof2(self):
  126. # self.stopTransmit(self.sink)
  127. if self.on_complete:
  128. self.on_complete(self.player_id)
  129. # Subclass to extend the Account and get notifications etc.
  130. class Account(pj.Account):
  131. def __init__(self, agent, user_part, **kwargs):
  132. pj.Account.__init__(self)
  133. self.agent = agent
  134. self.user_part = user_part
  135. self.calls = {}
  136. self.kwargs = kwargs
  137. def onRegState(self, prm):
  138. ai = self.getInfo()
  139. print("***OnRegState: " + prm.reason, ai.id,ai.isDefault,ai.uri,ai.regIsConfigured,ai.regIsActive,ai.regExpiresSec,ai.regStatus,ai.regStatusText,ai.regLastErr,ai.onlineStatus,ai.onlineStatusText)
  140. def onIncomingCall(self, prm):
  141. self.agent.logger.info("daviddebugger::onIncomingCall::%s ", prm.callId)
  142. ai = self.getInfo()
  143. print("***onIncomingCall: ", prm.callId, ai.id,ai.isDefault,ai.uri,ai.regIsConfigured,ai.regIsActive,ai.regExpiresSec,ai.regStatus,ai.regStatusText,ai.regLastErr,ai.onlineStatus,ai.onlineStatusText)
  144. call = MyCall(self.agent, self, self.user_part, prm.callId, **self.kwargs)
  145. call_op_param = pj.CallOpParam(True)
  146. call_op_param.statusCode = pj.PJSIP_SC_OK
  147. call.answer(call_op_param)
  148. self.calls[prm.callId] = call
  149. class MyCall(pj.Call):
  150. def __init__(self, agent, acc, user_part, call_id, **kwargs):
  151. pj.Call.__init__(self, acc, call_id)
  152. self.agent = agent
  153. self.logger = agent.logger
  154. self.user_part = user_part
  155. self.call_id = call_id
  156. self.kwargs = kwargs
  157. self.audio_media = None
  158. self.audio_port = None
  159. self.audio_player = None
  160. self.asr = None
  161. self.session_id = kwargs.get('variable_sip_h_P-LIBRA-CallId')
  162. self.device_id = kwargs.get('variable_sip_h_P-LIBRA-DeviceId')
  163. self.call_phone = kwargs.get("Caller-Caller-ID-Number")
  164. self.logger.info("daviddebugger::self.session_id:%s, self.call_phone:%s", self.session_id,self.call_phone)
  165. self.taskId = "10001"
  166. # self.scripts = build_demo_script()
  167. self.user_asr_text_queue = queue.Queue(maxsize=100)
  168. self.message_queue = queue.Queue(maxsize=3)
  169. self.player_complete_dict = {}
  170. self.wait_time = None
  171. self.inputType = None #记录按键类型 1为长按键类型
  172. self.digit = '' # 存储长按键内容
  173. self.action = None
  174. self.node_id= 'start'
  175. self.cur_player_file = None #当前播放的文件
  176. from src.core.voip.asr import TestSt
  177. self.asr = TestSt(call_id, message_receiver=self.on_receiver_asr_result) # 创建ASR实例
  178. self.asr.start() # 启动ASR线程
  179. self.start_time = time.time() # 当前机器人对话开始时间
  180. # 超时设置
  181. self.play_start_time = time.time() # 倒计时开始时间
  182. self.play_complete_flag = False # 倒计时开始标志
  183. self.txtLock = False
  184. self.inputLongStart = time.time() #长按键开始时间
  185. self.inter_action_total = 0
  186. self.statistics_lock = False
  187. def wait_time_check(self, current_time, wait_time):
  188. try:
  189. # 确保 wait_time 是整数类型
  190. wait_time = int(wait_time)
  191. # 如果播放尚未完成,重置标志并返回
  192. if not self.play_complete_flag:
  193. self.play_complete_flag = True
  194. self.play_start_time = current_time
  195. # print(f"开始计时: {self.play_start_time}")
  196. elapsed_time = int(current_time - self.play_start_time)
  197. # print(f"当前时间: {current_time}, 已过时间: {elapsed_time}, 最大等待时间: {wait_time}")
  198. if elapsed_time > wait_time:
  199. # self.user_asr_text_queue.put("ASR408error")
  200. self.chat('ASR408error')
  201. registry.BOT_ASR_408.labels(self.taskId).inc()
  202. except ValueError as e:
  203. self.logger.info(f"无效的等待时间参数: {wait_time}, 错误: {e}")
  204. self.reset_wait_time()
  205. def reset_wait_time(self):
  206. self.play_complete_flag = False # 重置播放完成标志
  207. self.play_start_time = None # 重置开始计时时间
  208. def is_play_complete(self): #语音机器人是否播放结束
  209. if self.cur_player_file:
  210. player_id = murmur3_32(self.cur_player_file)
  211. return self.player_complete_dict.get(player_id)
  212. def onDtmfDigit(self, prm):
  213. # 判断是否播放完成 否则不记录用户说的内容
  214. if not self.is_play_complete():
  215. return
  216. digit = prm.digit
  217. self.reset_wait_time()
  218. # 假设为超长类型按键 把用户输入的按键进行拼接 如果为# 则把用户输入所有按键放入队列并发送文本机器人
  219. # 如果为非正常按键服务 输入以后直接发送文本机器人
  220. if self.inputType == '1.0':
  221. if digit != '#':
  222. self.digit += digit
  223. elif digit == '#':
  224. # self.user_asr_text_queue.put(f"DTMF({self.digit})DTMF")
  225. self.chat(f"DTMF({self.digit})DTMF")
  226. else:
  227. self.user_asr_text_queue.put(f"DTMF({digit})DTMF")
  228. def onCallState(self, prm):
  229. call_info = self.getInfo()
  230. # pj.PJSIP_INV_STATE_NULL
  231. # pj.PJSIP_INV_STATE_CALLING
  232. # pj.PJSIP_INV_STATE_INCOMING
  233. # pj.PJSIP_INV_STATE_EARLY
  234. # pj.PJSIP_INV_STATE_CONNECTING
  235. # pj.PJSIP_INV_STATE_CONFIRMED
  236. # pj.PJSIP_INV_STATE_DISCONNECTED
  237. self.logger.info("daviddebugger::onCallState::[%s,%s] %s", pj.PJSIP_INV_STATE_CONFIRMED, pj.PJSIP_INV_STATE_DISCONNECTED, call_info.state)
  238. if call_info.state == pj.PJSIP_INV_STATE_CONFIRMED:
  239. # 当呼叫状态为已确认(即接通)
  240. self.bot_say_hello()
  241. if call_info.state == pj.PJSIP_INV_STATE_DISCONNECTED:
  242. self.logger.info("通话结束:%s", self.user_part)
  243. self.release()
  244. def onCallMediaState(self, prm):
  245. call_info = self.getInfo()
  246. # print("Call Media state: ", call_info.stateText)
  247. for media in call_info.media:
  248. if media.type == pj.PJMEDIA_TYPE_AUDIO and \
  249. (media.status == pj.PJSUA_CALL_MEDIA_ACTIVE):
  250. self.logger.info("Call Media state 111: %s", call_info.stateText)
  251. self.audio_media = self.getAudioMedia(media.index)
  252. try:
  253. # 建立双向通道
  254. self.receive_user_speaker()
  255. # self.bot_say_hello()
  256. except Exception as e:
  257. traceback.print_exc()
  258. def receive_user_speaker(self):
  259. self.audio_port = MyAudioMediaPort(self, self.audio_media, self.asr)
  260. self.audio_port.createPort("Incoming Call Port", build_audio_format())
  261. self.audio_media.startTransmit(self.audio_port)
  262. def send_bot_speaker(self, player_file):
  263. if not player_file :
  264. return
  265. if not self.isActive():
  266. return
  267. player_id = murmur3_32(player_file)
  268. self.player_complete_dict[player_id] = False
  269. for f in player_file:
  270. if not os.path.isfile(f):
  271. self.logger.info(f"Sending bot speaker, not exists, player_file: {player_file}, player_id: {player_id}, isActive: {self.isActive()}")
  272. return
  273. key = murmur3_32(str(datetime.now().timestamp()))
  274. self.agent.call_players[key] = [datetime.now().timestamp()]
  275. # print('self.player_complete_dict[player_id]D:', player_id, player_file, self.player_complete_dict[player_id])
  276. self.logger.info(f"Sending bot speaker, 111, player_file: {player_file}, player_id: {player_id}, isActive: {self.isActive()}")
  277. self.audio_player = MyAudioMediaPlayer(player_id, self.audio_media, on_complete=self.on_media_player_complete)
  278. if len(player_file) == 1:
  279. self.logger.info(f"Sending bot speaker, 222, player_file: {player_file}, player_id: {player_id}, isActive: {self.isActive()}")
  280. self.audio_player.createPlayer(player_file[0], pj.PJMEDIA_FILE_NO_LOOP)
  281. else:
  282. self.logger.info(f"Sending bot speaker, 333, player_file: {player_file}, player_id: {player_id}, isActive: {self.isActive()}")
  283. self.audio_player.createPlaylist(player_file, f'my_hello_playlist{player_id}', pj.PJMEDIA_FILE_NO_LOOP)
  284. self.logger.info(f"Sending bot speaker, 444, player_file: {player_file}, player_id: {player_id}, isActive: {self.isActive()}")
  285. self.audio_player.startTransmit(self.audio_media)
  286. self.agent.call_players[key].append(datetime.now().timestamp())
  287. def on_receiver_asr_result(self, message, *args):
  288. # 判断是否播放完成 否则不记录用户说的内容
  289. if not self.is_play_complete():
  290. return
  291. message = json.loads(message)
  292. if message["header"]["status"] == 20000000:
  293. if message["header"]["name"] == "SentenceEnd":
  294. result = message["payload"]["result"]
  295. # self.logger.info("asr返回内容Result:%s", result)
  296. self.user_asr_text_queue.put(result)
  297. elif message["header"]["name"] == "TranscriptionResultChanged":
  298. self.reset_wait_time()
  299. else:
  300. self.logger.info(f"Status is not {message['header']['status']}")
  301. registry.ASR_ERRORS.labels(message['header']['status']).inc()
  302. def on_media_player_complete(self, player_id):
  303. self.logger.info('player complete')
  304. self.player_complete_dict[player_id] = True
  305. self.digit = ''
  306. self.inputLongStart = time.time()
  307. #播放完毕执行的动作
  308. self.say_end_action(self.action)
  309. def bot_say_hello(self):
  310. self.chat(user_asr_text="start")
  311. def chat(self, user_asr_text=None):
  312. # 调用文本机器人接口
  313. ToTextBotAgent(user_asr_text,self)
  314. def say_end_action(self, action):
  315. self.logger.info('handling_release %s', action.action_code)
  316. action_code = action.action_code
  317. if action_code == 'hang': # 挂断
  318. self.agent.hangup(self.user_part)
  319. self.end_statistics()
  320. elif action_code == 'transfer': # 转人工
  321. self.agent.transfer(user_part=self.user_part, call_id=self.session_id, device_id=self.device_id)
  322. self.end_statistics()
  323. #更新通话记录
  324. self.agent.dataHandleServer.update_record(self.session_id, service_category=2)
  325. def end_statistics(self):
  326. if not self.statistics_lock:
  327. self.statistics_lock = True
  328. self.logger.info(f"self.inter_action_total:{self.inter_action_total}")
  329. latency = (time.time() - self.start_time)
  330. registry.BOT_CALL_DURATION.labels(self.taskId).observe(latency)
  331. registry.BOT_INTERACTION_ROUNDS.labels(self.taskId).observe(self.inter_action_total)
  332. def release(self):
  333. self.logger.info('liuwei::debugger::release:: come in ')
  334. if self.audio_player:
  335. try:
  336. self.audio_player.stopTransmit(self.audio_media)
  337. print("Success to stopTransmit:")
  338. except pj.Error as e:
  339. print("Failed to stopTransmit:", e)
  340. self.audio_player = None # 或调用播放器停止方法
  341. if self.audio_port:
  342. try:
  343. self.audio_media.stopTransmit(self.audio_port)
  344. print("Success to stopTransmit:")
  345. except pj.Error as e:
  346. print("Failed to stopTransmit:", e)
  347. self.audio_port = None # 或调用相关销毁方法
  348. if self.audio_media:
  349. # self.audio_media.stopTransmit()
  350. self.audio_media = None
  351. self.asr.close()
  352. # 远程挂机之后要将分机号回收
  353. self.agent.hangup(self.user_part)
  354. # self.agent.release(self.user_part)
  355. self.end_statistics()
  356. class ToTextBotAgent:
  357. def __init__(self, user_asr_text, call_agent):
  358. if not user_asr_text or (call_agent.action and call_agent.action.action_code != 'normal'):
  359. # print("ASR文本为空,终止执行。")
  360. return
  361. self.call_agent = call_agent
  362. self.request_data = BotChatRequest(
  363. nodeId=self.call_agent.node_id,
  364. userId=self.call_agent.call_phone,
  365. sessionId= self.call_agent.session_id,
  366. recordId="",
  367. taskId=self.call_agent.taskId,
  368. asrText=user_asr_text,
  369. ext= None
  370. )
  371. self.call_agent.logger.info("user_asr_text发送结果: %s", user_asr_text)
  372. if user_asr_text != 'ASR408error':
  373. self.call_agent.inter_action_total += 1
  374. # 发送请求并处理响应
  375. # self.test_request(self.request_data)
  376. self.to_quest(self.request_data)
  377. def to_quest(self, request: BotChatRequest, try_count = 3):
  378. request_data = request.to_json_string()
  379. url = f"http://{SERVE_HOST}:40072/botservice"
  380. start_time = time.time()
  381. self.call_agent.logger.info(f"请求数据:{request_data},url:{url}")
  382. with requests.Session() as session:
  383. session.headers.update({'Content-Type': 'application/json'})
  384. response = None
  385. while try_count > 0:
  386. try:
  387. try_count = try_count - 1
  388. response = session.post(url=url, json=json.loads(request_data), timeout=.5)
  389. # response = requests.post(url=url, json=json.loads(request_data), headers=headers, timeout=10) # 使用占位URL
  390. self.call_agent.logger.info(f"sessionId={ self.call_agent.session_id}, chat::request:{request_data}, response:{response.text}")
  391. if response.status_code == 200:
  392. response_data = response.json()
  393. if "data" in response_data and response_data["code"]==0:
  394. data = response_data["data"]
  395. parsed_response = ChatMessage.from_json(data)
  396. self.call_agent.message_queue.put(parsed_response)
  397. else:
  398. self.call_agent.logger.info("响应中没有 'data' 字段")
  399. else:
  400. self.call_agent.logger.info(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
  401. break
  402. except requests.RequestException as e:
  403. traceback.print_exc()
  404. self.call_agent.logger.error(f"TaskId={request.taskId}, 请求发生异常: {e}, URL: {url}")
  405. if not response or response.status_code != 200:
  406. self.call_agent.message_queue.put(self.get_default_response())
  407. latency = (time.time() - start_time)
  408. registry.BOT_REQUEST_COUNT.inc()
  409. registry.BOT_REQUEST_LATENCY.labels(request.taskId).observe(latency)
  410. session.close()
  411. def get_default_response(self):
  412. response = {
  413. "node_id": "100.01",
  414. "contents": [
  415. {
  416. "content_type": "voice",
  417. "content": "感谢您的来电,祝您生活愉快,再见!",
  418. "voice_url": "/root/aibot/dm/voice/buy.wav",
  419. "voice_content": ""
  420. }
  421. ],
  422. "wait_time": 1,
  423. "action": {
  424. "action_code": "hang",
  425. "action_content": "机器人挂断"
  426. },
  427. "inputType": "0"
  428. }
  429. parsed_response = ChatMessage.from_json(response)
  430. return parsed_response
  431. # 模拟接口请求返回
  432. def test_request(self, params: BotChatRequest):
  433. self.call_agent.logger.info("test_request::params= %s", params)
  434. response_data = {
  435. "node_id": "1.0",
  436. "contents": [],
  437. "wait_time": "6",
  438. "action": {
  439. "action_code": "normal",
  440. "action_content": "正常通话"
  441. },
  442. "inputType": "0"
  443. }
  444. self.call_agent.logger.info("asrText: %s", params.asrText)
  445. if params.asrText == 'start': #欢迎语
  446. response_data['contents'].append({
  447. "content_type": "voice",
  448. "content": "",
  449. "voice_url": '/code/src/core/voip/scripts/1_00.wav',
  450. "voice_content": "五一北京到上海的高铁票还有吗?"
  451. })
  452. response_data['inputType'] = '1.0'
  453. elif params.asrText == 'ASR408error': #超时执行
  454. response_data['contents'].append({
  455. "content_type": "voice",
  456. "content": "",
  457. "voice_url": '/code/src/core/voip/scripts/4_00.wav',
  458. "voice_content": "waitTime超时"
  459. })
  460. elif "DTMF" in params.asrText and self.call_agent.inputType =='1.0': #长按键超30s执行
  461. response_data['contents'].append({
  462. "content_type": "voice",
  463. "content": "",
  464. "voice_url": '/code/src/core/voip/scripts/2_00.wav',
  465. "voice_content": "sds"
  466. })
  467. else :
  468. response_data['contents'] = [
  469. {
  470. "content_type": "voice",
  471. "content": "",
  472. "voice_url": '/code/src/core/voip/test111.wav',
  473. "voice_content": "测试第二个录音文件"
  474. },
  475. {
  476. "content_type": "voice",
  477. "content": "",
  478. "voice_url": '/code/src/core/voip/test222.wav',
  479. "voice_content": "五一北京到上海的高铁票还有吗?"
  480. }
  481. ]
  482. try:
  483. parsed_response = ChatMessage.from_json(response_data)
  484. self.call_agent.message_queue.put(parsed_response)
  485. except Exception as e:
  486. traceback.print_exc() # 打印完整的错误信息
  487. return None
  488. @singleton_keys
  489. class BotAgent:
  490. def __init__(self, app, user_part_range=range(1001, 1011), host=SIP_SERVER, port="5060", password="slibra@#123456"):
  491. self.app = app
  492. self.logger = app.logger
  493. self.user_part_range, self.host, self.port, self.password = user_part_range, host, port, password
  494. self.user_part_pool = queue.Queue(maxsize=len(user_part_range))
  495. self.accounts = {}
  496. self.calls = {}
  497. self.call_players={}
  498. self.ep = pj.Endpoint()
  499. self.daemon_stopping = False
  500. self.is_stopping = False
  501. self.counter = 0
  502. self.acd_service = None
  503. self.cache = Cache(app)
  504. self.dataHandleServer = DataHandleServer(app)
  505. self.pjsua_thread = None
  506. self.start()
  507. threading.Thread(target=self.main_thread_daemon).start()
  508. def create_pjsua2(self, timeout_sec=86400):
  509. start_time = time.time()
  510. try:
  511. self.cache.set_register_per_hours(expire=timeout_sec - (60*3))
  512. # Create and initialize the library
  513. ep_cfg = pj.EpConfig()
  514. ep_cfg.uaConfig.threadCnt = 1
  515. ep_cfg.uaConfig.mainThreadOnly = False
  516. ep_cfg.uaConfig.maxCalls = 32
  517. ep_cfg.uaConfig.maxAccounts = 32
  518. ep_cfg.medConfig.noVad = True
  519. ep_cfg.logConfig.level = 3
  520. ep_cfg.logConfig.consoleLevel = 3
  521. self.ep.libCreate()
  522. self.ep.libInit(ep_cfg)
  523. aud_dev_mgr = self.ep.audDevManager()
  524. aud_dev_mgr.setNullDev() # 使用虚拟音频设备(如果没有实际设备)
  525. # Set up media configuration, particularly jitter buffer
  526. media_cfg = pj.MediaConfig()
  527. media_cfg.jbMinPre = 4 # Minimum pre-fetch frames
  528. media_cfg.jbMaxPre = 16 # Maximum pre-fetch frames
  529. media_cfg.noVad = True # Disable Voice Activity Detection if needed
  530. self.ep.medConfig = media_cfg # Apply media config to endpoint
  531. # Create SIP transport. Error handling sample is shown
  532. sipTpConfig = pj.TransportConfig()
  533. sipTpConfig.port = 30506
  534. self.ep.transportCreate(pj.PJSIP_TRANSPORT_UDP, sipTpConfig)
  535. # Start the library
  536. self.ep.libStart()
  537. for user_part in self.user_part_range:
  538. acfg = pj.AccountConfig()
  539. acfg.idUri = f"sip:{user_part}@{self.host}:{self.port}"
  540. acfg.regConfig.registrarUri = f"sip:{self.host}:{self.port}"
  541. cred = pj.AuthCredInfo("digest", "*", f"{user_part}", 0, self.password)
  542. acfg.sipConfig.authCreds.append(cred)
  543. acfg.regConfig.timeoutSec = timeout_sec # 注册超时时间(秒)
  544. acfg.regConfig.retryIntervalSec = 10 # 重试间隔时间(秒)
  545. acfg.regConfig.firstRetryIntervalSec = 10 # 首次重试间隔时间(秒)
  546. acfg.natConfig.iceEnabled = True
  547. acfg.natConfig.turnEnabled = True
  548. acfg.natConfig.turnServer = f"stun:{self.host}:3478"
  549. # acfg.natConfig.turnUsername = "username"
  550. # acfg.natConfig.turnPassword = "password"
  551. # Create the account
  552. acc = Account(self, user_part=user_part)
  553. acc.create(acfg)
  554. self.user_part_pool.put(user_part)
  555. self.accounts[user_part] = acc
  556. except:
  557. traceback.print_exc()
  558. finally:
  559. latency = time.time() - start_time
  560. registry.BOT_CREATE_ACCOUNT_LATENCY.observe(latency)
  561. self.logger.info("create pjsua latency: %.3fs", latency)
  562. while not self.is_stopping:
  563. registry.BOT_AGENT_LIVES.set(self.user_part_pool.qsize())
  564. self.ep.libHandleEvents(100)
  565. # Destroy the library
  566. self.ep.libDestroy()
  567. def main_thread_daemon(self):
  568. while not self.daemon_stopping:
  569. _lock = self._play_complete_degree_check()
  570. if _lock:
  571. self.logger.error("daviddebugger::play time greater than 60s, will restart")
  572. self.restart()
  573. continue
  574. _lock = self.cache.get_pjsua_thread_lock()
  575. if _lock:
  576. self.cache.del_pjsua_thread_lock()
  577. self.logger.error("daviddebugger::thread is lock, will restart")
  578. self.restart()
  579. continue
  580. _lock = self.cache.lock_register_per_hours()
  581. if not _lock and len(self.accounts) == len(self.user_part_range):
  582. self.logger.error("daviddebugger::register expire, will restart")
  583. self.restart()
  584. continue
  585. time.sleep(0.1)
  586. def _play_complete_degree_check(self):
  587. for k, v in list(self.call_players.items()):
  588. if len(v) == 2:
  589. self.call_players.pop(k)
  590. continue
  591. if len(v) == 1:
  592. sec = datetime.now().timestamp() - v[0]
  593. if sec > 90:
  594. return True
  595. return False
  596. def transfer(self, user_part, call_id, device_id, service_id='00000000000000000'):
  597. if self.acd_service:
  598. self.acd_service.transfer_to_agent(call_id, device_id, service_id, hold=True)
  599. # sip_headers = {'P-LIBRA-HangUpReason': 'transferToAgent', 'P-LIBRA-ServiceId': service_id}
  600. try_count = 30
  601. while try_count >0:
  602. if self.cache.get_after_play_hold_music(call_id):
  603. self.hangup(user_part)
  604. break
  605. try_count = try_count - 1
  606. time.sleep(0.1)
  607. def hangup(self, user_part, reason="NORMAL_CLEARING", **sip_headers):
  608. call_op_param = pj.CallOpParam(True)
  609. call_op_param.statusCode = pj.PJSIP_SC_OK
  610. call_op_param.reason = reason
  611. call_op_param.txOption = pj.SipTxOption()
  612. sip_header_vector = pj.SipHeaderVector()
  613. for k, v in sip_headers.items():
  614. _sip_header = pj.SipHeader()
  615. _sip_header.hName = str(k)
  616. _sip_header.hValue = str(v)
  617. self.logger.info('hangup, header_name=%s, header_value=%s'%(k, v))
  618. sip_header_vector.push_back(_sip_header)
  619. call_op_param.txOption.headers = sip_header_vector
  620. try:
  621. acc = self.accounts.get(user_part)
  622. if acc:
  623. for k, v in list(acc.calls.items()):
  624. self.logger.info('hangup, call_idx=%s, call_active=%s'%(k, v.isActive()))
  625. if v.isActive():
  626. v.hangup(call_op_param)
  627. acc.calls.clear()
  628. except:
  629. traceback.print_exc()
  630. finally:
  631. # 机器人主动挂机回收分机号
  632. self.release(user_part)
  633. def register(self, **kwargs):
  634. self.logger.info('register,come in, pool.size :%d', self.user_part_pool.qsize())
  635. user_part = self.user_part_pool.get()
  636. acc = self.accounts.get(user_part)
  637. self.logger.info('register, user_part :%d, pool.size :%d', user_part, self.user_part_pool.qsize())
  638. if acc:
  639. self.logger.info('register==========> %s', acc.getId())
  640. # ps = pj.PresenceStatus()
  641. # ps.status = pj.PJSUA_BUDDY_STATUS_ONLINE
  642. # ps.activity = pj.PJRPID_ACTIVITY_AWAY
  643. # ps.note = "Away"
  644. # acc.setOnlineStatus(ps)
  645. # acc.setRegistration(renew=True)
  646. acc.kwargs = kwargs
  647. return user_part
  648. def unregister(self, user_part):
  649. acc = self.accounts.get(user_part)
  650. if acc:
  651. acc.setRegistration(renew=False)
  652. # 用户远程挂机回收分机号
  653. self.release(user_part)
  654. def release(self, user_part):
  655. if not user_part:
  656. self.logger.info("release, user_part is None")
  657. return
  658. def element_in_queue(q, element):
  659. with q.mutex: # 确保线程安全
  660. for item in list(q.queue): # 将队列转换为列表进行遍历
  661. if item == element:
  662. return True
  663. return False
  664. if element_in_queue(self.user_part_pool, user_part):
  665. self.logger.info("release, already exists, user_part :%d, pool.size :%d", user_part, self.user_part_pool.qsize())
  666. return
  667. self.user_part_pool.put(user_part)
  668. self.logger.info("release, user_part :%d, pool.size :%d", user_part, self.user_part_pool.qsize())
  669. def start(self):
  670. self.is_stopping = False
  671. self.counter += 1
  672. self.pjsua_thread = threading.Thread(name=f"PJSUA-THREAD-{self.counter}", target=self.create_pjsua2, daemon=True)
  673. self.pjsua_thread.start()
  674. self.logger.info("bot agent starting ...")
  675. def restart(self):
  676. self.destroy()
  677. self.logger.info('restart, 22222')
  678. self.start()
  679. # threading.Thread(target=self.create_pjsua2, daemon=True).start()
  680. def destroy(self):
  681. self.is_stopping = True
  682. self.logger.info("destroy, come in 11111")
  683. try:
  684. while not self.user_part_pool.empty():
  685. self.user_part_pool.get_nowait()
  686. except:
  687. pass
  688. self.logger.info("destroy, come in 22222")
  689. self.call_players.clear()
  690. self.accounts.clear()
  691. self.calls.clear()
  692. # # Destroy the library
  693. # self.ep.libDestroy()
  694. self.logger.info("destroy, come in 33333")
  695. if not self.pjsua_thread.is_alive():
  696. return
  697. ident = self.pjsua_thread.ident
  698. thread_id = ctypes.pythonapi.PyThreadState_SetAsyncExc
  699. res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
  700. ctypes.c_long(ident), ctypes.py_object(SystemExit)
  701. )
  702. self.logger.info("destroy, ident=%s, thread_id=%s, res=%s", ident, thread_id, res)
  703. # if res == 0:
  704. # raise ValueError("Invalid thread ID")
  705. # elif res > 1:
  706. # # 如果多次调用,需要复位
  707. # ctypes.pythonapi.PyThreadState_SetAsyncExc(ident, 0)
  708. # raise SystemError("PyThreadState_SetAsyncExc failed")
  709. def __del__(self):
  710. self.destroy()
  711. self.daemon_stopping = True
  712. # if __name__ == '__main__':
  713. # import logging
  714. # logger = logging.getLogger('voip')
  715. # bot = BotAgent(logger)