From a7241a76659bd84f8016038b3809cffb5a53f920 Mon Sep 17 00:00:00 2001 From: Marco G Date: Sun, 17 Dec 2017 11:26:25 +0100 Subject: [PATCH 1/4] Updated the Notification System and added Pushover as Service --- example.config.inc.php | 7 ++++- trader.php | 66 +++++++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/example.config.inc.php b/example.config.inc.php index 9068359..5950191 100644 --- a/example.config.inc.php +++ b/example.config.inc.php @@ -23,9 +23,14 @@ define('SLEEPTIME',10); // chat system output (rocket.chat and Slack supported) -define('ROCKETCHAT_REPORTING',true); //enables/disables rocketchat reporting +define('ROCKETCHAT_REPORTING',false); //enables/disables rocketchat reporting define('ROCKETCHAT_WEBHOOK',''); // The URL of the webhook. Also works with a Slack webhook! +// secure mobile notifications by pushover (https://pushover.net) +define('PUSHOVER_REPORTING',false); //enables/disables rocketchat reporting +define('PUSHOVER_TOKEN',''); // Pushover App token +define('PUSHOVER_USER',''); // Pushover User API Key + // Coinbase define('COINBASE_KEY',''); define('COINBASE_SECRET',''); diff --git a/trader.php b/trader.php index 6c4b27e..e0cbb84 100644 --- a/trader.php +++ b/trader.php @@ -21,9 +21,13 @@ use Coinbase\Wallet\Resource\Buy; use Coinbase\Wallet\Value\Money; +if(empty($argv[1])) $argv[1] = false; $t = new trader(($argv[1]==='debug'?true:false)); $myname = $argv[0]; + + + switch($argv[1]) { @@ -78,6 +82,11 @@ $t->debug(); break; + case 'pushovertest': + var_dump(sendToPushover("test")); + break; + + default: echo "Usage info\n---------------\n"; echo "php $myname buy \n"; @@ -325,7 +334,7 @@ function addBuyTransaction($eur,$buyat,$sellat) $id = @max(array_keys($this->transactions))+1; $this->transactions[$id] = array('eur'=>$eur,'buyprice'=>$buyat,'sellat'=>$sellat); $this->saveTransactions(); - if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Added BUY order for *$eur ".CURRENCY."* when ".CRYPTO." price hits *$buyat ".CURRENCY."*. Currently it's at: *$this->sellPrice ".CURRENCY."*. Only *".($this->sellPrice-$buyat).' '.CURRENCY.'* to go',':raised_hands:'); + sendMessage("Added BUY order for *$eur ".CURRENCY."* when ".CRYPTO." price hits *$buyat ".CURRENCY."*. Currently it's at: *$this->sellPrice ".CURRENCY."*. Only *".($this->sellPrice-$buyat).' '.CURRENCY.'* to go',':raised_hands:'); } function addSellTransaction($eur,$sellat) @@ -335,7 +344,7 @@ function addSellTransaction($eur,$sellat) $id = @max(array_keys($this->transactions))+1; $this->transactions[$id] = array('eur'=>$eur,'sellat'=>$sellat); $this->saveTransactions(); - if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Added SELL order for *$eur ".CURRENCY."* when ".CRYPTO." price hits *$sellat ".CURRENCY."*. Currently it's at: *$this->sellPrice ".CURRENCY."*. Only *".($sellat-$this->sellPrice).' '.CURRENCY.'* to go',':raised_hands:'); + sendMessage("Added SELL order for *$eur ".CURRENCY."* when ".CRYPTO." price hits *$sellat ".CURRENCY."*. Currently it's at: *$this->sellPrice ".CURRENCY."*. Only *".($sellat-$this->sellPrice).' '.CURRENCY.'* to go',':raised_hands:'); } /* @@ -394,8 +403,7 @@ function buyBTC($amount,$sellat,$btc=false) echo "[B #$id] Buying $eur €\t=\t$btc ".CRYPTO."\n"; - if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Buying *$btc ".CRYPTO."* for *$eur ".CURRENCY."*",':moneybag:'); - + sendMessage("Buying *$btc ".CRYPTO."* for *$eur ".CURRENCY."*",':moneybag:'); $this->saveTransactions(); return $id; @@ -410,7 +418,7 @@ function sellBTCID($id) $profit = round(($data['btc']*$this->sellPrice)-($data['btc']*$data['buyprice']),2); - if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Selling *".$data['btc']." ".CRYPTO."* for *".$data['eur']." ".CURRENCY."*. Profit: *$profit ".CURRENCY."*",':money_with_wings:'); + sendMessage("Selling *".$data['btc']." ".CRYPTO."* for *".$data['eur']." ".CURRENCY."*. Profit: *$profit ".CURRENCY."*",':money_with_wings:'); } function sellBTC($amount,$btc=false) @@ -429,6 +437,7 @@ function sellBTC($amount,$btc=false) if($this->checkBalanceOfAccount($this->account)<$btc) { echo " [ERR] You don't have enough ".CRYPTO." in your '".$this->account->getName()."'. Cancelling sell\n"; + sendMessage(" [ERR] You don't have enough ".CRYPTO." in your '".$this->account->getName()."'. Cancelling sell\n",":no_entry:"); return; } else @@ -477,7 +486,7 @@ function listTransactions() function watchdog() { - if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Starting watchdog",':wave:'); + sendMessage("Starting watchdog",':wave:'); while(1) { $this->mainCheck(); @@ -519,7 +528,7 @@ function mainCheck() $btc = (1/$this->sellPrice) * $eur; $this->deleteTransaction($id); $this->sellBTC($btc,true); - if(ROCKETCHAT_REPORTING===true) sendToRocketchat("Selling *".$btc." ".CRYPTO."* for *".$eur." ".CURRENCY."*. Forefilling and deleting this sell order.",':money_with_wings:'); + sendMessage("Selling *".$btc." ".CRYPTO."* for *".$eur." ".CURRENCY."*. Forefilling and deleting this sell order.",':money_with_wings:'); } else echo " [#$id] Watching SELL order for \t$eur ".CURRENCY.". Will sell when ".CRYPTO." price reaches ".$td['sellat']." ".CRYPTO.".\n"; @@ -559,7 +568,7 @@ function report() $out = ob_get_contents(); ob_end_clean(); - sendToRocketchat($out,':information_source:'); + sendMessage($out,':information_source:'); } function autotrade($stake=10,$sellpercent=115) @@ -611,11 +620,20 @@ function autotrade($stake=10,$sellpercent=115) } +// a general message function to handle different Message protocols +function sendMessage($message,$icon=':ghost:') +{ + if(!empty($message)) { + if(ROCKETCHAT_REPORTING===true) sendToRocketchat($message,$icon); + if(PUSHOVER_REPORTING===true) sendToPushover($message); + } +} + //rocketchat function sendToRocketchat($message,$icon=':ghost:') { - $username = CURRENCY.' - '.CRYPTO.' trader'.(SIMULATE===true?' (simulation)':''); + $username = CURRENCY.' - '.CRYPTO.' trader'.(SIMULATE===true?' (simulation)':''); $data = array("icon_emoji"=>$icon, "username"=>$username, "text"=>$message); @@ -636,4 +654,32 @@ function makeRequest($url,$data,$headers=false,$post=true) $result = file_get_contents($url, false, $context); if ($result === FALSE) { /* Handle error */ } return json_decode($result,true); -} \ No newline at end of file +} + + +//pushover +function sendToPushover($message) +{ + if( empty(PUSHOVER_TOKEN) || empty(PUSHOVER_USER) || empty($message) ) return false; + + $username = CURRENCY.' - '.CRYPTO.' trader'.(SIMULATE===true?' (simulation)':''); + curl_setopt_array($ch = curl_init(), array( + CURLOPT_URL => "https://api.pushover.net/1/messages.json", + CURLOPT_POSTFIELDS => array( + "token"=>PUSHOVER_TOKEN, + "user"=>PUSHOVER_USER, + "priority"=>-1, + "title"=>$username, + "message"=>$message), + CURLOPT_SAFE_UPLOAD => true, + CURLOPT_RETURNTRANSFER => true, + )); + $RET = curl_exec($ch); + curl_close($ch); + return $RET; +} + + + + + From 729f1cbf0b857d59f89bd5413befe20712db2b10 Mon Sep 17 00:00:00 2001 From: Marco G Date: Sun, 17 Dec 2017 11:50:53 +0100 Subject: [PATCH 2/4] added systemd example script for more robust startup --- README.md | 16 ++++++++++++++-- example.systemd.service | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 example.systemd.service diff --git a/README.md b/README.md index b148f25..1e07a79 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ - Raspberry Pi or some Linux box - php5 or up - [composer](https://getcomposer.org/) -- (Optional) A Rocket.Chat or Slack webhook which will inform you whenever BTC/ETH are sold or bought +- (Optional) A Rocket.Chat, Slack or Pushover webhook which will inform you whenever BTC/ETH are sold or bought ## Install 1. Download the repo by using ```git clone https://github.com/chrisiaut/phptrader.git``` or download as [ZIP file](https://github.com/chrisiaut/phptrader/archive/master.zip) @@ -45,4 +45,16 @@ The heart of the bot is an infinite loop that checks periodically for price changes. You can start it yourself or use the ```start.sh``` script which will put the process in background and log to ```/var/log/phptrader.log``` -```./start.sh``` \ No newline at end of file +```./start.sh``` + +## Using SystemD +Instead of the nohup start Script you can add the phpTrader to SystemD. For this purpose just copy the example.systemd.service File. + +```cp example.systemd.service /lib/systemd/system/phpTrader.service``` + +After this please edit the phpTrader.service File and change the Lines "user / group / ExecStart" fitting to your Setup needs, when done reload the systemd and start: + +```systemctl daemon-reload +systemctl enable phpTrader +systemctl start phpTrader``` + diff --git a/example.systemd.service b/example.systemd.service new file mode 100644 index 0000000..9d580e5 --- /dev/null +++ b/example.systemd.service @@ -0,0 +1,36 @@ +# name: phpTrader.service +# 1. copy/rename this file into: '/lib/systemd/system/phpTrader.service' +# 2. change user/group and ExecStart matching your setup +# 3. then run: 'systemctl daemon-reload' +# 4. Enable by 'systemctl enable phpTrader' +# 5. start by 'systemctl start phpTrader' +# status by 'systemctl status phpTrader' + +[Unit] +Description=phpTrader Service +After=local-fs.target network.target + +[Service] +Type=simple +User=myphptraderuser +Group=myphptradergroup +SyslogIdentifier=phpTrader +StandardOutput=syslog +ExecStart=/usr/bin/php /opt/phptrader/trader.php watchdog 2>&1 +ReadOnlyDirectories=/etc +ReadOnlyDirectories=/usr +ReadOnlyDirectories=/proc +ReadOnlyDirectories=/sys +ReadOnlyDirectories=/sbin +ReadOnlyDirectories=/lib +ReadOnlyDirectories=/lib32 +ReadOnlyDirectories=/lib64 +InaccessibleDirectories=/boot +InaccessibleDirectories=/home +Nice=10 +KillMode=process +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target From d711ac101c3914eeac8343bb25e00e5e386d56cc Mon Sep 17 00:00:00 2001 From: Marco G Date: Sun, 17 Dec 2017 11:51:59 +0100 Subject: [PATCH 3/4] fix --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1e07a79..9b5f463 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,9 @@ Instead of the nohup start Script you can add the phpTrader to SystemD. For this After this please edit the phpTrader.service File and change the Lines "user / group / ExecStart" fitting to your Setup needs, when done reload the systemd and start: -```systemctl daemon-reload +``` +systemctl daemon-reload systemctl enable phpTrader -systemctl start phpTrader``` +systemctl start phpTrader +``` From 3436aa16b9c08edde4bdc81ca818efb82433018b Mon Sep 17 00:00:00 2001 From: Marco G Date: Sun, 17 Dec 2017 11:52:36 +0100 Subject: [PATCH 4/4] fix heading --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b5f463..081d10d 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ You can start it yourself or use the ```start.sh``` script which will put the pr ```./start.sh``` -## Using SystemD +### Using SystemD Instead of the nohup start Script you can add the phpTrader to SystemD. For this purpose just copy the example.systemd.service File. ```cp example.systemd.service /lib/systemd/system/phpTrader.service```