commit cf24a8d858bc187b5372f4f6d0a3862f0e3b8b89 Author: r4sas Date: Tue Oct 16 17:53:36 2018 +0000 publish telegram binding and push notificator diff --git a/README.md b/README.md new file mode 100644 index 0000000..5348efa --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +MPOS Telegram push notifications binding +===== + +Here is example of telegram based push notificator and webhook handler. + +####TODO + +Write more information about this stuff diff --git a/include/classes/push_notification/telegram.php b/include/classes/push_notification/telegram.php new file mode 100644 index 0000000..c627676 --- /dev/null +++ b/include/classes/push_notification/telegram.php @@ -0,0 +1,45 @@ +config['push']['telegram']['api_key']); + +class Notifications_Telegramnew implements IPushNotification { + + private $tgid; + public function __construct($tgid){ + $this->tgid = $tgid; + } + + static $priorities = array( + 0 => 'info', + 1 => 'warning', + 2 => 'error', + ); + + public static function getName(){ + return "Telegram"; + } + + public static function getParameters(){ + return array( + 'tgid' => 'Your Telegram ID from bot', + ); + } + + public function notify($message, $severity = 'info', $event = null){ + $patterns = array ('
'); + $replace = array (''); + $msg = str_replace ($patterns, $replace, $message); + + curl_setopt_array($ch = curl_init(), array( + CURLOPT_URL => TGAPI_URL . "/sendMessage", + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => http_build_query($data = array( + "chat_id" => $this->tgid, + "text" => $msg, + "parse_mode" => "HTML", + )), + )); + curl_exec($ch); + curl_close($ch); + } +} diff --git a/include/config/global.inc.dist.php b/include/config/global.inc.dist.php new file mode 100644 index 0000000..e6c7095 --- /dev/null +++ b/include/config/global.inc.dist.php @@ -0,0 +1,322 @@ +getValue('website_title')?:"PHP-MPOS")." notifications bot.".PHP_EOL."Type /help to see all commands!"; + break; + case "/help": + $response = "/api - Get API state of pool frontend".PHP_EOL."/getid - Show your id, required for pool notifications".PHP_EOL."/workers - Show information about your workers"; + break; + case "/api": + $response = "API state: ".($api->isActive()?"enabled":"disabled"); + break; + case "/getid": + $response = "Your ID is ".$chatID.PHP_EOL."Use that ID on pool notifications settings page."; + break; + // That is example of command, which will send information to user about his workers. + case "/workers": + // Get userID in MPOS by chatID + $stmt = $mysqli->prepare("SELECT account_id as user_id FROM user_settings WHERE value LIKE '%".$chatID."%' LIMIT 1"); + if($stmt && $stmt->execute() && $result = $stmt->get_result()) + if($user_id = $result->fetch_object()){ // If user with chatID found + if ( ! $interval = $setting->getValue('statistics_ajax_data_interval')) $interval = 300; + $workers = $worker->getWorkers($user_id, $interval); // Get all workers and prepare message + foreach ($workers as $worker) + $response .= sprintf("*Username: %s*\nShares: %s\nHashrate: %s\nDifficulty: %s\n\n", $worker[username], $worker[shares], $worker[hashrate], $worker[difficulty]); + } else { // Else write about requirement to provide chatID in notification settings + $response = "We coudn't find you in our database.".PHP_EOL."Make sure that you set ID in notifications settings on pool."; + } + break; + default: + $response = "mumble-mumble..."; + break; + } + return $response; +}; + +function sendMessage($chatID, $reply){ + curl_setopt_array($ch = curl_init(), array( + CURLOPT_URL => TGAPI_URL . "/sendMessage", + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => http_build_query($data = array( + "chat_id" => $chatID, + "text" => $reply, + "parse_mode" => "Markdown", + )), + )); + curl_exec($ch); + curl_close($ch); +} + +$reply = $prepareResponse($message); +sendMessage($chatID, $reply); + diff --git a/include/pages/tghook/set.inc.php b/include/pages/tghook/set.inc.php new file mode 100644 index 0000000..dc5b8d6 --- /dev/null +++ b/include/pages/tghook/set.inc.php @@ -0,0 +1,24 @@ +isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) { + header("HTTP/1.1 404 Page not found"); + die("404 Page not found"); +} + +define('TGAPI_URL', 'https://api.telegram.org/bot' . $config['push']['telegram']['api_key']); + +$pushto = $_SERVER['SCRIPT_NAME'].'?page=tghook'; +$hook_url = 'https://' . $_SERVER['HTTP_HOST'] . $pushto; + +curl_setopt_array($ch = curl_init(), array( + CURLOPT_URL => TGAPI_URL . "/setWebhook", + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => http_build_query($data = array( + "url" => $hook_url, + )), +)); +echo curl_exec($ch); +curl_close($ch); + diff --git a/include/pages/tghook/unset.inc.php b/include/pages/tghook/unset.inc.php new file mode 100644 index 0000000..18d297a --- /dev/null +++ b/include/pages/tghook/unset.inc.php @@ -0,0 +1,16 @@ +isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) { + header("HTTP/1.1 404 Page not found"); + die("404 Page not found"); +} + +define('TGAPI_URL', 'https://api.telegram.org/bot' . $config['push']['telegram']['api_key']); + +curl_setopt_array($ch = curl_init(), array( + CURLOPT_URL => TGAPI_URL . "/deleteWebhook", + CURLOPT_RETURNTRANSFER => true, +)); +echo curl_exec($ch); +curl_close($ch);