Files
2026-09-16 23:20:08 +07:00

123 lines
5.7 KiB
PHP

<?php
// This script is meant to be run via Cron Job daily (e.g., at 08:00 AM)
// It checks for pending assignments close to the deadline and sends notifications.
require_once 'config.php';
// Prevent rendering layout if accessed via web (can be protected with a key in production)
echo "<h1>ระบบแจ้งเตือนอัตโนมัติ (Cron Job)</h1>";
echo "<pre>";
// Get settings
$days_before_due = getSetting($pdo, 'days_before_due') ?: 3;
$telegram_bot_token = getSetting($pdo, 'telegram_bot_token');
$telegram_group_chat_id = getSetting($pdo, 'telegram_group_chat_id');
$line_notify_token = getSetting($pdo, 'line_notify_token');
$notify_via_line = getSetting($pdo, 'notify_via_line') !== '0';
$notify_via_telegram = getSetting($pdo, 'notify_via_telegram') !== '0';
// Function to send 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 = array('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);
curl_close($ch);
return json_decode($result, true);
}
// Function to send Telegram Message
function sendTelegramMessage($botToken, $chatId, $message) {
if (empty($botToken) || empty($chatId)) return false;
$url = "https://api.telegram.org/bot{$botToken}/sendMessage";
$data = [
'chat_id' => $chatId,
'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);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Fetch pending assignments that need notification
// We don't need line_token or telegram_chat_id from users anymore for external notification.
$sql = "SELECT a.id, a.title, a.due_date, u.name, DATEDIFF(a.due_date, CURDATE()) as days_left
FROM assignments a
JOIN users u ON a.student_id = u.id
WHERE a.status = 'pending'
AND DATEDIFF(a.due_date, CURDATE()) = :days_before_due";
$stmt = $pdo->prepare($sql);
$stmt->execute([':days_before_due' => $days_before_due]);
$assignments = $stmt->fetchAll();
echo "พบงานที่ต้องแจ้งเตือน: " . count($assignments) . " รายการ (เหลือเวลา $days_before_due วัน)\n\n";
$success_line = 0;
$success_tg = 0;
foreach ($assignments as $task) {
// Send via LINE (GROUP ONLY)
if ($notify_via_line && !empty($line_notify_token)) {
$message = "⚠️ แจ้งเตือนกำหนดส่งงาน ⚠️\n";
$message .= "เรียนคุณ: {$task['name']}\n";
$message .= "งาน: {$task['title']}\n";
$message .= "กำหนดส่ง: " . date('d/m/Y', strtotime($task['due_date'])) . "\n";
$message .= "สถานะ: เหลือเวลาอีก {$task['days_left']} วัน!\n";
$message .= "กรุณาตรวจสอบระบบ";
$res = sendLineNotify($line_notify_token, $message);
if (isset($res['status']) && $res['status'] == 200) {
$success_line++;
echo "✅ LINE Group: แจ้งเตือน '{$task['title']}' สำหรับ '{$task['name']}' สำเร็จ\n";
} else {
echo "❌ LINE Group: แจ้งเตือน '{$task['title']}' สำหรับ '{$task['name']}' ล้มเหลว\n";
}
}
// Send via Telegram (GROUP ONLY)
if ($notify_via_telegram && !empty($telegram_bot_token) && !empty($telegram_group_chat_id)) {
$tgMessage = "⚠️ <b>แจ้งเตือนกำหนดส่งงาน</b> ⚠️\n\n";
$tgMessage .= "👤 เรียนคุณ: {$task['name']}\n";
$tgMessage .= "📝 งาน: <b>{$task['title']}</b>\n";
$tgMessage .= "📅 กำหนดส่ง: " . date('d/m/Y', strtotime($task['due_date'])) . "\n";
$tgMessage .= "⏳ สถานะ: <b>เหลือเวลาอีก {$task['days_left']} วัน!</b>\n";
$res = sendTelegramMessage($telegram_bot_token, $telegram_group_chat_id, $tgMessage);
if (isset($res['ok']) && $res['ok'] == 1) {
$success_tg++;
echo "✅ Telegram Group: แจ้งเตือน '{$task['title']}' สำหรับ '{$task['name']}' สำเร็จ\n";
} else {
echo "❌ Telegram Group: แจ้งเตือน '{$task['title']}' สำหรับ '{$task['name']}' ล้มเหลว\n";
}
}
}
// Fetch OVERDUE assignments (optional, just to show how it could be done)
$sql_overdue = "SELECT a.id, a.title, u.name, u.line_token, DATEDIFF(CURDATE(), a.due_date) as days_overdue
FROM assignments a JOIN users u ON a.student_id = u.id
WHERE a.status = 'pending' AND a.due_date < CURDATE()";
// ... execution skipped to keep output clean, but can be added
echo "\n--- สรุปผลการแจ้งเตือน ---\n";
echo "LINE: ส่งสำเร็จ $success_line รายการ\n";
echo "Telegram: ส่งสำเร็จ $success_tg รายการ\n";
echo "</pre>";
?>