"telegram.error.BadRequest: Entities_too_long" error when trying to send long markdown message. What is the max string length limit?
Asked Answered
B

2

5

This is the error i get when i try to send a long markdown message which has a ton of formatted hyperlinks in it:

context.bot.send_message(chat_id=update.effective_chat.id, text=longmsg, parse_mode="MARKDOWN")

#Output:
telegram.error.BadRequest: Entities_too_long

The actual message is hardly 500 characters long, but almost every word is hyperlinked which is making the string longmsg very long (>12k string length)

Now, I know that for normal messages 4096 characters is the limit, but when sending markdown messages, the Telegram API seems to be allowing > 4096 string length. So what is the exact string length limit for markdown messages? I'm using the python-telegram-bot library.

Blackfoot answered 13/8, 2021 at 7:3 Comment(2)
Are you sure you need that many characters in links? It seems that for every text character, you have 24 link characters -- for a 5-letter word, you have a link of 116 symbols in length. If you absolutely must have that many links, maybe you can use a URL shortener -- either external or self-hosted?Pacificia
That's ok, i'll just split the messages & send them to the user. I ran some tests and found out the str len to be capped at 9500 chars.Blackfoot
E
2

This limit seems to be undocumented. I only know that TG only parses 100 entities per message and just drops any further entities (which is also undocumented). You can try to contact TG about it via one of the following channels:

Just to clarify: This is not an issue of the library you're using (here python-telegram-bot) - you'd get the same error when making the request manually

Eveleen answered 13/8, 2021 at 7:30 Comment(1)
Thanks for your answer, i'll raise an issue on Github as i too couldn't find the str len limit in documentation. Also, i ran a brute force test and found out that 9500 is the str len limit.Blackfoot
B
10

9500 characters is the string length limit for sending markdown messages via send_message, found out using a brute force test.

Blackfoot answered 13/8, 2021 at 17:50 Comment(0)
E
2

This limit seems to be undocumented. I only know that TG only parses 100 entities per message and just drops any further entities (which is also undocumented). You can try to contact TG about it via one of the following channels:

Just to clarify: This is not an issue of the library you're using (here python-telegram-bot) - you'd get the same error when making the request manually

Eveleen answered 13/8, 2021 at 7:30 Comment(1)
Thanks for your answer, i'll raise an issue on Github as i too couldn't find the str len limit in documentation. Also, i ran a brute force test and found out that 9500 is the str len limit.Blackfoot

© 2022 - 2024 — McMap. All rights reserved.