From 99c96702e364f46a3460d72fe52a41bae02d3440 Mon Sep 17 00:00:00 2001 From: KimigaiiWuyi <444835641@qq.com> Date: Mon, 2 Oct 2023 00:22:36 +0800 Subject: [PATCH 1/3] =?UTF-8?q?:bug:=20=E5=AE=8C=E5=96=84QQBot=E7=9A=84?= =?UTF-8?q?=E9=83=A8=E5=88=86API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/adapters/qq/bot.py | 52 +++++++++++++++++++---------- nonebot/adapters/qq/event.py | 8 +++++ nonebot/adapters/qq/models/guild.py | 6 ++-- nonebot/adapters/qq/models/qq.py | 16 ++++----- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/nonebot/adapters/qq/bot.py b/nonebot/adapters/qq/bot.py index e8c61d6..5652e02 100644 --- a/nonebot/adapters/qq/bot.py +++ b/nonebot/adapters/qq/bot.py @@ -294,7 +294,8 @@ def _extract_send_message( kwargs["markdown"] = markdown[-1].data["markdown"] if reference := (message["reference"] or None): kwargs["message_reference"] = reference[-1].data["reference"] - + if keyboard := (message["keyboard"] or None): + kwargs["keyboard"] = keyboard[-1].data['keyboard'] return kwargs async def send_to_dms( @@ -331,7 +332,7 @@ async def send_to_c2c( message: Union[str, Message, MessageSegment], msg_id: Optional[str] = None, event_id: Optional[str] = None, - ) -> PostC2CMessagesReturn: + ) -> Union[PostC2CMessagesReturn, PostC2CFilesReturn]: kwargs = self._extract_send_message(message=message) if kwargs.get("embed"): msg_type = 4 @@ -344,13 +345,18 @@ async def send_to_c2c( else: msg_type = 0 - return await self.post_c2c_messages( - user_id=user_id, - msg_type=msg_type, - msg_id=msg_id, - event_id=event_id, - **kwargs, - ) + if msg_type == 1: + return await self.post_c2c_files( + user_id=user_id, file_type=msg_type, url=kwargs['image'] + ) + else: + return await self.post_c2c_messages( + user_id=user_id, + msg_type=msg_type, + msg_id=msg_id, + event_id=event_id, + **kwargs, + ) async def send_to_group( self, @@ -358,7 +364,7 @@ async def send_to_group( message: Union[str, Message, MessageSegment], msg_id: Optional[str] = None, event_id: Optional[str] = None, - ) -> PostGroupMessagesReturn: + ) -> Union[PostGroupMessagesReturn, PostGroupFilesReturn]: kwargs = self._extract_send_message(message=message) if kwargs.get("embed"): msg_type = 4 @@ -371,13 +377,18 @@ async def send_to_group( else: msg_type = 0 - return await self.post_group_messages( - group_id=group_id, - msg_type=msg_type, - msg_id=msg_id, - event_id=event_id, - **kwargs, - ) + if msg_type == 1: + return await self.post_group_files( + group_id=group_id, file_type=msg_type, url=kwargs['image'] + ) + else: + return await self.post_group_messages( + group_id=group_id, + msg_type=msg_type, + msg_id=msg_id, + event_id=event_id, + **kwargs, + ) @override async def send( @@ -1518,7 +1529,13 @@ async def post_c2c_messages( message_reference: None = None, event_id: Optional[str] = None, msg_id: Optional[str] = None, + timestamp: Optional[Union[int, datetime]] = None, ) -> PostC2CMessagesReturn: + if isinstance(timestamp, datetime): + timestamp = int(timestamp.timestamp()) + elif timestamp is None: + timestamp = int(datetime.now(timezone.utc).timestamp()) + request = Request( "POST", self.adapter.get_api_base().joinpath("v2", "users", user_id, "messages"), @@ -1544,6 +1561,7 @@ async def post_c2c_messages( "message_reference": message_reference, "event_id": event_id, "msg_id": msg_id, + "timestamp": timestamp, } ), ) diff --git a/nonebot/adapters/qq/event.py b/nonebot/adapters/qq/event.py index 1509315..6c56284 100644 --- a/nonebot/adapters/qq/event.py +++ b/nonebot/adapters/qq/event.py @@ -331,6 +331,10 @@ class C2CMessageCreateEvent(MessageEvent): content: str timestamp: str + @override + def get_user_id(self) -> str: + return self.author.id + @override def get_message(self) -> Message: if not hasattr(self, "_message"): @@ -352,6 +356,10 @@ class GroupAtMessageCreateEvent(MessageEvent): content: str timestamp: str + @override + def get_user_id(self) -> str: + return self.author.id + @override def get_message(self) -> Message: if not hasattr(self, "_message"): diff --git a/nonebot/adapters/qq/models/guild.py b/nonebot/adapters/qq/models/guild.py index 9ea3672..839a933 100644 --- a/nonebot/adapters/qq/models/guild.py +++ b/nonebot/adapters/qq/models/guild.py @@ -24,7 +24,7 @@ class Guild(BaseModel): # User class User(BaseModel): id: str - username: str + username: Optional[str] = None avatar: Optional[str] = None bot: Optional[bool] = None union_openid: Optional[str] = None @@ -180,7 +180,7 @@ class Message(BaseModel): channel_id: str guild_id: str content: Optional[str] = None - timestamp: datetime + timestamp: Optional[datetime] = None edited_timestamp: Optional[datetime] = None mention_everyone: Optional[bool] = None author: User @@ -343,7 +343,7 @@ class ReactionTargetType(IntEnum): class ReactionTarget(BaseModel): id: str - type: Union[ReactionTargetType, int] + type: Union[ReactionTargetType, str] class MessageReaction(BaseModel): diff --git a/nonebot/adapters/qq/models/qq.py b/nonebot/adapters/qq/models/qq.py index acb81cd..6339b2b 100644 --- a/nonebot/adapters/qq/models/qq.py +++ b/nonebot/adapters/qq/models/qq.py @@ -9,23 +9,23 @@ class Author(BaseModel): class PostC2CMessagesReturn(BaseModel): - id: str - timestamp: datetime + id: Optional[str] = None + timestamp: Optional[datetime] = None class PostGroupMessagesReturn(BaseModel): - id: str - timestamp: datetime + id: Optional[str] = None + timestamp: Optional[datetime] = None class PostC2CFilesReturn(BaseModel): - id: str - timestamp: datetime + id: Optional[str] = None + timestamp: Optional[datetime] = None class PostGroupFilesReturn(BaseModel): - id: str - timestamp: datetime + id: Optional[str] = None + timestamp: Optional[datetime] = None # Interaction Event From 707bdaafeb9402e130a17175c101bf1ac3aaafe9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 16:23:06 +0000 Subject: [PATCH 2/3] :rotating_light: auto fix by pre-commit hooks --- nonebot/adapters/qq/bot.py | 6 +++--- nonebot/adapters/qq/event.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/nonebot/adapters/qq/bot.py b/nonebot/adapters/qq/bot.py index 5652e02..f15c47f 100644 --- a/nonebot/adapters/qq/bot.py +++ b/nonebot/adapters/qq/bot.py @@ -295,7 +295,7 @@ def _extract_send_message( if reference := (message["reference"] or None): kwargs["message_reference"] = reference[-1].data["reference"] if keyboard := (message["keyboard"] or None): - kwargs["keyboard"] = keyboard[-1].data['keyboard'] + kwargs["keyboard"] = keyboard[-1].data["keyboard"] return kwargs async def send_to_dms( @@ -347,7 +347,7 @@ async def send_to_c2c( if msg_type == 1: return await self.post_c2c_files( - user_id=user_id, file_type=msg_type, url=kwargs['image'] + user_id=user_id, file_type=msg_type, url=kwargs["image"] ) else: return await self.post_c2c_messages( @@ -379,7 +379,7 @@ async def send_to_group( if msg_type == 1: return await self.post_group_files( - group_id=group_id, file_type=msg_type, url=kwargs['image'] + group_id=group_id, file_type=msg_type, url=kwargs["image"] ) else: return await self.post_group_messages( diff --git a/nonebot/adapters/qq/event.py b/nonebot/adapters/qq/event.py index 6c56284..9ab9911 100644 --- a/nonebot/adapters/qq/event.py +++ b/nonebot/adapters/qq/event.py @@ -334,7 +334,7 @@ class C2CMessageCreateEvent(MessageEvent): @override def get_user_id(self) -> str: return self.author.id - + @override def get_message(self) -> Message: if not hasattr(self, "_message"): @@ -359,7 +359,7 @@ class GroupAtMessageCreateEvent(MessageEvent): @override def get_user_id(self) -> str: return self.author.id - + @override def get_message(self) -> Message: if not hasattr(self, "_message"): From 16b504ffe2a2a3ff61f976e4d0c08b5879039d13 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Mon, 2 Oct 2023 10:43:51 +0800 Subject: [PATCH 3/3] Apply suggestions from code review --- nonebot/adapters/qq/bot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nonebot/adapters/qq/bot.py b/nonebot/adapters/qq/bot.py index f15c47f..9b7763d 100644 --- a/nonebot/adapters/qq/bot.py +++ b/nonebot/adapters/qq/bot.py @@ -345,6 +345,7 @@ async def send_to_c2c( else: msg_type = 0 + # tmp fix due to image not implemented in message api if msg_type == 1: return await self.post_c2c_files( user_id=user_id, file_type=msg_type, url=kwargs["image"] @@ -377,6 +378,7 @@ async def send_to_group( else: msg_type = 0 + # tmp fix due to image not implemented in message api if msg_type == 1: return await self.post_group_files( group_id=group_id, file_type=msg_type, url=kwargs["image"]