From 7373e75c75ae28f46400ccf120dbb8572e2f5628 Mon Sep 17 00:00:00 2001 From: xuqian13 <1334431750@qq.com> Date: Sat, 14 Jun 2025 08:04:33 +0000 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=8F=91?= =?UTF-8?q?=E9=80=81=E8=AF=AD=E9=9F=B3=E5=8F=AF=E9=80=89=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=92=8C=E7=BD=91=E7=BB=9C=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/send_handler.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/send_handler.py b/src/send_handler.py index baf6fe7..f2a0c83 100644 --- a/src/send_handler.py +++ b/src/send_handler.py @@ -207,16 +207,27 @@ class SendHandler: }, } - def handle_voice_message(self, encoded_voice: str) -> dict: + + + + def handle_voice_message(self, voice_data_or_path: str) -> dict: """处理语音消息""" - if not global_config.use_tts: - logger.warning("未启用语音消息处理") - return {} - if not encoded_voice: + + if not voice_data_or_path: return {} + + if voice_data_or_path.startswith("file://") or voice_data_or_path.startswith("http://") or voice_data_or_path.startswith("https://"): + file_value = voice_data_or_path + logger.debug(f"识别到语音数据为路径/URL: {file_value}") + elif voice_data_or_path.startswith("base64://"): + file_value = voice_data_or_path + logger.debug(f"识别到语音数据为 Base64 (带前缀): {file_value[:50]}...") + else: + file_value = f"base64://{voice_data_or_path}" + logger.debug(f"识别到语音数据为 Base64 (无前缀): {voice_data_or_path[:50]}...") return { "type": "record", - "data": {"file": f"base64://{encoded_voice}"}, + "data": {"file": file_value}, } def handle_ban_command(self, args: Dict[str, Any], group_info: GroupInfo) -> Tuple[str, Dict[str, Any]]: @@ -296,16 +307,25 @@ class SendHandler: ) async def send_message_to_napcat(self, action: str, params: dict) -> dict: + if not self.server_connection: + logger.error("Adapter 未连接到平台 API,无法发送消息!") + return {"status": "error", "message": "adapter not connected to platform api"} + request_uuid = str(uuid.uuid4()) payload = json.dumps({"action": action, "params": params, "echo": request_uuid}) - await self.server_connection.send(payload) + try: + await self.server_connection.send(payload) + logger.debug(f"发送平台 API 命令: action={action}, echo={request_uuid}") + response = await get_response(request_uuid) + logger.debug(f"收到平台 API 响应: status={response.get('status')}, echo={response.get('echo')}") + except TimeoutError: - logger.error("发送消息超时,未收到响应") + logger.error(f"发送平台 API 命令 '{action}' 超时,未收到响应 (echo: {request_uuid})") return {"status": "error", "message": "timeout"} except Exception as e: - logger.error(f"发送消息失败: {e}") + logger.error(f"发送平台 API 命令 '{action}' 失败 (echo: {request_uuid}): {e}") return {"status": "error", "message": str(e)} return response From 7c78027d4b8f090d2691d5541a7f860d01600c10 Mon Sep 17 00:00:00 2001 From: 1334431750 <1334431750@qq.com> Date: Sat, 21 Jun 2025 09:40:40 +0000 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=9F=B3=E4=B9=90=E5=8D=A1=E7=89=87=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/send_handler.py | 61 ++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/send_handler.py b/src/send_handler.py index f2a0c83..f506d00 100644 --- a/src/send_handler.py +++ b/src/send_handler.py @@ -156,6 +156,12 @@ class SendHandler: elif seg.type == "voice": voice = seg.data new_payload = self.build_payload(payload, self.handle_voice_message(voice), False) + elif seg.type == "voiceurl": + voice = seg.data + new_payload = self.build_payload(payload, self.handle_voiceurl_message(voice), False) + elif seg.type == "music": + music = seg.data + new_payload = self.build_payload(payload, self.handle_music_message(music), False) return new_payload def build_payload(self, payload: list, addon: dict, is_reply: bool = False) -> list: @@ -207,27 +213,33 @@ class SendHandler: }, } - - - - def handle_voice_message(self, voice_data_or_path: str) -> dict: + def handle_voice_message(self, encoded_voice: str) -> dict: """处理语音消息""" - - if not voice_data_or_path: + if not global_config.use_tts: + logger.warning("未启用语音消息处理") + return {} + if not encoded_voice: return {} - - if voice_data_or_path.startswith("file://") or voice_data_or_path.startswith("http://") or voice_data_or_path.startswith("https://"): - file_value = voice_data_or_path - logger.debug(f"识别到语音数据为路径/URL: {file_value}") - elif voice_data_or_path.startswith("base64://"): - file_value = voice_data_or_path - logger.debug(f"识别到语音数据为 Base64 (带前缀): {file_value[:50]}...") - else: - file_value = f"base64://{voice_data_or_path}" - logger.debug(f"识别到语音数据为 Base64 (无前缀): {voice_data_or_path[:50]}...") return { "type": "record", - "data": {"file": file_value}, + "data": {"file": f"base64://{encoded_voice}"}, + } + + def handle_voiceurl_message(self, voice_url: str) -> dict: + """处理语音链接消息""" + return { + "type": "record", + "data": {"file": voice_url}, + } + + def handle_music_message(self, song_id: str) -> dict: + """处理音乐消息""" + return { + "type": "music", + "data": { + "type": "163", + "id": song_id + }, } def handle_ban_command(self, args: Dict[str, Any], group_info: GroupInfo) -> Tuple[str, Dict[str, Any]]: @@ -307,25 +319,16 @@ class SendHandler: ) async def send_message_to_napcat(self, action: str, params: dict) -> dict: - if not self.server_connection: - logger.error("Adapter 未连接到平台 API,无法发送消息!") - return {"status": "error", "message": "adapter not connected to platform api"} - request_uuid = str(uuid.uuid4()) payload = json.dumps({"action": action, "params": params, "echo": request_uuid}) - + await self.server_connection.send(payload) try: - await self.server_connection.send(payload) - logger.debug(f"发送平台 API 命令: action={action}, echo={request_uuid}") - response = await get_response(request_uuid) - logger.debug(f"收到平台 API 响应: status={response.get('status')}, echo={response.get('echo')}") - except TimeoutError: - logger.error(f"发送平台 API 命令 '{action}' 超时,未收到响应 (echo: {request_uuid})") + logger.error("发送消息超时,未收到响应") return {"status": "error", "message": "timeout"} except Exception as e: - logger.error(f"发送平台 API 命令 '{action}' 失败 (echo: {request_uuid}): {e}") + logger.error(f"发送消息失败: {e}") return {"status": "error", "message": str(e)} return response From bfb91702365d2f0fd3cb7a34b1b9f1a16069e480 Mon Sep 17 00:00:00 2001 From: 1334431750 <1334431750@qq.com> Date: Sat, 21 Jun 2025 09:50:20 +0000 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=9F=B3=E4=B9=90=E5=8D=A1=E7=89=87=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/send_handler.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/send_handler.py b/src/send_handler.py index f506d00..b763d96 100644 --- a/src/send_handler.py +++ b/src/send_handler.py @@ -157,11 +157,11 @@ class SendHandler: voice = seg.data new_payload = self.build_payload(payload, self.handle_voice_message(voice), False) elif seg.type == "voiceurl": - voice = seg.data - new_payload = self.build_payload(payload, self.handle_voiceurl_message(voice), False) + voice_url = seg.data + new_payload = self.build_payload(payload, self.handle_voiceurl_message(voice_url), False) elif seg.type == "music": - music = seg.data - new_payload = self.build_payload(payload, self.handle_music_message(music), False) + song_id = seg.data + new_payload = self.build_payload(payload, self.handle_music_message(song_id), False) return new_payload def build_payload(self, payload: list, addon: dict, is_reply: bool = False) -> list: From e757196fe1f07015f009423d533edb27cdc70c1f Mon Sep 17 00:00:00 2001 From: 1334431750 <1334431750@qq.com> Date: Sun, 22 Jun 2025 02:22:50 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/send_handler.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/send_handler.py b/src/send_handler.py index b763d96..2a6709e 100644 --- a/src/send_handler.py +++ b/src/send_handler.py @@ -97,6 +97,8 @@ class SendHandler: command, args_dict = self.handle_whole_ban_command(seg_data.get("args"), group_info) case CommandType.GROUP_KICK.name: command, args_dict = self.handle_kick_command(seg_data.get("args"), group_info) + case CommandType.SEND_POKE.name: + command, args_dict = self.handle_poke_command(seg_data.get("args"), group_info) case _: logger.error(f"未知命令: {command_name}") return @@ -215,7 +217,7 @@ class SendHandler: def handle_voice_message(self, encoded_voice: str) -> dict: """处理语音消息""" - if not global_config.use_tts: + if not global_config.voice.use_tts: logger.warning("未启用语音消息处理") return {} if not encoded_voice: @@ -236,10 +238,7 @@ class SendHandler: """处理音乐消息""" return { "type": "music", - "data": { - "type": "163", - "id": song_id - }, + "data": {"type": "163", "id": song_id}, } def handle_ban_command(self, args: Dict[str, Any], group_info: GroupInfo) -> Tuple[str, Dict[str, Any]]: @@ -318,6 +317,33 @@ class SendHandler: }, ) + def handle_poke_command(self, args: Dict[str, Any], group_info: GroupInfo) -> Tuple[str, Dict[str, Any]]: + """处理戳一戳命令 + + Args: + args (Dict[str, Any]): 参数字典 + group_info (GroupInfo): 群聊信息(对应目标群聊) + + Returns: + Tuple[CommandType, Dict[str, Any]] + """ + user_id: int = int(args["qq_id"]) + if group_info is None: + group_id = None + else: + group_id: int = int(group_info.group_id) + if group_id <= 0: + raise ValueError("群组ID无效") + if user_id <= 0: + raise ValueError("用户ID无效") + return ( + CommandType.SEND_POKE.value, + { + "group_id": group_id, + "user_id": user_id, + }, + ) + async def send_message_to_napcat(self, action: str, params: dict) -> dict: request_uuid = str(uuid.uuid4()) payload = json.dumps({"action": action, "params": params, "echo": request_uuid})