Telegram bot Can't find end of the entity starting at (truncated...)

Telegram bot Can't find end of the entity starting at (truncated...)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing Entities in Telegram Bots: A Symfony Compatibility Pitfall

A developer recently encountered a 400 Bad Request error when deploying a critical error logging Telegram bot to a Symfony 3.4 project. The application had previously functioned perfectly within a Symfony 4.4 environment, yet switching the framework version caused Telegram's API to reject the message with the cryptic description: "Can't find end of the entity starting at".

The root cause lies in how Telegram interprets parse_mode and the specific syntax used for code blocks. The original implementation attempted to send a string containing backticks (`$error`) using Markdown parsing. In Markdown, backticks define inline code fences. However, Telegram's parser has strict requirements regarding whitespace. If the text immediately surrounding the opening backtick does not contain at least one non-whitespace character before it, or if the closing sequence is ambiguous due to formatting edge cases, the parser fails to identify where the entity ends.

In this specific scenario, the message string $user just had an error at: $path\n$error\n$file:$line places the opening backtick directly after a newline character. While human readers distinguish code blocks easily, the Markdown parser struggles when the preceding content consists solely of whitespace or line breaks before the fence begins. This ambiguity leads to the "Can't find end of the entity" crash because the parser cannot definitively locate the matching closing boundary without a clear non-whitespace trigger.

The immediate fix demonstrated in the source material involves switching the parse_mode from Markdown to HTML. Telegram's HTML parsing engine is significantly more lenient and predictable for developers. It treats backticks as plain text characters rather than special delimiters, allowing the message to render exactly as intended without triggering entity parsing errors.

'parse_mode' => 'HTML', // Changed from Markdown
'text' => $text,

While Markdown offers a cleaner syntax for many use cases—especially when formatting bold or italic text alongside code—it introduces fragility when dealing with complex strings containing backticks, newlines, or specific spacing rules. HTML mode bypasses these syntactic ambiguities entirely. For instance, if you need to render code blocks in HTML mode, Telegram accepts standard backtick syntax without requiring the strict whitespace adherence that Markdown demands.

This discrepancy highlights a broader lesson about framework versioning and third-party API compatibility. Symfony 3.4 might handle string escaping or entity encoding differently than Symfony 4.4, but in this case, the issue was not with the framework itself but with how the raw string data interacted with Telegram's parser. The underlying logic remained consistent; only the parsing rules changed based on the selected mode.

When integrating bots into production systems, relying on Markdown for all text formatting is often a mistake if your error logs or dynamic content contain code snippets. It is safer to default to HTML parsing when the payload includes backticks, URLs, or mixed formatting elements. This approach ensures that the bot remains robust against edge cases where Markdown's strict syntax requirements would otherwise cause the connection to fail.

If you are maintaining a legacy system like Symfony 3.4 and cannot upgrade immediately, adopting HTML parsing mode is a reliable workaround for these entity parsing issues. However, if you have the flexibility to refactor, consider sanitizing your input strings before sending them via Markdown or using HTML as the standard default for any bot handling dynamic error logs or user-generated content.

Our network