为什么机器人在复制时无法保留文本格式?

问题描述 投票:0回答:1

我遇到了问题。我的电报机器人在发送时无法保留文本格式。假设,如果源通道中的文本包含引号、带下划线或使用 ||Spoiler|| 隐藏,则文本到达目标通道时不会格式化。但是,如果文本为删除线、粗体或斜体,则格式会保留。我尝试在提交之前将文本转换为 markdownV2HTML,但没有任何效果。

为什么机器人可以复制某些格式样式而不能复制其他样式?

我很高兴有任何想法。

async def send_media(message, destination_channel_id):
    try:
        if message.media and isinstance(message.media, (MessageMediaPhoto, MessageMediaDocument)):
            return await client.send_message(destination_channel_id, message.text, file=message.media)
        else:
            return await client.send_message(destination_channel_id, message.text, parse_mode='MarkdownV2')
    except MediaCaptionTooLongError:
        print("Message text is too long.")
    except Exception as e:
        print(f"Error: {e}")


@client.on(events.NewMessage())
async def my_event_handler(event):

    source_channels = await get_source_channels_ids()
    destination_channels = await get_destination_channels_ids()

    if event.chat_id in source_channels:
        if event.message.grouped_id:
            return

        original_text = event.message.text
        updated_text = replace_link(replace_at_word(original_text, replaceable_link), new_link)

        for destination_channel_id in destination_channels:
            if event.message.media:
                await client.send_file(destination_channel_id, event.message.media, caption=updated_text)
            else:
                await client.send_message(destination_channel_id, updated_text)
python html markdown telegram-bot telethon
1个回答
0
投票

我在 Telethon 库的文档中找到了答案。也许这对某人有用。 文档 第19页

2.3.2 格式化消息

该库支持3种格式化模式:无格式化、CommonMark、 HTML。 Telegram 本身不支持 Markdown 或 HTML。

诸如 Telethon 之类的客户端将文本解析为格式列表 不同偏移量的 MessageEntity。

请注意,CommonMark 的 markdown 与 HTTP Bot 并不完全兼容 API的MarkdownV2风格,不支持剧透:

*italic* and _italic_
**bold** and __bold__
# headings are underlined
~~strikethrough~~
[inline URL](https://www.example.com/)
[inline mention](tg://user?id=ab1234cd6789)
custom emoji image with ![](tg://emoji?id=1234567890)
`inline code`
```python
multiline pre-formatted
block with optional language
© www.soinside.com 2019 - 2024. All rights reserved.