Browse Source

publish telegram binding and push notificator

master
R4SAS 6 years ago
commit
cf24a8d858
  1. 8
      README.md
  2. 45
      include/classes/push_notification/telegram.php
  3. 322
      include/config/global.inc.dist.php
  4. 71
      include/pages/tghook.inc.php
  5. 24
      include/pages/tghook/set.inc.php
  6. 16
      include/pages/tghook/unset.inc.php

8
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

45
include/classes/push_notification/telegram.php

@ -0,0 +1,45 @@
<?php
define('TGAPI_URL', 'https://api.telegram.org/bot' . $this->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 ('<br/>');
$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);
}
}

322
include/config/global.inc.dist.php

@ -0,0 +1,322 @@
<?php
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
/**
* Do not edit this unless you have confirmed that your config has been updated!
* Also the URL to check for the most recent upstream versions available
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#config-version
**/
$config['version'] = '1.0.1';
$config['version_url'] = 'https://raw.githubusercontent.com/MPOS/php-mpos/master/include/version.inc.php';
/**
* Unless you disable this, we'll do a quick check on your config first.
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#config-check
*/
$config['skip_config_tests'] = false;
/**
* Unless you disable this, we'll do a check for a valid coin address on registration.
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#check-for-valid-wallet-address
*/
$config['check_valid_coinaddress'] = true;
/**
* Defines
* Debug setting and salts for hashing passwords
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#defines--salts
*/
$config['DEBUG'] = 0;
$config['SALT'] = 'PLEASEMAKEMESOMETHINGRANDOM';
$config['SALTY'] = 'THISSHOULDALSOBERRAANNDDOOM';
/**
* Coin Algorithm
* Algorithm used by this coin, sha256d or scrypt
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#algorithm
**/
$config['algorithm'] = 'scrypt';
/**
* Getbalance API Calls
* System used for getting actual Balance from Wallet
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#getbalance-api-calls
**/
$config['getbalancewithunconfirmed'] = true;
/**
* Database configuration
* MySQL database configuration
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#database-configuration
**/
$config['db']['host'] = 'localhost';
$config['db']['user'] = 'someuser';
$config['db']['pass'] = 'somepass';
$config['db']['port'] = 3306;
$config['db']['name'] = 'mpos';
// Disabled by default and set in bootstrap if unset, but left in here so
// people know it exists
// $config['db']['shared']['accounts'] = $config['db']['name'];
// $config['db']['shared']['workers'] = $config['db']['name'];
// $config['db']['shared']['news'] = $config['db']['name'];
/**
* Setup read-only/slave database server for selects (read queries)
**/
$config['db-ro']['enabled'] = false;
$config['db-ro']['host'] = 'localhost';
$config['db-ro']['user'] = 'someuser';
$config['db-ro']['pass'] = 'somepass';
$config['db-ro']['port'] = 3306;
$config['db-ro']['name'] = 'mpos';
/**
* Local wallet RPC
* RPC configuration for your daemon/wallet
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#local-wallet-rpc
**/
$config['wallet']['type'] = 'http';
$config['wallet']['host'] = 'localhost:19334';
$config['wallet']['username'] = 'testnet';
$config['wallet']['password'] = 'testnet';
/**
* Swiftmailer configuration
* Configure your way to send mails
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#swiftmailer
**/
$config['swiftmailer']['type'] = 'sendmail';
$config['swiftmailer']['sendmail']['path'] = '/usr/sbin/sendmail';
$config['swiftmailer']['sendmail']['options'] = '-bs';
$config['swiftmailer']['smtp']['host'] = 'your.mail-relay.com';
$config['swiftmailer']['smtp']['port'] = '587';
$config['swiftmailer']['smtp']['encryption'] = 'tls';
$config['swiftmailer']['smtp']['username'] = '';
$config['swiftmailer']['smtp']['password'] = '';
$config['swiftmailer']['smtp']['throttle'] = 100;
/**
* Getting Started Config
* Shown to users in the 'Getting Started' section
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#getting-started
**/
$config['gettingstarted']['coinname'] = 'Litecoin';
$config['gettingstarted']['coinurl'] = 'http://www.litecoin.org';
$config['gettingstarted']['stratumurl'] = '';
$config['gettingstarted']['stratumport'] = '3333';
/**
* Ticker API
* Fetch exchange rates via an API
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#ticker-api
**/
$config['price']['enabled'] = false;
$config['price']['url'] = 'https://btc-e.nz';
$config['price']['target'] = '/api/2/ltc_usd/ticker';
$config['price']['currency'] = 'USD';
/**
* Automatic Payout Thresholds
* Minimum and Maximum auto payout amount
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#automatic-payout-thresholds
**/
$config['ap_threshold']['min'] = 1;
$config['ap_threshold']['max'] = 250;
/**
* Minimum manual Payout Threshold
* Minimum manual payout amount
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#manual-payout-threshold
**/
$config['mp_threshold'] = 1;
/**
* Donation thresholds
* Minimum donation amount in percent
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#donation-thresholds
**/
$config['donate_threshold']['min'] = 1;
/**
* Account Specific Settings
* Settings for each user account
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#account-specific-settings
**/
$config['accounts']['invitations']['count'] = 5;
/**
* Currency
* Shorthand name for the currency
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#currency
*/
$config['currency'] = 'LTC';
/**
* Coin Target
* Target time for coins to be generated
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#coin-target
**/
$config['cointarget'] = '150';
/**
* Coin Diff Change
* Amount of blocks between difficulty changes
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#coin-diff-change
**/
$config['coindiffchangetarget'] = 2016;
/**
* TX Fees
* Fees applied to transactions
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#tx-fees
**/
$config['txfee_auto'] = 0.1;
$config['txfee_manual'] = 0.1;
/**
* Block & Pool Bonus
* Bonus coins for blockfinder or a pool bonus for everyone
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#block-bonus
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#pool-bonus
*/
$config['block_bonus'] = 0;
$config['pool_bonus'] = 0;
$config['pool_bonus_type'] = 'payout';
/**
* Payout System
* Payout system chosen
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#payout-system
**/
$config['payout_system'] = 'prop';
/**
* Sendmany Support
* Enable/Disable Sendmany RPC method
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#sendmany-support
**/
$config['sendmany']['enabled'] = false;
/**
* Transaction Limits
* Number of transactions per payout run
**/
$config['payout']['txlimit_manual'] = 500;
$config['payout']['txlimit_auto'] = 500;
/**
* Round Purging
* Round share purging configuration
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#round-purging
**/
$config['purge']['sleep'] = 1;
$config['purge']['shares'] = 25000;
/**
* Share Archiving
* Share archiving configuration details
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#archiving
**/
$config['archive']['maxrounds'] = 10;
$config['archive']['maxage'] = 60 * 24;
/**
* Pool Fees
* Fees applied to users
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#pool-fees
*/
$config['fees'] = 0;
/**
* PPLNS
* Pay Per Last N Shares
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#pplns-settings
*/
$config['pplns']['shares']['default'] = 4000000;
$config['pplns']['shares']['type'] = 'blockavg';
$config['pplns']['blockavg']['blockcount'] = 10;
$config['pplns']['reverse_payout'] = true;
$config['pplns']['dynamic']['percent'] = 30;
/**
* Difficulty
* Difficulty setting for stratum/pushpool
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#pool-target-difficulty
*/
$config['difficulty'] = 20;
/**
* Block Reward
* Block reward configuration details
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#reward-settings
**/
$config['reward_type'] = 'block';
$config['reward'] = 50;
/**
* Confirmations
* Credit and Network confirmation settings
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#confirmations
*/
$config['confirmations'] = 120;
$config['network_confirmations'] = 120;
/**
* PPS
* Pay Per Share configuration details
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#pps-settings
**/
$config['pps']['reward']['default'] = 50;
$config['pps']['reward']['type'] = 'blockavg';
$config['pps']['blockavg']['blockcount'] = 10;
/**
* Memcache
* Memcache configuration details
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#memcache
**/
$config['memcache']['enabled'] = true;
$config['memcache']['host'] = 'localhost';
$config['memcache']['port'] = 11211;
$config['memcache']['keyprefix'] = 'mpos_';
$config['memcache']['expiration'] = 90;
$config['memcache']['splay'] = 15;
$config['memcache']['force']['contrib_shares'] = false;
$config['memcache']['sasl'] = false;
$config['memcache']['sasl']['username'] = '';
$config['memcache']['sasl']['password'] = '';
/**
* Cookies
* Cookie configuration details
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#cookies
**/
$config['cookie']['duration'] = '1440';
$config['cookie']['domain'] = '';
$config['cookie']['path'] = '/';
$config['cookie']['httponly'] = true;
$config['cookie']['secure'] = false;
/**
* Smarty Cache
* Enable smarty cache and cache length
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#smarty-cache
**/
$config['smarty']['cache'] = 0;
$config['smarty']['cache_lifetime'] = 30;
/**
* System load
* Disable some calls when high system load
* https://github.com/MPOS/php-mpos/wiki/Config-Setup#system-load
**/
$config['system']['load']['max'] = 10.0;
/**
* Telegram notificator configuration
* To use push notifications via Telegram you need create bot and set here its API key
**/
$config['push']['telegram']['api_key'] = '';

71
include/pages/tghook.inc.php

@ -0,0 +1,71 @@
<?php
$supress_master = 1;
define('TGAPI_URL', 'https://api.telegram.org/bot' . $config['push']['telegram']['api_key']);
// https://gist.github.com/theMiddleBlue/6d5e9082e0c3c378bfb037795b2570b8
if(!preg_match('/^149\.154\.167\.(19[7-9]|20[0-9]|21[0-9]|22[0-9]|23[0-3])$/', $_SERVER['REMOTE_ADDR'])) {
die('IP Address not allowed.');
}
if($_SERVER['REQUEST_METHOD'] != 'POST') {
die('Request method not allowed.');
}
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$prepareResponse = function ($message) use ($chatID, $api, $setting, $block, $worker, $mysqli){
switch($message){
case "/start":
$response = "Hi there! I'm ".($setting->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);

24
include/pages/tghook/set.inc.php

@ -0,0 +1,24 @@
<?php
$supress_master = 1;
if (!$user->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);

16
include/pages/tghook/unset.inc.php

@ -0,0 +1,16 @@
<?php
$supress_master = 1;
if (!$user->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);
Loading…
Cancel
Save