[GH-ISSUE #1990] TelegramBotHandler: API returns error when truncating/splitting in HTML parse mode #853

Open
opened 2026-03-04 03:01:33 +03:00 by kerem · 3 comments
Owner

Originally created by @MacDada on GitHub (Sep 19, 2025).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1990

Monolog 3.9

I set $parseMode to 'HTML' and keep the default $splitLongMessages as false.

When the message is looooong, it gets truncated. If the truncation happens inside HTML tag,
the Telegram API returns error 400:

Bad Request: can't parse entities: Can't find end tag corresponding to start tag "code"

In my example the loooong string has <code>blah blah</code>. Lets say it gets truncated at first blah, then the </code> is gone and API complains.

  • Possible solution 1: would need to fix (actually parse?) HTML if truncating?
  • Possible solution 2: strip HTML tags when truncating? (kinda ugly)
  • Low hanging fruit: at least want about that in the (php) docs?

EDIT: actually, with $splitLongMessages=true same kind of problem – the message splits inside HTML causing Telegram API error(s).

Originally created by @MacDada on GitHub (Sep 19, 2025). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1990 Monolog 3.9 I set [`$parseMode`](https://github.com/Seldaek/monolog/blob/3.9.0/src/Monolog/Handler/TelegramBotHandler.php#L114) to `'HTML'` and keep the default [`$splitLongMessages`](https://github.com/Seldaek/monolog/blob/3.9.0/src/Monolog/Handler/TelegramBotHandler.php#L117) as `false`. When the message is looooong, it [gets truncated](https://github.com/Seldaek/monolog/blob/3.9.0/src/Monolog/Handler/TelegramBotHandler.php#L292). If the truncation happens inside HTML tag, the [Telegram API returns error 400](https://github.com/Seldaek/monolog/blob/3.9.0/src/Monolog/Handler/TelegramBotHandler.php#L280): > Bad Request: can't parse entities: Can't find end tag corresponding to start tag "code" In my example the loooong string has `<code>blah blah</code>`. Lets say it gets truncated at first `blah`, then the `</code>` is gone and API complains. * Possible solution 1: would need to fix (actually parse?) HTML if truncating? * Possible solution 2: strip HTML tags when truncating? (kinda ugly) * Low hanging fruit: at least want about that in the (php) docs? --- EDIT: actually, with `$splitLongMessages=true` same kind of problem – the message splits inside HTML causing Telegram API error(s).
Author
Owner

@akilesh1706 commented on GitHub (Oct 5, 2025):

@MacDada I can work on this issue. Can I get it assigned as part of Hacktober fest 2025?

<!-- gh-comment-id:3368929288 --> @akilesh1706 commented on GitHub (Oct 5, 2025): @MacDada I can work on this issue. Can I get it assigned as part of Hacktober fest 2025?
Author
Owner

@MacDada commented on GitHub (Oct 5, 2025):

Yeah, well… why not… we can compare results as I've actually got splitting (almost) done :D

<!-- gh-comment-id:3368963307 --> @MacDada commented on GitHub (Oct 5, 2025): Yeah, well… why not… we can compare results as I've actually got splitting (almost) done :D
Author
Owner

@Seldaek commented on GitHub (Dec 30, 2025):

PR welcome ;)

<!-- gh-comment-id:3700627519 --> @Seldaek commented on GitHub (Dec 30, 2025): PR welcome ;)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#853
No description provided.