From 9aca7a0aa37e1ac2d747e858ead7674973985a6b Mon Sep 17 00:00:00 2001 From: "Ilham D. Sofyan" Date: Fri, 29 Nov 2024 17:44:34 +0700 Subject: [PATCH] Auth telegram dari env --- application/core/MY_Exceptions.php | 10 +++++-- application/core/MY_Log.php | 46 ++++++++++++++++++++++++++++++ env.example.php | 4 +++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 application/core/MY_Log.php diff --git a/application/core/MY_Exceptions.php b/application/core/MY_Exceptions.php index 4a4f2e3..d6c447c 100644 --- a/application/core/MY_Exceptions.php +++ b/application/core/MY_Exceptions.php @@ -190,9 +190,13 @@ class MY_Exceptions extends CI_Exceptions // Function helper buat kirim ke Telegram protected function sendToTelegramChannel($message) { - $bot_token = '8003350874:AAElg1OOqTiyIldyDEYPzvDXDrQfJ-i7iuo'; - $chat_id = '-1002435267790'; // atau ganti sama chat_id kalau channel private - $url = "https://api.telegram.org/bot{$bot_token}/sendMessage"; + $bot_token = defined('TELEGRAM_TOKEN') ? TELEGRAM_TOKEN : ''; + $chat_id = defined('TELEGRAM_CHANNEL_ID') ? TELEGRAM_CHANNEL_ID : ''; + $url = defined('TELEGRAM_URL') ? TELEGRAM_URL : ''; + + if (!$bot_token || !$chat_id || !$url) { + return; + } $data = [ 'chat_id' => $chat_id, diff --git a/application/core/MY_Log.php b/application/core/MY_Log.php new file mode 100644 index 0000000..66201d5 --- /dev/null +++ b/application/core/MY_Log.php @@ -0,0 +1,46 @@ +_date_fmt); + $message = $this->_format_line($level, $date, $msg); + + if ($level == 'error') { + $this->sendToTelegramChannel($message); + } + + parent::write_log($level, $msg); + } + + // Function helper buat kirim ke Telegram + protected function sendToTelegramChannel($message) { + $bot_token = defined('TELEGRAM_TOKEN') ? TELEGRAM_TOKEN : ''; + $chat_id = defined('TELEGRAM_CHANNEL_ID') ? TELEGRAM_CHANNEL_ID : ''; + $url = defined('TELEGRAM_URL') ? TELEGRAM_URL : ''; + + if (!$bot_token || !$chat_id || !$url) { + return; + } + + $data = [ + 'chat_id' => $chat_id, + 'text' => $message, + 'parse_mode' => 'HTML', + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + curl_close($ch); + + return json_decode($response, true); + } +} diff --git a/env.example.php b/env.example.php index 916a284..6c5cd7d 100644 --- a/env.example.php +++ b/env.example.php @@ -68,3 +68,7 @@ define("SSO_CLIENT_SECRET", ''); define("ALGOLIA_CLIENT", ''); define("ALGOLIA_WRITE_SECRET", ''); define("ALGOLIA_READ_SECRET", ''); + +define("TELEGRAM_TOKEN", 'BOT-TOKEN'); +define("TELEGRAM_CHANNEL_ID", 'CHANNEL-ID'); +define("TELEGRAM_URL", 'https://api.telegram.org/bot'. TELEGRAM_TOKEN .'/sendMessage'); -- GitLab