Developing a Telegram Bot for Critical Site Event Notifications
Monitoring critical events via Telegram is a quick way to get alerts about website problems on mobile phone, bypassing email and monitoring systems. Bot notifies: site went down, payment error, disk full, 500 errors appeared.
Event Types
// List of critical events with importance levels
enum AlertLevel: string {
case CRITICAL = '🔴'; // requires immediate action
case WARNING = '🟡'; // requires attention
case INFO = '🔵'; // informational
}
class SiteEventAlerter
{
public function alert(AlertLevel $level, string $event, array $context = []): void
{
$message = "{$level->value} <b>{$event}</b>\n\n";
foreach ($context as $key => $value) {
$message .= "<b>{$key}:</b> {$value}\n";
}
$message .= "\n⏰ " . now()->format('d.m.Y H:i:s');
// Critical events — personal messages to on-call engineer
$recipients = $level === AlertLevel::CRITICAL
? $this->getOnCallEngineers()
: [$this->alertsChannelId];
foreach ($recipients as $chatId) {
$this->telegram->sendMessage($chatId, $message);
}
}
}
Integration into Code
// In exception handler (Handler.php)
public function report(Throwable $exception): void
{
if ($exception instanceof PaymentException) {
app(SiteEventAlerter::class)->alert(
AlertLevel::CRITICAL,
'Payment gateway error',
[
'Gateway' => $exception->getGateway(),
'Order' => $exception->getOrderId(),
'Error' => $exception->getMessage(),
]
);
}
parent::report($exception);
}
// In scheduler (Kernel.php)
$schedule->call(function () {
$freeSpace = disk_free_space('/') / disk_total_space('/') * 100;
if ($freeSpace < 10) {
app(SiteEventAlerter::class)->alert(
AlertLevel::WARNING,
'Low disk space',
['Free' => round($freeSpace, 1) . '%']
);
}
})->hourly();
Anti-Spam
Same error can generate hundreds of alerts per minute. Deduplication via Redis:
private function shouldSend(string $eventKey): bool
{
$cacheKey = "alert_dedup:{$eventKey}";
if (Cache::has($cacheKey)) return false;
Cache::put($cacheKey, 1, now()->addMinutes(15));
return true;
}
Timeline: 1–2 business days.







