108 lines
4.8 KiB
PHP
108 lines
4.8 KiB
PHP
<?php
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 0); // Don't print errors to output directly to not break JSON
|
|
|
|
require_once '../config.php';
|
|
header('Content-Type: application/json');
|
|
|
|
if (!function_exists('curl_init')) {
|
|
echo json_encode(['status' => 'error', 'message' => 'เซิร์ฟเวอร์ของคุณไม่รองรับ cURL (cURL extension is missing)']);
|
|
exit;
|
|
}
|
|
|
|
if (!isLoggedIn() || !isAdmin()) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Unauthorized access']);
|
|
exit;
|
|
}
|
|
|
|
$type = $_POST['type'] ?? '';
|
|
|
|
if ($type === 'line') {
|
|
$token = $_POST['token'] ?? '';
|
|
if (empty($token)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'กรุณากรอก Token ก่อนทดสอบ']);
|
|
exit;
|
|
}
|
|
|
|
$tomorrow = date('d/m/Y', strtotime('+1 day'));
|
|
$message = "⚠️ แจ้งเตือนกำหนดส่งงาน ⚠️\n";
|
|
$message .= "เรียนคุณ: นักเรียนทดสอบ (Test)\n";
|
|
$message .= "งาน: ทดสอบระบบแจ้งเตือน\n";
|
|
$message .= "กำหนดส่ง: {$tomorrow}\n";
|
|
$message .= "สถานะ: เหลือเวลาอีก 1 วัน!\n";
|
|
$message .= "กรุณาตรวจสอบระบบ";
|
|
|
|
$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, http_build_query(array('message' => $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);
|
|
$error_msg = curl_error($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($result === false) {
|
|
echo json_encode(['status' => 'error', 'message' => 'cURL Error: ' . $error_msg]);
|
|
} elseif ($httpCode == 200) {
|
|
echo json_encode(['status' => 'success', 'message' => 'ส่งข้อความทดสอบ LINE สำเร็จ']);
|
|
} else {
|
|
$resObj = json_decode($result, true);
|
|
$reason = isset($resObj['message']) ? $resObj['message'] : 'HTTP Code ' . $httpCode;
|
|
echo json_encode(['status' => 'error', 'message' => 'ไม่สามารถส่งข้อความได้: ' . $reason]);
|
|
}
|
|
} elseif ($type === 'telegram') {
|
|
$bot_token = $_POST['bot_token'] ?? '';
|
|
$chat_id = $_POST['chat_id'] ?? '';
|
|
|
|
if (empty($bot_token) || empty($chat_id)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'กรุณากรอก Bot Token และ Chat ID ก่อนทดสอบ']);
|
|
exit;
|
|
}
|
|
|
|
$tomorrow = date('d/m/Y', strtotime('+1 day'));
|
|
$message = "⚠️ <b>แจ้งเตือนกำหนดส่งงาน (Test)</b> ⚠️\n\n";
|
|
$message .= "👤 เรียนคุณ: นักเรียนทดสอบ\n";
|
|
$message .= "📝 งาน: <b>ทดสอบระบบการแจ้งเตือน</b>\n";
|
|
$message .= "📅 กำหนดส่ง: {$tomorrow}\n";
|
|
$message .= "⏳ สถานะ: <b>เหลือเวลาอีก 1 วัน!</b>\n";
|
|
|
|
$url = "https://api.telegram.org/bot{$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_SSL_VERIFYHOST, 0);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
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);
|
|
$error_msg = curl_error($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($result === false) {
|
|
echo json_encode(['status' => 'error', 'message' => 'cURL Error: ' . $error_msg]);
|
|
} elseif ($httpCode == 200) {
|
|
echo json_encode(['status' => 'success', 'message' => 'ส่งข้อความทดสอบ Telegram สำเร็จ']);
|
|
} else {
|
|
$resObj = json_decode($result, true);
|
|
$reason = isset($resObj['description']) ? $resObj['description'] : 'HTTP Code ' . $httpCode;
|
|
echo json_encode(['status' => 'error', 'message' => 'ไม่สามารถส่งข้อความได้: ' . $reason]);
|
|
}
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid request type']);
|
|
}
|
|
?>
|