I wanted it to be automatically copied after sending this message when clicking on context.user_data["code"] So I used “ and parse_mode="MarkdownV2"
await context.bot.send_message(
chat_id=CHANEL_ID,
text= f"{context.user_data['key']}nnCode: `{context.user_data['code']}`nnMod: {context.user_data['mod']}nnby: @{update.effective_user.username}",
parse_mode='MarkdownV2'
)
But if username of the user has _ like: @Jason_99, I get the following error. how to fix it?
telegram.error.BadRequest: Can't parse entities: can't find end of italic entity at byte offset 53
I tried to apply pars_mode to only part of the text. But I don’t know if it is possible or not. I did not find a way
2
Answers
You’ll need to clean the username so there are no un-closed markdown entries.
I’d replace
_
with-
like so:And then use it in your message:
The proper way to include a link to a user is to make a markdown link to the user ID:
In Addition to 0stone0’s answer let me add, that
python-telegram-bot
comes with built-in functionality to escape text for markup processing:escape_markdown
. That way you can usewhich has two benefits:
Disclaimer: I’m currently the maintainer of
python-telegram-bot