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
Telegram\Bot\Exceptions\TelegramOtherException: Bad Request: can't parse entities in message text: Can't find end of the entity starting at byte offset 52 File "app/Console/Commands/SendConfirmationMailCommand.php", line 64, in handle 'text' => '*E-Mail Verification*'. PHP_EOL . 'Nachricht an '.$email->email.' wurde gesendet.'
People can use telegram.me/<bot_username> links or username search to find your bot. Bot usernames always end in ‘bot’ (e.g. @TriviaBot, @GitHub_bot). When added to a group, bots do not receive all messages by default (see Privacy mode). Bots never eat, sleep or complain (unless expressly programmed otherwise).
When these buttons are pressed, Telegram clients will display a confirmation alert that tells the user what's about to happen. 6. BotFather BotFather is the one bot to rule them all. It will help you create new bots and change settings for existing ones. Use the /newbot command to create a new bot.
If you are getting a "Too Many Attempts" error, it means that you have attempted to log in to Telegram with the wrong code multiple times recently. In this case, you should wait for 24 hours before you try to log in again.
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.
This happends when your final string have _
or @
or &
(when the endpoint has some rules with the string).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With