Added activation process for Telegram notifications
parent
67632ab966
commit
64b4d60ffc
|
@ -742,38 +742,48 @@ function psm_password_decrypt($key, $encryptedString)
|
|||
}
|
||||
|
||||
/**
|
||||
* Send notification to Telegram
|
||||
*
|
||||
* @return string
|
||||
* @author Tim Zandbergen <tim@xervion.nl>
|
||||
*/
|
||||
class telegram
|
||||
{
|
||||
private $_token;
|
||||
private $_user;
|
||||
private $_message;
|
||||
private $_url;
|
||||
* Send notification to Telegram
|
||||
*
|
||||
* @return string
|
||||
* @author Tim Zandbergen <tim@xervion.nl>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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!<br><a href="%s" target="_blank"><button class="btn btn-primary">Next step</button></a> <br>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' => '<a href="https://telegram.org/">Telegram</a> is a chat app that makes it easy to get real-time notifications. Visit the <a href="http://docs.phpservermonitor.org/">documentation</a> 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 <a href="http://docs.phpservermonitor.org/">documentation</a> for help.',
|
||||
'alert_type' => 'Select when you\'d like to be notified.',
|
||||
'alert_type_description' => '<b>Status change:</b> '.
|
||||
'You will receive a notifcation when a server has a change in status. So from online -> offline or offline -> online.<br/>'.
|
||||
|
@ -353,3 +359,4 @@ $sm_lang = array(
|
|||
'401_unauthorized_description' => 'You do not have the privileges to view this page.',
|
||||
),
|
||||
);
|
||||
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<title>{{ title }}</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, minimum-scale=1">
|
||||
<meta name="description" content="">
|
||||
<meta name="robots" content="noindex" />
|
||||
<meta name="robots" content="noindex" />
|
||||
<meta name="theme-color" content="#424242">
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||
<link rel="icon" type="image/png" href="favicon.png" />
|
||||
|
@ -57,7 +57,7 @@
|
|||
{% for msg in messages %}
|
||||
<div class="alert alert-{{ msg.shortcode }}">
|
||||
<p class="pull-left span1"><i class="icon-{{ msg.icon }}"></i></p>
|
||||
<p>{{ msg.message }}</p>
|
||||
<p>{{ msg.message|raw }}</p>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
@ -76,3 +76,4 @@
|
|||
<!-- /container -->
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
|
|
@ -71,9 +71,15 @@
|
|||
<div class="control-group">
|
||||
<label class="control-label" for="mobile">{{ label_telegram_chat_id }}</label>
|
||||
<div class="controls">
|
||||
<input type="text" id="telegram_id" name="telegram_id" maxlength="255" data-toggle="tooltip" title="{{ label_telegram_chat_id_description }}" />
|
||||
<input type="text" id="telegram_id" name="telegram_id" value="{{ telegram_id }}" maxlength="255" data-toggle="tooltip" title="{{ label_telegram_chat_id_description }}" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<button class="btn btn-primary show-modal" data-modal-id="activateTelegram">{{ label_activate_telegram }}</button>
|
||||
<input type="hidden" name="activate_telegram" value="0" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row-fluid">
|
||||
|
@ -83,3 +89,4 @@
|
|||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
|
|
Loading…
Reference in New Issue