-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: parse aiocqhttp forward message content #8950
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
Open
FuShang114
wants to merge
1
commit into
AstrBotDevs:master
Choose a base branch
from
FuShang114:fix/aiocqhttp-forward-message
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import pytest | ||
|
|
||
| from astrbot.core.message.components import Forward, Plain | ||
| from astrbot.core.platform.sources.aiocqhttp.aiocqhttp_platform_adapter import ( | ||
| AiocqhttpAdapter, | ||
| ) | ||
|
|
||
|
|
||
| class _FakeEvent(dict): | ||
| def __getattr__(self, name): | ||
| return self[name] | ||
|
|
||
|
|
||
| class _FakeBot: | ||
| def __init__(self, responses): | ||
| self.responses = responses | ||
| self.calls = [] | ||
|
|
||
| async def call_action(self, action, **params): | ||
| self.calls.append((action, params)) | ||
| forward_id = params.get("message_id") or params.get("id") | ||
| key = (action, str(forward_id)) | ||
| if key not in self.responses: | ||
| raise RuntimeError(f"no mock response for {key}") | ||
| return self.responses[key] | ||
|
|
||
|
|
||
| def _make_group_event(message): | ||
| return _FakeEvent( | ||
| { | ||
| "self_id": 10000, | ||
| "post_type": "message", | ||
| "message_type": "group", | ||
| "message_id": 123, | ||
| "group_id": 456, | ||
| "group_name": "test group", | ||
| "sender": { | ||
| "user_id": 20000, | ||
| "nickname": "Alice", | ||
| "card": "", | ||
| }, | ||
| "message": message, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_aiocqhttp_forward_segment_expands_forward_text(): | ||
| adapter = object.__new__(AiocqhttpAdapter) | ||
| adapter.bot = _FakeBot( | ||
| { | ||
| ( | ||
| "get_forward_msg", | ||
| "fwd_1", | ||
| ): { | ||
| "data": { | ||
| "messages": [ | ||
| { | ||
| "sender": {"nickname": "Alice"}, | ||
| "message": [{"type": "text", "data": {"text": "hello"}}], | ||
| }, | ||
| { | ||
| "sender": {"nickname": "Bot", "user_id": 10000}, | ||
| "message": [ | ||
| { | ||
| "type": "text", | ||
| "data": {"text": "bot reply"}, | ||
| } | ||
| ], | ||
| }, | ||
| ] | ||
| } | ||
| }, | ||
| } | ||
| ) | ||
|
|
||
| abm = await adapter._convert_handle_message_event( | ||
| _make_group_event( | ||
| [ | ||
| {"type": "text", "data": {"text": "context"}}, | ||
| {"type": "forward", "data": {"id": "fwd_1"}}, | ||
| ] | ||
| ) | ||
| ) | ||
|
|
||
| assert isinstance(abm.message[0], Plain) | ||
| assert isinstance(abm.message[1], Forward) | ||
| assert isinstance(abm.message[2], Plain) | ||
| assert abm.message[1].id == "fwd_1" | ||
| assert abm.message_str == "context\nAlice: hello\nBot: bot reply" | ||
| assert abm.message[2].text == "Alice: hello\nBot: bot reply" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_aiocqhttp_forward_segment_keeps_placeholder_when_fetch_fails(): | ||
| adapter = object.__new__(AiocqhttpAdapter) | ||
| adapter.bot = _FakeBot({}) | ||
|
|
||
| abm = await adapter._convert_handle_message_event( | ||
| _make_group_event([{"type": "forward", "data": {"id": "missing"}}]) | ||
| ) | ||
|
|
||
| assert len(abm.message) == 1 | ||
| assert isinstance(abm.message[0], Forward) | ||
| assert abm.message[0].id == "missing" | ||
| assert abm.message_str == "[Forward Message]" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the message segment
mdoes not contain thedatakey,m.get("data", {})in line 410 will return{}andforward_idwill beNone. When entering this block, accessingm['data']directly will raise aKeyError, crashing the message conversion loop. Usingm.get('data')avoids this potential crash.