余尚辉 пре 3 месеци
родитељ
комит
ae90fc8148
2 измењених фајлова са 28 додато и 49 уклоњено
  1. 2 2
      docker-compose.yml
  2. 26 47
      src/core/voip/bot.py

+ 2 - 2
docker-compose.yml

@@ -1,6 +1,6 @@
 services:
   pjsua:
-    image: pjsua2:v2.14.1115
+    image: pjsua2:v2.14.1114
     container_name: pjsua
     restart: always
     network_mode: host
@@ -25,4 +25,4 @@ services:
           #        - 5066:5066/tcp
           #        - 7443:7443/tcp
           #        - 8021:8021/tcp
-          #        - "64536-64556:64536-64556/udp"
+          #        - "64536-64556:64536-64556/udp"

+ 26 - 47
src/core/voip/bot.py

@@ -7,7 +7,7 @@ import wave
 import queue
 import threading
 import traceback
-
+import sys
 import pjsua2 as pj
 from enum import Enum
 
@@ -15,8 +15,6 @@ 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
@@ -367,57 +365,38 @@ class ToTextBotAgent:
         )
         # 发送请求并处理响应
         # self.test_request(self.request_data)
-        # self.to_quest(self.request_data)
-        asyncio.run(self.to_quest_async(self.request_data))
+        self.to_quest(self.request_data)
+
 
-    async def to_quest_async(self, request: BotChatRequest):
+    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"
-
-        print(f"请求数据:{request_data}, url: {url}")
+        # 发送POST请求
+        print(f"请求数据:{request_data},url:{url}")
         try:
-            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"发送请求之前2334")
+            response = requests.post(url=url,  json=json.loads(request_data), headers=headers, timeout=10)  # 使用占位URL
+            print(f"原始响应内容:{response.text}")
+            print(f"响应内容:{response.content}")  # 查看原始响应内容
+            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)
+                    sys.stdout.flush()  # 强制刷新输出缓冲区
+                else:
+                    print("响应中没有 'data' 字段")
+            else:
+                # 错误处理
+                print(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
+        except requests.RequestException 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)