From 64b4d60ffc5d21311ae09e5d1117452beb47fcd6 Mon Sep 17 00:00:00 2001 From: TimZ99 Date: Sat, 3 Mar 2018 16:14:55 +0000 Subject: [PATCH] Added activation process for Telegram notifications --- src/includes/functions.inc.php | 76 +++++++++++-------- src/lang/en_US.lang.php | 9 ++- .../User/Controller/ProfileController.php | 41 +++++++++- src/templates/default/main/body.tpl.html | 5 +- .../default/module/user/profile.tpl.html | 9 ++- 5 files changed, 102 insertions(+), 38 deletions(-) diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index b7717c28..f02ccb67 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -742,38 +742,48 @@ function psm_password_decrypt($key, $encryptedString) } /** - * Send notification to Telegram - * - * @return string - * @author Tim Zandbergen - */ - class telegram - { - private $_token; - private $_user; - private $_message; - private $_url; +* Send notification to Telegram +* +* @return string +* @author Tim Zandbergen +*/ +class telegram +{ + private $_token; + private $_user; + private $_message; + private $_url; - public function setToken ($token) { - $this->_token = (string)$token; - - } - public function setUser ($user) { - $this->_user = (string)$user; - } - public function setMessage ($message) { - $this->_message = (string)$message; - } - public function send () { - if(!Empty($this->_token) && !Empty($this->_user) && !Empty($this->_message)) { - $this->_url = 'https://api.telegram.org/bot' . urlencode($this->_token) . '/sendMessage?chat_id=' . urlencode($this->_user) . '&text=' . urlencode($this->_message); - } - $con = curl_init($this->_url); - curl_setopt($con, CURLOPT_RETURNTRANSFER, true); - curl_setopt($con, CURLOPT_CONNECTTIMEOUT, 5); - curl_setopt($con, CURLOPT_TIMEOUT, 60); - $response = curl_exec($con); - $response = json_decode($response, true); - return $response; - } + public function setToken ($token) { + $this->_token = (string)$token; + } + public function setUser ($user) { + $this->_user = (string)$user; + } + public function setMessage ($message) { + $this->_message = (string)$message; + } + public function sendurl () { + $con = curl_init($this->_url); + curl_setopt($con, CURLOPT_RETURNTRANSFER, true); + curl_setopt($con, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($con, CURLOPT_TIMEOUT, 60); + $response = curl_exec($con); + $response = json_decode($response, true); + return $response; + } + public function send () { + if(!Empty($this->_token) && !Empty($this->_user) && !Empty($this->_message)) { + $this->_url = 'https://api.telegram.org/bot' . urlencode($this->_token) . '/sendMessage?chat_id=' . urlencode($this->_user) . '&text=' . urlencode($this->_message); } + return $this->sendurl(); + } + // Get the bots username + public function getBotUsername () { + if(!Empty($this->_token)) { + $this->_url = 'https://api.telegram.org/bot' . urlencode($this->_token) . '/getMe'; + } + return $this->sendurl(); + } +} + diff --git a/src/lang/en_US.lang.php b/src/lang/en_US.lang.php index bc558555..b9df5874 100644 --- a/src/lang/en_US.lang.php +++ b/src/lang/en_US.lang.php @@ -48,6 +48,7 @@ $sm_lang = array( 'go_back' => 'Go back', 'ok' => 'OK', 'cancel' => 'Cancel', + 'activate' => 'Activate', // date/time format according the strftime php function format parameter http://php.net/manual/function.strftime.php 'short_day_format' => '%B %e', 'long_day_format' => '%B %e, %Y', @@ -93,6 +94,11 @@ $sm_lang = array( 'telegram_chat_id' => 'Telegram chat id', 'telegram_chat_id_description' => 'The message will be send to the corresponding chat.', 'telegram_get_chat_id' => 'Click here to get your chat id', + 'activate_telegram' => 'Activate Telegram notifications', + 'activate_telegram_description' => 'Allow Telegram notifications to be sent to the specified chat id. Without this permission, Telegram doesn\'t allow us to send notifications to you.', + 'telegram_bot_username_found' => 'The bot was found!

This will open a chat with the bot. Here you need to press start of type /start.', + 'telegram_bot_username_error_token' => '401 - Unauthorized. Please make sure that the API token is valid.', + 'telegram_bot_error' => 'An error has occurred while activating Telegram notification: %s', 'delete_title' => 'Delete User', 'delete_message' => 'Are you sure you want to delete user \'%1\'?', 'deleted' => 'User deleted.', @@ -245,7 +251,7 @@ $sm_lang = array( 'telegram_status' => 'Allow sending Telegram messages', 'telegram_description' => 'Telegram is a chat app that makes it easy to get real-time notifications. Visit the documentation for more info and an install guide.', 'telegram_api_token' => 'Telegram API Token', - 'telegram_api_token_description' => 'Before you can use Telegram, you need to get a API token. Open your Telegram app and search for @Botfather.', + 'telegram_api_token_description' => 'Before you can use Telegram, you need to get a API token. Visit the documentation for help.', 'alert_type' => 'Select when you\'d like to be notified.', 'alert_type_description' => 'Status change: '. 'You will receive a notifcation when a server has a change in status. So from online -> offline or offline -> online.
'. @@ -353,3 +359,4 @@ $sm_lang = array( '401_unauthorized_description' => 'You do not have the privileges to view this page.', ), ); + diff --git a/src/psm/Module/User/Controller/ProfileController.php b/src/psm/Module/User/Controller/ProfileController.php index 2300f5ec..e3b4b554 100644 --- a/src/psm/Module/User/Controller/ProfileController.php +++ b/src/psm/Module/User/Controller/ProfileController.php @@ -54,6 +54,12 @@ class ProfileController extends AbstractController { $this->twig->addGlobal('subtitle', psm_get_lang('users', 'profile')); $user = $this->getUser()->getUser(null, true); + $modal = new \psm\Util\Module\Modal($this->twig, 'activate' . ucfirst('telegram'), \psm\Util\Module\Modal::MODAL_TYPE_OKCANCEL); + $this->addModal($modal); + $modal->setTitle(psm_get_lang('users', 'activate_telegram')); + $modal->setMessage(psm_get_lang('users', 'activate_telegram_description')); + $modal->setOKButtonLabel(psm_get_lang('system', 'activate')); + $tpl_data = array( 'label_name' => psm_get_lang('users', 'name'), 'label_user_name' => psm_get_lang('users', 'user_name'), @@ -70,6 +76,7 @@ class ProfileController extends AbstractController { 'label_telegram_description' => psm_get_lang('users', 'telegram_description'), 'label_telegram_chat_id' => psm_get_lang('users', 'telegram_chat_id'), 'label_telegram_chat_id_description' => psm_get_lang('users', 'telegram_chat_id_description'), + 'label_activate_telegram' => psm_get_lang('users', 'activate_telegram'), 'label_telegram_get_chat_id' => psm_get_lang('users', 'telegram_get_chat_id'), 'telegram_get_chat_id_url' => PSM_TELEGRAM_GET_ID_URL, 'label_email' => psm_get_lang('users', 'email'), @@ -138,7 +145,39 @@ class ProfileController extends AbstractController { $this->getUser()->changePassword($this->getUser()->getUserId(), $password); } $this->addMessage(psm_get_lang('users', 'profile_updated'), 'success'); - + if(!empty($_POST['activate_telegram'])) { + $this->activateTelegram(); + } return $this->executeIndex(); } + + /** + * Allow the bot to send notifications to chat_id + * + */ + protected function activateTelegram() { + $telegram = psm_build_telegram(); + $api_token = psm_get_conf('telegram_api_token'); + + if(empty($api_token)) { + $this->addMessage(psm_get_lang('config', 'telegram_error_notoken'), 'error'); + } else { + $result = $telegram->getBotUsername(); + + if(isset($result['ok']) && $result['ok'] != false) { + $url = "https://t.me/".$result["result"]["username"]; + $this->addMessage(sprintf(psm_get_lang('users', 'telegram_bot_username_found'), $url), 'success'); + } else { + if(isset($result['error_code']) && $result['error_code'] == 401) { + $error = psm_get_lang('users', 'telegram_bot_username_error_token'); + } elseif (isset($result['description'])) { + $error = $result['description']; + } else { + $error = 'Unknown'; + } + $this->addMessage(sprintf(psm_get_lang('users', 'telegram_bot_error'), $error), 'error'); + } + } + } } + diff --git a/src/templates/default/main/body.tpl.html b/src/templates/default/main/body.tpl.html index 4ce7dc56..f3afb824 100644 --- a/src/templates/default/main/body.tpl.html +++ b/src/templates/default/main/body.tpl.html @@ -6,7 +6,7 @@ {{ title }} - + @@ -57,7 +57,7 @@ {% for msg in messages %}

-

{{ msg.message }}

+

{{ msg.message|raw }}

{% endfor %} @@ -76,3 +76,4 @@ + diff --git a/src/templates/default/module/user/profile.tpl.html b/src/templates/default/module/user/profile.tpl.html index 36260972..ceedaa48 100644 --- a/src/templates/default/module/user/profile.tpl.html +++ b/src/templates/default/module/user/profile.tpl.html @@ -71,9 +71,15 @@
- +
+
+
+ + +
+
@@ -83,3 +89,4 @@
+