diff --git a/src/langbot/libs/qq_official_api/api.py b/src/langbot/libs/qq_official_api/api.py index f5c4c618..435dd530 100644 --- a/src/langbot/libs/qq_official_api/api.py +++ b/src/langbot/libs/qq_official_api/api.py @@ -291,6 +291,15 @@ class QQOfficialClient: """ if not await self.check_access_token(): await self.get_access_token() + + # Validate that exactly one context parameter is provided + provided_contexts = sum([bool(channel_id), bool(group_openid), bool(user_openid)]) + if provided_contexts == 0: + await self.logger.warning(f'Cannot fetch message {message_id}: no context provided') + return {} + if provided_contexts > 1: + await self.logger.warning(f'Cannot fetch message {message_id}: multiple contexts provided') + return {} # Determine which API endpoint to use based on provided parameters if channel_id: @@ -302,9 +311,6 @@ class QQOfficialClient: elif user_openid: # Private message url = f'{self.base_url}/v2/users/{user_openid}/messages/{message_id}' - else: - await self.logger.warning(f'Cannot fetch message {message_id}: no valid context provided') - return {} async with httpx.AsyncClient() as client: headers = { diff --git a/src/langbot/pkg/platform/sources/qqofficial.py b/src/langbot/pkg/platform/sources/qqofficial.py index 05e5f641..d58a9f1d 100644 --- a/src/langbot/pkg/platform/sources/qqofficial.py +++ b/src/langbot/pkg/platform/sources/qqofficial.py @@ -70,7 +70,7 @@ class QQOfficialMessageConverter(abstract_platform_adapter.AbstractMessageConver content_type=attachment.get('content_type', '') ) quoted_chain.append(platform_message.Image(base64=img_base64)) - except Exception as e: + except Exception: # If image fetch fails, just skip it pass @@ -89,8 +89,7 @@ class QQOfficialMessageConverter(abstract_platform_adapter.AbstractMessageConver ) except Exception as e: # If fetching quoted message fails, log and continue - if self.bot and hasattr(self.bot, 'logger'): - await self.bot.logger.warning(f'Failed to fetch quoted message {referenced_msg_id}: {e}') + await self.bot.logger.warning(f'Failed to fetch quoted message {referenced_msg_id}: {e}') if pic_url is not None: base64_url = await image.get_qq_official_image_base64(pic_url=pic_url, content_type=content_type)