How can I find out that the bot is blocked? #75
-
For the second day I've been scratching my head, can I find out that the bot is blocked? public function toTelegram($notifiable)
{
$chatId = $this->chatId;
$message = $this->message;
$bot = new TelegramNotification();
try {
$response = $bot->sendMessage([
'chat_id' => $chatId,
'text' => $message,
'parse_mode' => 'HTML',
'exceptions' => true,
]);
return $response;
} catch (TeleBotException $e) {
if ($e->getCode() == 403 && strpos($e->getMessage(), 'bot was blocked by the user') !== false) {
// Действия при блокировке бота пользователем
Log::error('Бот заблокирован пользователем.');
} else {
// Другие действия при других ошибках Telegram
Log::error('Ошибка отправки сообщения в Telegram: ' . $e->getMessage());
}
} catch (Exception $e) {
// Действия при других исключениях
Log::error('Ошибка отправки сообщения: ' . $e->getMessage());
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The reason you don't get an exception there is because telebot/src/Laravel/Notifications/TelegramChannel.php Lines 27 to 58 in c00ad32 If you want to handle some logic if user notification failed - you need to create event listener for |
Beta Was this translation helpful? Give feedback.
The reason you don't get an exception there is because
TelegramNotification
class is only creating a dataset for future notification, so there are no errors yet. Sending of notification happens internallytelebot/src/Laravel/Notifications/TelegramChannel.php
Lines 27 to 58 in c00ad32