|
@@ -13,7 +13,7 @@ from enum import Enum
|
|
|
from src.core.voip.constant import *
|
|
|
|
|
|
import requests
|
|
|
-from src.core.callcenter.model import BotChatRequest, ChatResponse
|
|
|
+from src.core.callcenter.model import BotChatRequest, ChatResponse,ChatMessage
|
|
|
|
|
|
|
|
|
calls = {}
|
|
@@ -78,9 +78,9 @@ class MyAudioMediaPort(pj.AudioMediaPort):
|
|
|
self.user_asr_texts.clear()
|
|
|
self.call.chat("2",user_asr_text)
|
|
|
|
|
|
- player_queue_size = self.call.player_queue.qsize()
|
|
|
- if (player_queue_size > 0 and not self.cur_player_file) or (player_queue_size > 0 and play_complete):
|
|
|
- print('onFrameReceived:player_queue_size=', player_queue_size, 'play_complete=', play_complete)
|
|
|
+ message_queue_size = self.call.message_queue.qsize()
|
|
|
+ if (message_queue_size > 0 and not self.cur_player_file) or (message_queue_size > 0 and play_complete):
|
|
|
+ print('onFrameReceived:message_queue_size=', message_queue_size, 'play_complete=', play_complete)
|
|
|
self.cur_player_file = self.get_player_file()
|
|
|
self.call.send_bot_speaker(self.cur_player_file)
|
|
|
except:
|
|
@@ -101,11 +101,12 @@ class MyAudioMediaPort(pj.AudioMediaPort):
|
|
|
|
|
|
def get_player_file(self):
|
|
|
try:
|
|
|
- player_file = self.call.player_queue.get(block=False)
|
|
|
- print('get_player_file:', player_file)
|
|
|
+ message = self.call.message_queue.get(block=False)
|
|
|
+ player_file = [item.voice_url for item in message.contents if item.content_type == 'voice']
|
|
|
return player_file
|
|
|
- except:
|
|
|
- pass
|
|
|
+ except Exception as e:
|
|
|
+ print(f"sdsd: {e}")
|
|
|
+ traceback.print_exc()
|
|
|
|
|
|
|
|
|
class MyAudioMediaPlayer(pj.AudioMediaPlayer):
|
|
@@ -160,9 +161,9 @@ class MyCall(pj.Call):
|
|
|
self.asr = None
|
|
|
|
|
|
|
|
|
- self.scripts = build_demo_script()
|
|
|
+ # self.scripts = build_demo_script()
|
|
|
self.user_asr_text_queue = queue.Queue(maxsize=100)
|
|
|
- self.player_queue = queue.Queue(maxsize=3)
|
|
|
+ self.message_queue = queue.Queue(maxsize=3)
|
|
|
self.player_complete_dict = {}
|
|
|
|
|
|
from src.core.voip.asr import TestSt
|
|
@@ -217,13 +218,14 @@ class MyCall(pj.Call):
|
|
|
self.aud_med.startTransmit(self.audio_port)
|
|
|
|
|
|
def send_bot_speaker(self, player_file):
|
|
|
- if not player_file or not os.path.exists(player_file):
|
|
|
+ if not player_file :
|
|
|
return
|
|
|
player_id = murmur3_32(player_file)
|
|
|
self.player_complete_dict[player_id] = False
|
|
|
+ print('self.player_complete_dict[player_id]D:', player_id, player_file, self.player_complete_dict[player_id])
|
|
|
self.player = MyAudioMediaPlayer(player_id, self.aud_med, on_complete=self.on_media_player_complete)
|
|
|
- self.player.createPlayer(player_file, options=pj.PJMEDIA_FILE_NO_LOOP)
|
|
|
- # self.player.createPlaylist(player_file, options=pj.PJMEDIA_FILE_NO_LOOP)
|
|
|
+ # self.player.createPlayer(player_file, pj.PJMEDIA_FILE_NO_LOOP)
|
|
|
+ self.player.createPlaylist(player_file, 'my_hello_playlist', pj.PJMEDIA_FILE_NO_LOOP)
|
|
|
self.player.startTransmit(self.aud_med)
|
|
|
|
|
|
def on_receiver_asr_result(self, message, *args):
|
|
@@ -236,20 +238,8 @@ class MyCall(pj.Call):
|
|
|
|
|
|
def bot_say_hello(self):
|
|
|
print('bot_say_hello, come in ')
|
|
|
- # self.chat('1', user_asr_text="SAY_HELLO")
|
|
|
- player_file = '/code/src/core/voip/test111.wav'
|
|
|
- player_id = murmur3_32(player_file)
|
|
|
- self.player = MyAudioMediaPlayer(player_id, self.aud_med, on_complete=self.on_media_player_complete)
|
|
|
- # self.player.createPlayer(player_file, options=pj.PJMEDIA_FILE_NO_LOOP)
|
|
|
- self.player.createPlaylist(['/code/src/core/voip/test111.wav','/code/src/core/voip/test222.wav'], "my_hello_playlist", pj.PJMEDIA_FILE_NO_LOOP)
|
|
|
- self.player.startTransmit(self.aud_med)
|
|
|
-
|
|
|
+ self.chat('1', user_asr_text="SAY_HELLO")
|
|
|
def chat(self,event_type, user_asr_text=None):
|
|
|
- # TODO 调用文本机器人接口
|
|
|
- # message = {'player_file': self.scripts.get()}
|
|
|
- # player = message.get('player_file')
|
|
|
- # print('chat::player_file=', player)
|
|
|
- # self.player_queue.put(player)
|
|
|
# 调用文本机器人接口
|
|
|
ToTextBotAgent(event_type,user_asr_text,self)
|
|
|
|
|
@@ -266,39 +256,28 @@ class ToTextBotAgent:
|
|
|
session_id="1",
|
|
|
record_id="2",
|
|
|
task_id="ceshi",
|
|
|
- # event_type=event_type, # 传入不同的 event_type 来测试不同的响应
|
|
|
asr_text=user_asr_text
|
|
|
)
|
|
|
# 发送请求并处理响应
|
|
|
- self.test_request(self.request_data)
|
|
|
+ self.test_request(self.request_data,event_type)
|
|
|
|
|
|
def to_hang(self, action_content):
|
|
|
user_part = self.call_agent.user_part_pool.get()
|
|
|
self.call_agent.hangup(user_part, action_content)
|
|
|
|
|
|
- def transfer_people(self):
|
|
|
- print('todo 转人工')
|
|
|
-
|
|
|
- def handling_release(self, response: ChatResponse):
|
|
|
- print('handling_release', response.data)
|
|
|
- if response:
|
|
|
- data = response.data
|
|
|
- action = data.action
|
|
|
+ def handling_message(self, message: ChatMessage):
|
|
|
+ print('handling_release', message)
|
|
|
+ if message:
|
|
|
+ action = message.action
|
|
|
action_code = action.action_code
|
|
|
- print(f"ActionCode: {action_code}", data)
|
|
|
if action_code == 'hang': # 挂断
|
|
|
action_content = action.action_content
|
|
|
self.to_hang(action_content)
|
|
|
elif action_code == 'transfer': # 转人工
|
|
|
- self.transfer_people()
|
|
|
+ print('todo 转人工')
|
|
|
elif action_code == 'normal': # 正常通话
|
|
|
- contents = data.contents
|
|
|
- for content in contents:
|
|
|
- if content.content_type == 'voice' and content.voice_url:
|
|
|
- self.call_agent.player_queue.put(content.voice_url)
|
|
|
- # 等待几秒以后重新调用文本机器人接口
|
|
|
- wait_time = data.wait_time
|
|
|
- print(f"成功获取响应: ActionCode: {action_code}, WaitTime: {wait_time}")
|
|
|
+ self.call_agent.message_queue.put(message)
|
|
|
+ print(f"成功获取响应: ActionCode: {message.wait_time}")
|
|
|
else:
|
|
|
print("文本机器人接口调用失败")
|
|
|
|
|
@@ -322,7 +301,7 @@ class ToTextBotAgent:
|
|
|
return None
|
|
|
|
|
|
# 模拟接口请求返回
|
|
|
- def test_request(self, params: BotChatRequest):
|
|
|
+ def test_request(self, params: BotChatRequest,event_type):
|
|
|
print("test_request::params=", params)
|
|
|
response_data = {
|
|
|
"node_id": "1.0",
|
|
@@ -336,33 +315,34 @@ class ToTextBotAgent:
|
|
|
"talk_time_out": ""
|
|
|
}
|
|
|
|
|
|
- print("params.event_type:", params.event_type)
|
|
|
- if params.event_type == '1':
|
|
|
+ print("event_type:", event_type)
|
|
|
+ if event_type == '1':
|
|
|
response_data['contents'].append({
|
|
|
"content_type": "voice",
|
|
|
"content": "",
|
|
|
- "voice_url": '/code/src/core/voip/test111.wav',
|
|
|
+ "voice_url": '/code/src/core/voip/scripts/1_00.wav',
|
|
|
"voice_content": "五一北京到上海的高铁票还有吗?"
|
|
|
})
|
|
|
- elif params.event_type == '2':
|
|
|
- response_data['contents'].append({
|
|
|
- "content_type": "voice",
|
|
|
- "content": "",
|
|
|
- "voice_url": '/code/src/core/voip/test222.wav',
|
|
|
- "voice_content": "测试第二个录音文件"
|
|
|
- })
|
|
|
-
|
|
|
- response = {
|
|
|
- "data": response_data,
|
|
|
- "message": "success",
|
|
|
- "code": 200
|
|
|
- }
|
|
|
+ elif event_type == '2':
|
|
|
+ new_contents = [
|
|
|
+ {
|
|
|
+ "content_type": "voice",
|
|
|
+ "content": "",
|
|
|
+ "voice_url": '/code/src/core/voip/test111.wav',
|
|
|
+ "voice_content": "测试第二个录音文件"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "content_type": "voice",
|
|
|
+ "content": "",
|
|
|
+ "voice_url": '/code/src/core/voip/test222.wav',
|
|
|
+ "voice_content": "五一北京到上海的高铁票还有吗?"
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ response_data['contents']= new_contents
|
|
|
try:
|
|
|
- # 转换为JSON字符串并解析
|
|
|
- response_json = json.dumps(response)
|
|
|
- parsed_response = ChatResponse.from_json(response_json)
|
|
|
- print(f"Response in test_request: {parsed_response}") # 确认response
|
|
|
- self.handling_release(parsed_response)
|
|
|
+ print(json.dumps(response_data['contents']))
|
|
|
+ parsed_response = ChatMessage.from_json(response_data)
|
|
|
+ self.handling_message(parsed_response)
|
|
|
except Exception as e:
|
|
|
print(f"Error in test_request: {e}")
|
|
|
traceback.print_exc() # 打印完整的错误信息
|