Repository navigation
🐛 parse received rich text and replied messages - #181
Merged
Merged
Conversation
A received `post` was kept as one `Post` segment holding the raw content. Its `at` nodes carry mention keys such as `@_user_1` instead of open_ids, so `_check_at_me` never found the leading @, commands in rich text never matched, and plugins could not see the text, mentions or images inside. Received rich text now becomes the same segments as other messages: `text`, `at` (resolved to the open_id through `mentions`), `image` and `media`, with other nodes such as `a`, `code_block` and `emotion` kept as generic segments. Paragraphs are joined with newlines and the title becomes the first line. `a` and `code_block` count as text, so their text is in the plain text. `Message.serialize` already turns these segments back into a post.
`event.reply` only gave the raw JSON in `body.content`, so the replied message had to be parsed by hand. `Message.deserialize` could not help, because the get message API returns mentions whose `id` is a single string, while it only accepted the `UserId` object from events. `deserialize` now accepts both kinds of mentions, and `Reply.get_message()` parses the replied message by its type, with mentions resolved like in events.
Contributor
|
🚀 Deployed to https://deploy-preview-181--adapter-feishu.netlify.app |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #181 +/- ##
==========================================
+ Coverage 89.19% 90.07% +0.87%
==========================================
Files 11 11
Lines 1499 1531 +32
==========================================
+ Hits 1337 1379 +42
+ Misses 162 152 -10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Received rich text (
post) and replied messages were hard to use:poststayed onePostsegment holding the raw content. Itsatnodes carry mention keys such as@_user_1rather than open_ids, as the documentation shows._check_at_menever saw the leading @, so@bot /cmdsent as rich text, for example with an image attached, never matched a command, andextract_plain_text()returned@@_user_1 /cmd …. Plugins could not see the text, mentions or images inside either.event.replyonly had the raw JSON inbody.content.Message.deserializecould not parse it with its mentions, because the get message API returns each mentionidas a single string, while events give aUserIdobject.Changes
Received rich text
deserializenow expands a received post into the same segments as other messages:texttext, withstylekept when setatat, withuser_idresolved throughmentions; a key without a mention is kept as it isimgimagemediamediaa,code_block,emotion,hr, …)Paragraphs are joined with
\n, and the title becomes the first line. Adjacent plain text is merged, and styled text stays separate. Emptystylelists, which Feishu sends on most nodes, are dropped.As a result:
_check_at_mestrips the leading @ and setsto_me, as it does for text messages.aandcode_blockcount as text, soextract_plain_text()includes link text and code.Message.serialize()already combines these segments into a post, so a received message can be sent again. Mentions are sent with the resolved open_id instead of the@_user_1key, which only has a meaning inside the received message.Behavior change: a received post is no longer a single
Postsegment. Code that readevent.get_message()[0].data["content"]has to use the segments, or the rawevent.event.message.content. Sending aPostbuilt withMessageSegment.post()is unchanged.Replied messages
Message.deserializeaccepts bothMentionfrom events andReplyMentionfrom the get message API.Reply.get_message()parsesevent.replyby itsmsg_type. Mentions resolve to the ids that the API returned. These are open_ids, because_check_replydoes not passuser_id_type.Testing
atnodes with@_user_Nkeys, and emptystylelists.test_check_tome_in_postruns_check_at_meon a@bot /say …post with an image.pytestpasses on Python 3.11 and 3.15, with 149 tests. Ruff passes, and Pyright reports nothing new.@bot /cmd …post with an image now becomesTextandImageinstead of a singleHyper.This was not tested with messages from a real Feishu client. The received structures come from the documentation only.
A follow-up in nonebot-plugin-alconna can switch its Feishu builder to
event.reply.get_message(), instead of passingbody.contentas the reply message.