141 lines
5.4 KiB
PHP
141 lines
5.4 KiB
PHP
<?php
|
||
require_once __DIR__ . '/../config.php';
|
||
|
||
// Telegram Bot Configuration - Please change these
|
||
define('TELEGRAM_BOT_TOKEN', 'YOUR_TELEGRAM_BOT_TOKEN_HERE');
|
||
define('TELEGRAM_CHAT_ID', 'YOUR_TELEGRAM_CHAT_ID_HERE');
|
||
|
||
// Optionally get date from query param, default to today
|
||
$dateStr = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
|
||
$date = new DateTime($dateStr);
|
||
$year = (int)$date->format('Y');
|
||
$month = (int)$date->format('n');
|
||
$day = (int)$date->format('j');
|
||
|
||
// Thai Date formatting
|
||
$thaiMonths = [
|
||
1 => 'มกราคม', 2 => 'กุมภาพันธ์', 3 => 'มีนาคม', 4 => 'เมษายน',
|
||
5 => 'พฤษภาคม', 6 => 'มิถุนายน', 7 => 'กรกฎาคม', 8 => 'สิงหาคม',
|
||
9 => 'กันยายน', 10 => 'ตุลาคม', 11 => 'พฤศจิกายน', 12 => 'ธันวาคม'
|
||
];
|
||
$thaiDays = [
|
||
'Sunday' => 'อาทิตย์', 'Monday' => 'จันทร์', 'Tuesday' => 'อังคาร',
|
||
'Wednesday' => 'พุธ', 'Thursday' => 'พฤหัสบดี', 'Friday' => 'ศุกร์', 'Saturday' => 'เสาร์'
|
||
];
|
||
|
||
$thaiDateStr = "วัน" . $thaiDays[$date->format('l')] . "ที่ " . $day . " " . $thaiMonths[$month] . " " . ($year + 543);
|
||
|
||
try {
|
||
// Get schedules for the target date
|
||
$stmt = $pdo->prepare("
|
||
SELECT s.shift_type, st.name, st.phone
|
||
FROM schedules s
|
||
JOIN staff st ON s.staff_id = st.id
|
||
WHERE s.year = :year AND s.month = :month AND s.day = :day
|
||
");
|
||
$stmt->execute([':year' => $year, ':month' => $month, ':day' => $day]);
|
||
$schedules = $stmt->fetchAll();
|
||
|
||
if (empty($schedules)) {
|
||
// No schedule for today, maybe send nothing or send a specific message
|
||
$message = "\n📅 <b>ประกาศตารางเวร</b>\n$thaiDateStr\n\n✨ <i>วันนี้ไม่มีผู้เข้าเวรพักผ่อนให้เต็มที่ครับ</i> ✨";
|
||
} else {
|
||
$morning = [];
|
||
$afternoon = [];
|
||
$both = [];
|
||
$leave = [];
|
||
|
||
foreach ($schedules as $row) {
|
||
$nameInfo = $row['name'];
|
||
if (!empty($row['phone'])) {
|
||
$nameInfo .= " (📞 " . $row['phone'] . ")";
|
||
}
|
||
|
||
if ($row['shift_type'] === 'ช') {
|
||
$morning[] = $nameInfo;
|
||
} elseif ($row['shift_type'] === 'บ') {
|
||
$afternoon[] = $nameInfo;
|
||
} elseif ($row['shift_type'] === 'ช/บ') {
|
||
$both[] = $nameInfo;
|
||
} elseif ($row['shift_type'] === 'ลา') {
|
||
$leave[] = $nameInfo;
|
||
}
|
||
}
|
||
|
||
// Build beautiful message using HTML tags for Telegram
|
||
$message = "📢 <b>แจ้งเตือนตารางเวรประจำวัน</b> 📢\n";
|
||
$message .= "🗓️ <b>$thaiDateStr</b>\n";
|
||
$message .= str_repeat("➖", 12) . "\n\n";
|
||
|
||
if (!empty($morning) || !empty($both)) {
|
||
$message .= "🌅 <b>เวรเช้า (08:30 - 16:30 น.)</b>\n";
|
||
foreach ($both as $name) {
|
||
$message .= " 🔹 " . $name . "\n";
|
||
}
|
||
foreach ($morning as $name) {
|
||
$message .= " 🔹 " . $name . "\n";
|
||
}
|
||
$message .= "\n";
|
||
}
|
||
|
||
if (!empty($afternoon) || !empty($both)) {
|
||
$message .= "🌇 <b>เวรบ่าย (16:30 - 00:30 น.)</b>\n";
|
||
foreach ($both as $name) {
|
||
$message .= " 🔸 " . $name . "\n";
|
||
}
|
||
foreach ($afternoon as $name) {
|
||
$message .= " 🔸 " . $name . "\n";
|
||
}
|
||
$message .= "\n";
|
||
}
|
||
|
||
if (!empty($leave)) {
|
||
$message .= "⛔ <b>ลาพัก/ลากิจ</b>\n";
|
||
foreach ($leave as $name) {
|
||
$message .= " ❌ " . $name . "\n";
|
||
}
|
||
$message .= "\n";
|
||
}
|
||
|
||
$message .= "💡 <i>ศูนย์คอมพิวเตอร์ ฝ่ายคอมพิวเตอร์และสารสนเทศ</i>";
|
||
}
|
||
|
||
// Only send to Telegram if token is set and not default
|
||
if (TELEGRAM_BOT_TOKEN !== 'YOUR_TELEGRAM_BOT_TOKEN_HERE' && !empty(TELEGRAM_BOT_TOKEN)) {
|
||
$url = "https://api.telegram.org/bot" . TELEGRAM_BOT_TOKEN . "/sendMessage";
|
||
|
||
$postData = [
|
||
'chat_id' => TELEGRAM_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($postData));
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||
$result = curl_exec($ch);
|
||
curl_close($ch);
|
||
|
||
echo json_encode([
|
||
'status' => 'success',
|
||
'message' => 'Notification sent successfully',
|
||
'response' => json_decode($result),
|
||
'text' => $message // For preview
|
||
]);
|
||
} else {
|
||
echo json_encode([
|
||
'status' => 'success',
|
||
'message' => 'Preview mode (No Telegram Token)',
|
||
'text' => $message // For preview
|
||
]);
|
||
}
|
||
|
||
} catch (PDOException $e) {
|
||
http_response_code(500);
|
||
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||
}
|
||
?>
|