-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat: send media from QQBot #1427
Conversation
…pload support - Add patched file upload method for QQBot API - Implement comprehensive message sending logic supporting text, images, voice, and video - Support message sequencing and multi-element message composition - Add base64 encoding for file uploads - Improve message conversion and handling for different message types
文件级别变更
提示和命令与 Sourcery 互动
自定义您的体验访问您的 仪表板 以:
获得帮助Original review guide in EnglishReviewer's Guide by SourceryThis pull request introduces the capability to send media messages (images, voice, and video) via the QQBot adapter. It includes modifications to the message sending logic to handle different message element types, including text, mentions, and media. A patched version of the Sequence diagram for sending a message with mediasequenceDiagram
participant User
participant IMMessage
participant QQBotAdapter
participant QQBotAPI
User->>IMMessage: Creates a message with text and media (ImageMessage/VoiceMessage/VideoElement)
User->>QQBotAdapter: send_message(message, recipient)
QQBotAdapter->>QQBotAdapter: Iterates through message.message_elements
alt TextMessage
QQBotAdapter->>QQBotAdapter: Appends text to current_text
else ImageMessage/VoiceMessage/VideoElement
QQBotAdapter->>QQBotAdapter: Sends current_text (if any) via post_message_func
QQBotAdapter->>QQBotAdapter: Uploads media using patched_post_file
QQBotAdapter->>QQBotAPI: patched_post_file(file_type, file_data, recipient)
QQBotAPI-->>QQBotAdapter: Returns media object
QQBotAdapter->>QQBotAdapter: Sends media object via post_message_func with msg_type=7
QQBotAPI->>QQBotAPI: post_c2c_message or post_group_message
end
QQBotAdapter->>QQBotAdapter: Sends remaining text (if any) via post_message_func
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
✅ All tests successful. No failed tests found. Additional details and impacted files@@ Coverage Diff @@
## master #1427 +/- ##
==========================================
+ Coverage 66.75% 68.42% +1.67%
==========================================
Files 105 105
Lines 4666 4821 +155
==========================================
+ Hits 3115 3299 +184
+ Misses 1551 1522 -29 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @lss233 - I've reviewed your changes and found some issues that need to be addressed.
Blocking issues:
- Flagging hardcoded app secret. (link)
Overall Comments:
- Consider adding type hints to the
file_data
parameter inpatched_post_file
for better clarity. - The logic for sending different types of media could be simplified by using a dictionary to map element types to file types.
Here's what I looked at during the review
- 🟡 General issues: 1 issue found
- 🔴 Security: 1 blocking issue
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
elif isinstance(element, VideoElement): | ||
file_type = 2 | ||
media = await upload_func(file_type=file_type, file_data=await element.get_data()) | ||
print(media) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Remove or replace debug print statement.
The print(media) statement appears to be a leftover debugging artifact. It might be better to use a logger or remove it to avoid unintended console output in production.
@@ -100,10 +133,59 @@ async def send_message(self, message: IMMessage, recipient: ChatSender): | |||
:param message: 要发送的消息对象。 | |||
:param recipient: 接收消息的目标对象。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider refactoring the complex loop in send_message
into helper functions and merging duplicated logic in patched_post_file
to improve readability and reduce nested conditionals and duplicated logic..
Consider refactoring the intricate loop in send_message
into small helper functions to improve readability and remove some of the nested conditionals. For example, extract the routine that flushes the current text buffer and the one that sends media elements:
async def flush_text_buffer(post_message_func, text_buffer: str, msg_seq: int) -> (str, int):
if text_buffer:
await post_message_func(content=text_buffer, msg_seq=msg_seq)
msg_seq += 1
return "", msg_seq
async def send_media_element(upload_func, post_message_func, element, msg_seq: int):
if isinstance(element, ImageMessage):
file_type = 1
elif isinstance(element, VoiceMessage):
file_type = 3
elif isinstance(element, VideoElement):
file_type = 2
media = await upload_func(file_type=file_type, file_data=await element.get_data())
await post_message_func(media=media, msg_seq=msg_seq, msg_type=7)
return msg_seq + 1
Then simplify your main loop in send_message
by invoking these helpers:
# ...
current_text = ""
msg_seq = 0
for element in message.message_elements:
if isinstance(element, TextMessage):
current_text += element.text
current_text, msg_seq = await flush_text_buffer(post_message_func, current_text, msg_seq)
elif isinstance(element, MentionElement):
current_text += f'<qqbot-at-user id="{element.target.user_id}" />'
elif isinstance(element, (ImageMessage, VoiceMessage, VideoElement)):
# Flush any accumulated text before handling media.
current_text, msg_seq = await flush_text_buffer(post_message_func, current_text, msg_seq)
msg_seq = await send_media_element(upload_func, post_message_func, element, msg_seq)
# Final flush if text remains
if current_text:
await post_message_func(content=current_text)
Additionally, consider merging any duplicated logic in patched_post_file
into the API if possible. These changes keep all functionality intact while reducing the internal complexity of your control flow.
@@ -35,7 +39,8 @@ class QQBotConfig(BaseModel): | |||
app_secret: str = Field(title="App Secret", description="机器人的 App Secret。") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨 issue (security): Flagging hardcoded app secret.
The app_secret
field in the QQBotConfig
model should not be hardcoded. It should be read from an environment variable or a secure configuration file.
elif isinstance(element, MentionElement): | ||
# 添加提及标记到当前文本缓冲区 | ||
current_text += f'<qqbot-at-user id="{element.target.user_id}" />' | ||
elif isinstance(element, ImageMessage) or isinstance(element, VoiceMessage) or isinstance(element, VideoElement): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Merge isinstance calls [×2] (merge-isinstance
)
elif isinstance(element, ImageMessage) or isinstance(element, VoiceMessage) or isinstance(element, VideoElement): | |
elif isinstance(element, (ImageMessage, VoiceMessage, VideoElement)): |
好的,这是将 pull request 总结翻译成中文的结果:
Sourcery 总结
添加了通过 QQBot 适配器发送媒体消息(图片、语音和视频)的支持。这包括修改消息发送逻辑以处理不同的消息元素类型,并添加了
post_file
方法的补丁版本来处理文件上传。新功能:
patched_post_file
方法来处理文件上传到 QQBot。MentionElement
的支持,以在 QQBot 消息中提及用户。patched_post_file
方法来处理文件上传到 QQBot。MentionElement
的支持,以在 QQBot 消息中提及用户。Original summary in English
Summary by Sourcery
Adds support for sending media messages (images, voice, and video) through the QQBot adapter. This includes modifying the message sending logic to handle different message element types and adding a patched version of the post_file method to handle file uploads.
New Features: