|
@@ -15,6 +15,8 @@ from src.core.datasource import SIP_SERVER, SERVE_HOST
|
|
|
from src.core.voip.constant import *
|
|
|
|
|
|
import requests
|
|
|
+import aiohttp
|
|
|
+import asyncio
|
|
|
from src.core.callcenter.api import BotChatRequest,ChatMessage
|
|
|
|
|
|
from src.core import singleton_keys
|
|
@@ -365,34 +367,57 @@ class ToTextBotAgent:
|
|
|
)
|
|
|
# 发送请求并处理响应
|
|
|
# self.test_request(self.request_data)
|
|
|
- self.to_quest(self.request_data)
|
|
|
+ # self.to_quest(self.request_data)
|
|
|
+ asyncio.run(self.to_quest_async(self.request_data))
|
|
|
|
|
|
-
|
|
|
- def to_quest(self, request: BotChatRequest):
|
|
|
- # 将实体类转换为JSON字符串
|
|
|
+ async def to_quest_async(self, request: BotChatRequest):
|
|
|
headers = {'Content-Type': 'application/json'}
|
|
|
request_data = request.to_json_string()
|
|
|
url = f"http://{SERVE_HOST}:40072/botservice"
|
|
|
- # 发送POST请求
|
|
|
- print(f"请求数据:{request_data},url:{url}")
|
|
|
+
|
|
|
+ print(f"请求数据:{request_data}, url: {url}")
|
|
|
try:
|
|
|
- response = requests.post(url=url, json=json.loads(request_data), headers=headers, timeout=10) # 使用占位URL
|
|
|
- print(f"原始响应内容:{response.text}")
|
|
|
- if response.status_code == 200:
|
|
|
- response_data = response.json()
|
|
|
- if "data" in response_data and response_data["code"]==0:
|
|
|
- data = response_data["data"]
|
|
|
- print(f"响应数据:{data}")
|
|
|
- parsed_response = ChatMessage.from_json(data)
|
|
|
- self.call_agent.message_queue.put(parsed_response)
|
|
|
- else:
|
|
|
- print("响应中没有 'data' 字段")
|
|
|
- else:
|
|
|
- # 错误处理
|
|
|
- print(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
|
|
|
- except requests.RequestException as e:
|
|
|
+ async with aiohttp.ClientSession() as session:
|
|
|
+ async with session.post(url, json=json.loads(request_data), headers=headers, timeout=10) as response:
|
|
|
+ print(f"响应状态码:{response.status}")
|
|
|
+ response_data = await response.json()
|
|
|
+ if "data" in response_data and response_data["code"] == 0:
|
|
|
+ data = response_data["data"]
|
|
|
+ print(f"响应数据:{data}")
|
|
|
+ parsed_response = ChatMessage.from_json(data)
|
|
|
+ self.call_agent.message_queue.put(parsed_response)
|
|
|
+ else:
|
|
|
+ print("响应中没有 'data' 字段或 code 不为 0")
|
|
|
+ except Exception as e:
|
|
|
print(f"请求发生异常: {e}")
|
|
|
|
|
|
+ # def to_quest(self, request: BotChatRequest):
|
|
|
+ # # 将实体类转换为JSON字符串
|
|
|
+ # headers = {'Content-Type': 'application/json'}
|
|
|
+ # request_data = request.to_json_string()
|
|
|
+ # url = f"http://{SERVE_HOST}:40072/botservice"
|
|
|
+ # # 发送POST请求
|
|
|
+ # print(f"请求数据:{request_data},url:{url}")
|
|
|
+ # try:
|
|
|
+ # print(f"发送请求之前2334")
|
|
|
+ # response = requests.post(url=url, json=json.loads(request_data), headers=headers, timeout=10) # 使用占位URL
|
|
|
+ # print(f"原始响应内容:{response.text}")
|
|
|
+ # print(f"响应状态码:{response.status_code}") # 查看状态码
|
|
|
+ # if response.status_code == 200:
|
|
|
+ # response_data = response.json()
|
|
|
+ # if "data" in response_data and response_data["code"]==0:
|
|
|
+ # data = response_data["data"]
|
|
|
+ # print(f"响应数据:{data}")
|
|
|
+ # parsed_response = ChatMessage.from_json(data)
|
|
|
+ # self.call_agent.message_queue.put(parsed_response)
|
|
|
+ # else:
|
|
|
+ # print("响应中没有 'data' 字段")
|
|
|
+ # else:
|
|
|
+ # # 错误处理
|
|
|
+ # print(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
|
|
|
+ # except requests.RequestException as e:
|
|
|
+ # print(f"请求发生异常: {e}")
|
|
|
+
|
|
|
# 模拟接口请求返回
|
|
|
def test_request(self, params: BotChatRequest):
|
|
|
print("test_request::params=", params)
|
|
@@ -475,8 +500,8 @@ class BotAgent:
|
|
|
ep_cfg.uaConfig.maxCalls = 12
|
|
|
ep_cfg.uaConfig.maxAccounts = 12
|
|
|
ep_cfg.medConfig.noVad = True
|
|
|
- ep_cfg.logConfig.level = 4
|
|
|
- ep_cfg.logConfig.consoleLevel = 4
|
|
|
+ ep_cfg.logConfig.level = 5
|
|
|
+ ep_cfg.logConfig.consoleLevel = 5
|
|
|
self.ep.libCreate()
|
|
|
self.ep.libInit(ep_cfg)
|
|
|
|