113 lines
3.3 KiB
PHP
113 lines
3.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db.php';
|
|
|
|
/**
|
|
* Send message via LINE Notify
|
|
*/
|
|
function sendLineNotify($token, $message) {
|
|
if (empty($token)) return false;
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, "https://notify-api.line.me/api/notify");
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, "message=" . urlencode($message));
|
|
$headers = [
|
|
'Content-type: application/x-www-form-urlencoded',
|
|
'Authorization: Bearer ' . $token,
|
|
];
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$result = curl_exec($ch);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($error) {
|
|
return $error;
|
|
}
|
|
|
|
$res = json_decode($result, true);
|
|
if ($res && isset($res['status']) && $res['status'] == 200) {
|
|
return true;
|
|
}
|
|
return $res['message'] ?? 'Unknown error';
|
|
}
|
|
|
|
/**
|
|
* Send message via Telegram Bot API
|
|
*/
|
|
function sendTelegramMessage($token, $chat_id, $message) {
|
|
if (empty($token) || empty($chat_id)) return false;
|
|
|
|
$url = "https://api.telegram.org/bot{$token}/sendMessage";
|
|
$data = [
|
|
'chat_id' => $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());
|
|
}
|
|
}
|