Skip to content
Closed
Changes from all 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
20 changes: 20 additions & 0 deletions astrbot/core/platform/sources/telegram/tg_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ async def convert_message(
message.message.append(
Comp.File(file=file_path, name=file_name, url=file_path)
)
if update.message.caption:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the repeated caption and mention handling into a shared helper used by both the document and video branches to avoid duplication.

You can remove the duplicated caption logic in the document and video branches by extracting it into a small helper. That keeps media-specific handling localized and makes future changes to caption parsing one‑shot.

For example:

def _append_caption_components(message: Message, tg_message: telegram.Message) -> None:
    if tg_message.caption:
        message.message_str = tg_message.caption
        message.message.append(Comp.Plain(message.message_str))

    if tg_message.caption_entities:
        for entity in tg_message.caption_entities:
            if entity.type == "mention":
                name = message.message_str[
                    entity.offset + 1 : entity.offset + entity.length
                ]
                message.message.append(Comp.At(qq=name, name=name))

Then use it in both branches:

if update.message.document:
    # existing document/file handling...
    if file_path is not None:
        message.message.append(Comp.File(file=file_path, name=file_name, url=file_path))
    _append_caption_components(message, update.message)

elif update.message.video:
    # existing video handling...
    if file_path is not None:
        message.message.append(Comp.Video(file=file_path, path=file.file_path))
    _append_caption_components(message, update.message)

This keeps behavior identical but removes duplicated control flow and ensures any future change to caption/mention handling is implemented in a single place.

message.message_str = update.message.caption
message.message.append(Comp.Plain(message.message_str))
if update.message.caption_entities:
for entity in update.message.caption_entities:
if entity.type == "mention":
name = message.message_str[
entity.offset + 1 : entity.offset + entity.length
]
message.message.append(Comp.At(qq=name, name=name))

elif update.message.video:
file = await update.message.video.get_file()
Expand All @@ -497,6 +507,16 @@ async def convert_message(
)
else:
message.message.append(Comp.Video(file=file_path, path=file.file_path))
if update.message.caption:
message.message_str = update.message.caption
message.message.append(Comp.Plain(message.message_str))
if update.message.caption_entities:
for entity in update.message.caption_entities:
if entity.type == "mention":
name = message.message_str[
entity.offset + 1 : entity.offset + entity.length
]
message.message.append(Comp.At(qq=name, name=name))

return message

Expand Down
Loading