$chat_id, 'text' => $message, 'parse_mode' => 'HTML' ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if ($error) { return $error; } $res = json_decode($result, true); if ($res && isset($res['ok']) && $res['ok'] === true) { return true; } return $res['description'] ?? 'Unknown error'; } /** * Main function to send notifications based on event settings * * @param string $eventType e.g., 'vehicle', 'driver', 'schedule', 'maintenance' * @param string $message The message to send */ function notifyEvent($eventType, $message) { global $pdo; try { // Fetch settings $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings"); $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); $lineToken = $settings['line_notify_token'] ?? ''; $telegramToken = $settings['telegram_bot_token'] ?? ''; $telegramChatId = $settings['telegram_chat_id'] ?? ''; $notifyEventsStr = $settings['notify_events'] ?? '[]'; $notifyEvents = json_decode($notifyEventsStr, true) ?: []; // Check if this event type is enabled if (in_array($eventType, $notifyEvents)) { // Send LINE if (!empty($lineToken)) { sendLineNotify($lineToken, $message); } // Send Telegram if (!empty($telegramToken) && !empty($telegramChatId)) { sendTelegramMessage($telegramToken, $telegramChatId, $message); } } } catch (Exception $e) { // Silently fail for notifications so it doesn't break main app flow error_log("Notification Error: " . $e->getMessage()); } }