diff --git a/application/core/MY_Exceptions.php b/application/core/MY_Exceptions.php index 4a4f2e3b486172a2f1c5a010f40d285064ba9724..d6c447ca650a9e7f6592d404eb127e404ecdc838 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 0000000000000000000000000000000000000000..66201d55d185b17af1be677291d94798a82d3e1f --- /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 916a284093d660d152fee0597a2f03117705272c..6c5cd7dd84390b01a2fb9729e031a9ffc68dba4f 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');