feat: preserve message entities as markdown#63
Open
ohld wants to merge 2 commits intochigwell:mainfrom
Open
Conversation
Previously all message retrieval functions used msg.message (plain text), losing formatting entities. Now message_to_markdown() converts entities to markdown: **bold**, _italic_, ~~strike~~, `code`, [text](url). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Telegram entities use UTF-16 code units for offsets, but Python strings use Unicode code points. Emoji characters (outside BMP) take 2 UTF-16 units but 1 Python char, causing entity boundaries to shift mid-word. Added _utf16_to_python_offsets() to correctly convert between the two. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
message_to_markdown()function that converts Telethon message entities to markdown formatWhat changed
Previously, all message retrieval functions (
get_messages,list_messages,search_messages,get_history,get_pinned_messages,get_chat,get_last_interaction,get_message_context,format_message) usedmsg.messagewhich returns plain text only. All Telegram formatting entities were lost:MessageEntityTextUrl) — visible text returned, URL lost entirelyMessageEntityBold) — formatting strippedMessageEntityItalic) — formatting strippedNow entities are converted to markdown:
MessageEntityBold→**text**MessageEntityItalic→_text_MessageEntityStrike→~~text~~MessageEntityCode→`text`MessageEntityPre→```lang\ntext\n```MessageEntityTextUrl→[text](url)MessageEntityMentionName→[text](tg://user?id=X)MessageEntityUrl,MessageEntityMention,MessageEntityHashtag— kept as-is (already visible in text)UTF-16 fix
Telegram entities use UTF-16 code units for offsets. Characters outside the BMP (most emoji like 👨, 🔗, etc.) take 2 UTF-16 code units but only 1 Python character. Without conversion, entity boundaries shift and formatting appears mid-word (e.g.,
с**вязка**instead of**связка**).Added
_utf16_to_python_offsets()to correctly convert between the two coordinate systems.Test plan
🤖 Generated with Claude Code