Files
gravity/บริหารจัดการงานเปล/models/Notification.php
T
2026-09-16 23:20:08 +07:00

79 lines
2.4 KiB
PHP

<?php
// models/Notification.php
require_once __DIR__ . '/Setting.php';
class Notification {
private $settingModel;
private $lineToken;
private $telegramToken;
private $telegramChatId;
public function __construct() {
$this->settingModel = new Setting();
$settings = $this->settingModel->getAllFlat();
$this->lineToken = $settings['line_notify_token'] ?? null;
$this->telegramToken = $settings['telegram_bot_token'] ?? null;
$this->telegramChatId = $settings['telegram_chat_id'] ?? null;
}
/**
* Send a simple text message to all configured platforms
*/
public function sendToAll($message) {
if (!empty($this->lineToken)) {
$this->sendToLine($message);
}
if (!empty($this->telegramToken) && !empty($this->telegramChatId)) {
$this->sendToTelegram($message);
}
}
/**
* Send message via LINE Notify
*/
private function sendToLine($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(['message' => "\n" . $message]));
$headers = [
'Content-type: application/x-www-form-urlencoded',
'Authorization: Bearer ' . $this->lineToken,
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Send message via Telegram Bot
*/
private function sendToTelegram($message) {
$url = "https://api.telegram.org/bot" . $this->telegramToken . "/sendMessage";
$data = [
'chat_id' => $this->telegramChatId,
'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);
curl_close($ch);
return $result;
}
}