Browse Source

change push class, add new commands to bot

master
R4SAS 6 years ago
parent
commit
85c224304c
  1. 2
      README.md
  2. 7
      include/classes/push_notification/telegram.php
  3. 58
      include/pages/tghook.inc.php

2
README.md

@ -3,6 +3,6 @@ MPOS Telegram push notifications binding
Here is example of telegram based push notificator and webhook handler. Here is example of telegram based push notificator and webhook handler.
####TODO #### TODO
Write more information about this stuff Write more information about this stuff

7
include/classes/push_notification/telegram.php

@ -1,7 +1,8 @@
<?php <?php
define('TGAPI_URL', 'https://api.telegram.org/bot' . $this->config['push']['telegram']['api_key']); define('TGAPI_URL', 'https://api.telegram.org/bot' . $this->config['push']['telegram']['api_key']);
define('TG_BOTNAME', $this->config['push']['telegram']['bot_username']);
class Notifications_Telegramnew implements IPushNotification { class Notifications_Telegram implements IPushNotification {
private $tgid; private $tgid;
public function __construct($tgid){ public function __construct($tgid){
@ -15,12 +16,12 @@ class Notifications_Telegramnew implements IPushNotification {
); );
public static function getName(){ public static function getName(){
return "Telegram"; return "Telegram (@".TG_BOTNAME.")";
} }
public static function getParameters(){ public static function getParameters(){
return array( return array(
'tgid' => 'Your Telegram ID from bot', 'tgid' => 'Your Telegram Chat ID',
); );
} }

58
include/pages/tghook.inc.php

@ -16,13 +16,22 @@ $update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"]; $chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"]; $message = $update["message"]["text"];
$prepareResponse = function ($message) use ($chatID, $api, $setting, $block, $transaction, $worker, $mysqli){ $prepareResponse = function ($message) use ($chatID, $api, $bitcoin, $block, $setting, $statistics, $transaction, $user, $worker, $mysqli){
// 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())
$user_id = $result->fetch_object()->user_id;
if ( ! $interval = $setting->getValue('statistics_ajax_data_interval')) $interval = 300;
$response = '';
switch($message){ switch($message){
case "/start": case "/start":
$response = "Hi there! I'm ".($setting->getValue('website_title')?:"PHP-MPOS")." notifications bot.".PHP_EOL."Type /help to see all commands!"; $response = "Hi there! I'm ".($setting->getValue('website_title')?:"PHP-MPOS")." notifications bot.".PHP_EOL."Type /help to see all commands!";
break; break;
case "/help": 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"; $response = "/api - Get API state of pool frontend".PHP_EOL."/getid - Show your id, required for pool notifications".PHP_EOL."/help - Print this text".PHP_EOL.PHP_EOL.
"*Commands for registered users:*".PHP_EOL."/hashrate - Show your hashrate".PHP_EOL."/status - Show information about network and pool".PHP_EOL."/workers - Show information about your workers";
break; break;
case "/api": case "/api":
$response = "API state: ".($api->isActive()?"enabled":"disabled"); $response = "API state: ".($api->isActive()?"enabled":"disabled");
@ -31,18 +40,45 @@ $prepareResponse = function ($message) use ($chatID, $api, $setting, $block, $tr
$response = "Your ID is ".$chatID.PHP_EOL."Use that ID on pool notifications settings page."; $response = "Your ID is ".$chatID.PHP_EOL."Use that ID on pool notifications settings page.";
break; break;
// That is example of command, which will send information to user about his workers. // That is example of command, which will send information to user about his workers.
case "/hashrate":
if($user_id){
$username = $user->getUsername($user_id);
$stats = $statistics->getUserMiningStats($username, $user_id, $interval);
$response = sprintf("Your workers hashrate is %.4f KH/s", $stats['hashrate']);
}
break;
case "/status":
if($user_id){
$poolHashrate = $statistics->getCurrentHashrate();
$poolWorkers = $worker->getCountAllActiveWorkers();
$poolLastBlock = $block->getLast();
$now = new DateTime( "now" );
if (!empty($poolLastBlock)) {
$poolTimeSinceLast = ($now->getTimestamp() - $poolLastBlock['time']);
} else {
$poolTimeSinceLast = 0;
}
if ($bitcoin->can_connect() === true){
$netDiff = $bitcoin->getdifficulty();
$netBlock = $bitcoin->getblockcount();
$netHashrate = $bitcoin->getnetworkhashps();
} else {
$netDiff = 0;
$netBlock = unknown;
$netHashrate = 0;
}
$response = sprintf("*Network*\nBlock: %s\nDiff: %.4f\nHashrate: %.4f MH/s\n\n*Pool*\nHashrate: %.4f MH/s\nWorkers: %d\nLast found block: %d\nTime since block: %d min",
$netBlock, $netDiff, $netHashrate/1000000, $poolHashrate/1000, $poolWorkers, $poolLastBlock['height'], $poolTimeSinceLast/60);
}
break;
case "/workers": case "/workers":
// Get userID in MPOS by chatID if($user_id){
$stmt = $mysqli->prepare("SELECT account_id as user_id FROM user_settings WHERE value LIKE '%".$chatID."%' LIMIT 1"); $workers = $worker->getWorkers($user_id, $interval);
if($stmt && $stmt->execute() && $result = $stmt->get_result())
if($user_id = $result->fetch_object()->user_id){ // 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
$response = '';
foreach ($workers as $worker) foreach ($workers as $worker)
$response .= sprintf("*Username: %s*\nShares: %.4f\nHashrate: %.4f\nDifficulty: %.4f\n\n", $worker['username'], $worker['shares'], $worker['hashrate'], $worker['difficulty']); $response .= sprintf("*Username: %s*\nShares: %.4f\nHashrate: %.4f\nDifficulty: %.4f\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; break;
default: default:

Loading…
Cancel
Save