Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 完善 QQBot 的部分 API #54

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions nonebot/adapters/qq/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -344,21 +345,26 @@ 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,
)
yanyongyu marked this conversation as resolved.
Show resolved Hide resolved

async def send_to_group(
self,
group_id: str,
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
Expand All @@ -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,
)
yanyongyu marked this conversation as resolved.
Show resolved Hide resolved

@override
async def send(
Expand Down Expand Up @@ -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"),
Expand All @@ -1544,6 +1561,7 @@ async def post_c2c_messages(
"message_reference": message_reference,
"event_id": event_id,
"msg_id": msg_id,
"timestamp": timestamp,
}
),
)
Expand Down
8 changes: 8 additions & 0 deletions nonebot/adapters/qq/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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"):
Expand Down
6 changes: 3 additions & 3 deletions nonebot/adapters/qq/models/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -343,7 +343,7 @@ class ReactionTargetType(IntEnum):

class ReactionTarget(BaseModel):
id: str
type: Union[ReactionTargetType, int]
type: Union[ReactionTargetType, str]


class MessageReaction(BaseModel):
Expand Down
16 changes: 8 additions & 8 deletions nonebot/adapters/qq/models/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down