Python Telegram Bot 中的解析模式

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

我希望在单击 context.user_data["code"] 时发送此消息后自动复制它,所以我使用了 `` 和 parse_mode="MarkdownV2"

await context.bot.send_message(
    chat_id=CHANEL_ID, 
    text= f"{context.user_data['key']}\n\nCode:  `{context.user_data['code']}`\n\nMod:  {context.user_data['mod']}\n\nby: @{update.effective_user.username}",
    parse_mode='MarkdownV2'
    )

但是如果用户的用户名有_,例如:@Jason_99,我会收到以下错误。如何修复它?

telegram.error.BadRequest: Can't parse entities: can't find end of italic entity at byte offset 53

我尝试仅将 pars_mode 应用于部分文本。但我不知道这是否可能。没找到方法

python telegram telegram-bot python-telegram-bot
1个回答
0
投票

您需要清理用户名,以便不存在未关闭的 Markdown 条目。

我会将

_
替换为
-
,如下所示:

clean_username = update.effective_user.username.replace('_', '-')

然后在您的消息中使用它:

text= f"{context.user_data['key']}\n\nCode:  `{context.user_data['code']}`\n\nMod:  {context.user_data['mod']}\n\nby: @{clean_username}"
© www.soinside.com 2019 - 2024. All rights reserved.