23

I've made a telegram bot which logs critical errors in our telegram chat. This bot has been used in another symfony application (4.4), and worked fine.

But now I'm trying to use it in a Symfony 3.4 project and upon generating an error, telegram responds with:

resulted in a `400 Bad Request` response:
{"ok":false,"error_code":400,"description":"Bad Request: can't parse entities: Can't find end of the entity starting at  (truncated...)

However, changing the parse_mode from Markdown to HTML fixes the issue, but I'm trying to wrap my head around it why this could be.

This is the string I'm trying to send:

$message = "$user just had an error at: $path\n`$error`\n$file:$line";

This is the function which sends the request:

/**
 * @param $method
 * @param $headers
 * @param $body
 * @return mixed|ResponseInterface
 * @throws GuzzleException
 */
public function APIMethod($method, $headers, $body)
{
    $client = new Client();
    $uri = 'https://api.telegram.org/bot' . $this->telegramToken . '/' . $method;

    return $client->request('POST', $uri, [
        'headers' => $headers,
        'form_params' => $body,
    ]);
}

/**
 * @param $telegramId
 * @param $text
 * @return mixed|ResponseInterface
 * @throws GuzzleException
 */
public function sendNotification($telegramId, $text)
{
    try {
        return $this->APImethod('sendMessage', [
            'Content-Type' => 'application/x-www-form-urlencoded',
            'Accept' => 'application/json',
        ], [
            'chat_id' => $telegramId,
            'parse_mode' => 'Markdown',
            'text' => $text,
            'disable_web_page_preview' => true,
        ]);
    } catch (Exception $exception) {
        return $exception->getMessage();
    }
}

Thanks in advance

Regentix
  • 352
  • 1
  • 2
  • 11
  • Almost all special chars must be *escaped* with slashes, see https://core.telegram.org/bots/api#formatting-options under markdown which chars must be escaped ... and escape them. essentially, you could escape just everything... You should also use MarkdownV2 probably, because Markdown is legacy. wouldn't be surprised, if the path/file contains a _ – Jakumi Apr 15 '20 at 09:30
  • You're right, this project is Symfony 3.4 and thus has `/app_dev.php/` in the url. Thanks for pointing this out! – Regentix Apr 15 '20 at 09:54

4 Answers4

46

The problem is very likely the content of one your variables ($user, $path, $file, $line) inside your message, which creates an invalid markdown string. Maybe you have an opening markdown symbol without the corresponding closing one. Like * or _.

If this doesn't help, please post here the exact message, with variables replaced, so we can spot markdown errors.

virtualize
  • 2,287
  • 2
  • 25
  • 36
  • I've also experienced same type error while trying to put `` string into message. Seems like you have to santitise user input strings, before placing them in your message. – chakzefir Mar 30 '23 at 14:20
13

This happends when your final string have _ or @ or & (when the endpoint has some rules with the string).

MichalOravec
  • 1,560
  • 3
  • 5
  • 20
Vítor Moreira
  • 131
  • 1
  • 2
4

If you just want to send plain text and don't need Markdown or HTML, just remove the parse_mode parameter altogether. It will send the message as plain text and you won't have to worry about any special characters (other than URL encoding the message text).

wisbucky
  • 33,218
  • 10
  • 150
  • 101
1

Try using "MarkdownV2" in parse_mode instead of "Markdown". It should work on any telegram api library (also make sure you've closed the special characters too)