From 8605407d6b60c566e47ea074457ae9ae6a803919 Mon Sep 17 00:00:00 2001 From: Henry Whitaker Date: Sun, 21 Jun 2020 14:07:50 +0100 Subject: [PATCH] Added telegram notifications --- .env.example | 3 + README.md | 5 +- app/Listeners/SpeedtestCompleteListener.php | 18 ++++- ...omplete.php => SpeedtestCompleteSlack.php} | 18 +++-- .../SpeedtestCompleteTelegram.php | 73 +++++++++++++++++++ changelog.json | 6 ++ composer.json | 1 + composer.lock | 63 +++++++++++++++- config/app.php | 2 + config/services.php | 4 + config/speedtest.php | 2 +- 11 files changed, 183 insertions(+), 12 deletions(-) rename app/Notifications/{SpeedtestComplete.php => SpeedtestCompleteSlack.php} (65%) create mode 100644 app/Notifications/SpeedtestCompleteTelegram.php diff --git a/.env.example b/.env.example index 0b717d33..2259aebc 100644 --- a/.env.example +++ b/.env.example @@ -24,4 +24,7 @@ REMEMBER_DAYS=30 SLACK_WEBHOOK= +TELEGRAM_BOT_TOKEN= +TELEGRAM_CHAT_ID= + BASE_PATH= diff --git a/README.md b/README.md index 23c41352..5150246d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Speedtest Tracker -[![Docker pulls](https://img.shields.io/docker/pulls/henrywhitaker3/speedtest-tracker)](https://hub.docker.com/r/henrywhitaker3/speedtest-tracker) [![last_commit](https://img.shields.io/github/last-commit/henrywhitaker3/Speedtest-Tracker)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) [![issues](https://img.shields.io/github/issues/henrywhitaker3/Speedtest-Tracker)](https://github.com/henrywhitaker3/Speedtest-Tracker/issues) ![version](https://img.shields.io/badge/version-v1.5.6-success) [![license](https://img.shields.io/github/license/henrywhitaker3/Speedtest-Tracker)](https://github.com/henrywhitaker3/Speedtest-Tracker/blob/master/LICENSE) +[![Docker pulls](https://img.shields.io/docker/pulls/henrywhitaker3/speedtest-tracker)](https://hub.docker.com/r/henrywhitaker3/speedtest-tracker) [![last_commit](https://img.shields.io/github/last-commit/henrywhitaker3/Speedtest-Tracker)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) [![issues](https://img.shields.io/github/issues/henrywhitaker3/Speedtest-Tracker)](https://github.com/henrywhitaker3/Speedtest-Tracker/issues) ![version](https://img.shields.io/badge/version-v1.6.0-success) [![license](https://img.shields.io/github/license/henrywhitaker3/Speedtest-Tracker)](https://github.com/henrywhitaker3/Speedtest-Tracker/blob/master/LICENSE) This program runs a speedtest check every hour and graphs the results. The back-end is written in [Laravel](https://laravel.com/) and the front-end uses [React](https://reactjs.org/). It uses the [speedtest-cli](https://www.speedtest.net/apps/cli) package to get the data and uses [Chart.js](https://www.chartjs.org/) to plot the results. @@ -11,7 +11,7 @@ This program runs a speedtest check every hour and graphs the results. The back- - Automatically run a speedtest every hour - Graph of previous speedtests going back x days - Backup/restore data in JSON/CSV format -- Slack/Discord notifications +- Slack/Discord/Telegram notifications - Organizr integration ## Installation & Setup @@ -28,6 +28,7 @@ docker create \ -e SLACK_WEBHOOK=webhook `#optional` \ -e PUID=uid `#optional` \ -e PGID=gid `#optional` \ + -e OOKLA_EULA_GDPR=true \ --restart unless-stopped \ henrywhitaker3/speedtest-tracker ``` diff --git a/app/Listeners/SpeedtestCompleteListener.php b/app/Listeners/SpeedtestCompleteListener.php index 6155c4b0..52c3c6af 100644 --- a/app/Listeners/SpeedtestCompleteListener.php +++ b/app/Listeners/SpeedtestCompleteListener.php @@ -2,12 +2,14 @@ namespace App\Listeners; -use App\Notifications\SpeedtestComplete; +use App\Notifications\SpeedtestCompleteSlack; +use App\Notifications\SpeedtestCompleteTelegram; use Exception; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Notification; +use NotificationChannels\Telegram\TelegramChannel; class SpeedtestCompleteListener { @@ -29,15 +31,25 @@ class SpeedtestCompleteListener */ public function handle($event) { + $data = $event->speedtest; if(env('SLACK_WEBHOOK')) { - $data = $event->speedtest; try { Notification::route('slack', env('SLACK_WEBHOOK')) - ->notify(new SpeedtestComplete($data)); + ->notify(new SpeedtestCompleteSlack($data)); } catch(Exception $e) { Log::notice('Your sleck webhook is invalid'); Log::notice($e); } } + + if(env('TELEGRAM_BOT_TOKEN') && env('TELEGRAM_CHAT_ID')) { + try { + Notification::route(TelegramChannel::class, env('TELEGRAM_CHAT_ID')) + ->notify(new SpeedtestCompleteTelegram($data)); + } catch(Exception $e) { + Log::notice('Your telegram settings are invalid'); + Log::notice($e); + } + } } } diff --git a/app/Notifications/SpeedtestComplete.php b/app/Notifications/SpeedtestCompleteSlack.php similarity index 65% rename from app/Notifications/SpeedtestComplete.php rename to app/Notifications/SpeedtestCompleteSlack.php index a4e542b9..485cf1d5 100644 --- a/app/Notifications/SpeedtestComplete.php +++ b/app/Notifications/SpeedtestCompleteSlack.php @@ -6,8 +6,11 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\SlackMessage; +use Illuminate\Support\Facades\Log; +use NotificationChannels\Telegram\TelegramChannel; +use NotificationChannels\Telegram\TelegramMessage; -class SpeedtestComplete extends Notification +class SpeedtestCompleteSlack extends Notification { use Queueable; @@ -20,6 +23,9 @@ class SpeedtestComplete extends Notification */ public function __construct($speedtest) { + $speedtest->ping = number_format((float)$speedtest->ping, 1, '.', ''); + $speedtest->download = number_format((float)$speedtest->download, 1, '.', ''); + $speedtest->upload = number_format((float)$speedtest->upload, 1, '.', ''); $this->speedtest = $speedtest; } @@ -31,7 +37,9 @@ class SpeedtestComplete extends Notification */ public function via($notifiable) { - return ['slack']; + return [ + 'slack', + ]; } /** @@ -48,9 +56,9 @@ class SpeedtestComplete extends Notification ->attachment(function ($attachment) use ($speedtest) { $attachment->title('New speedtest') ->fields([ - 'Ping' => number_format((float)$speedtest->ping, 1, '.', '') . ' ms', - 'Download' => number_format((float)$speedtest->download, 1, '.', '') . ' Mbit/s', - 'Upload' => number_format((float)$speedtest->upload, 1, '.', '') . ' Mbit/s', + 'Ping' => $speedtest->ping . ' ms', + 'Download' => $speedtest->download . ' Mbit/s', + 'Upload' => $speedtest->upload . ' Mbit/s', ]); }); } diff --git a/app/Notifications/SpeedtestCompleteTelegram.php b/app/Notifications/SpeedtestCompleteTelegram.php new file mode 100644 index 00000000..b7cff04c --- /dev/null +++ b/app/Notifications/SpeedtestCompleteTelegram.php @@ -0,0 +1,73 @@ +ping = number_format((float)$speedtest->ping, 1, '.', ''); + $speedtest->download = number_format((float)$speedtest->download, 1, '.', ''); + $speedtest->upload = number_format((float)$speedtest->upload, 1, '.', ''); + $this->speedtest = $speedtest; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return [ + TelegramChannel::class + ]; + } + + /** + * Format tekegram notification + * + * @param mixed $notifiable + * @return TelegramMessage + */ + public function toTelegram($notifiable) + { + $speedtest = $this->speedtest; + $msg = "*New Speedtest* +Ping: *$speedtest->ping* +Download: *$speedtest->download* +Upload: *$speedtest->upload*"; + return TelegramMessage::create() + ->to(env('TELEGRAM_CHAT_ID')) + ->content($msg) + ->options(['parse_mode' => 'Markdown']); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/changelog.json b/changelog.json index 0dacfe82..55366cb6 100644 --- a/changelog.json +++ b/changelog.json @@ -1,4 +1,10 @@ { + "1.6.0": [ + { + "description": "Added telegram notifications", + "link": "" + } + ], "1.5.6": [ { "description": "Auto-update all tests table", diff --git a/composer.json b/composer.json index 15d1f68c..8d1bb40a 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "fideloper/proxy": "^4.2", "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^6.3", + "laravel-notification-channels/telegram": "^0.4.0", "laravel/framework": "^7.0", "laravel/slack-notification-channel": "^2.0", "laravel/tinker": "^2.0", diff --git a/composer.lock b/composer.lock index c36c12eb..b2428d6d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ea45a7c53e5c9b5a8599abd05c8ed7ad", + "content-hash": "1f8e2d768bdcfbd5bf18db3f114479c8", "packages": [ { "name": "asm89/stack-cors", @@ -981,6 +981,67 @@ ], "time": "2019-07-01T23:21:34+00:00" }, + { + "name": "laravel-notification-channels/telegram", + "version": "0.4.0", + "source": { + "type": "git", + "url": "https://github.com/laravel-notification-channels/telegram.git", + "reference": "9e4bb2fbf1a7a06e8849fa2d50bf57fa7c4483e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel-notification-channels/telegram/zipball/9e4bb2fbf1a7a06e8849fa2d50bf57fa7c4483e3", + "reference": "9e4bb2fbf1a7a06e8849fa2d50bf57fa7c4483e3", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/guzzle": "^6.2", + "illuminate/notifications": "^5.5 || ^6.0 || ^7.0", + "illuminate/support": "^5.5 || ^6.0 || ^7.0", + "php": ">=7.1" + }, + "require-dev": { + "mockery/mockery": "^1.3", + "phpunit/phpunit": "^7.0 || ^8.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "NotificationChannels\\Telegram\\TelegramServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "NotificationChannels\\Telegram\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Irfaq Syed", + "email": "syed@lukonet.com", + "homepage": "https://lukonet.com", + "role": "Developer" + } + ], + "description": "Telegram Notifications Channel for Laravel", + "homepage": "https://github.com/laravel-notification-channels/telegram", + "keywords": [ + "laravel", + "notification", + "telegram", + "telegram notification", + "telegram notifications channel" + ], + "time": "2020-06-02T06:05:27+00:00" + }, { "name": "laravel/framework", "version": "v7.16.1", diff --git a/config/app.php b/config/app.php index f22cd9ca..a1c2c0ba 100644 --- a/config/app.php +++ b/config/app.php @@ -162,6 +162,8 @@ return [ Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, + NotificationChannels\Telegram\TelegramServiceProvider::class, + /* * Package Service Providers... */ diff --git a/config/services.php b/config/services.php index 2a1d616c..88eb96e4 100644 --- a/config/services.php +++ b/config/services.php @@ -30,4 +30,8 @@ return [ 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], + 'telegram-bot-api' => [ + 'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR BOT TOKEN HERE') + ], + ]; diff --git a/config/speedtest.php b/config/speedtest.php index fc4c7c58..dd6b4eda 100644 --- a/config/speedtest.php +++ b/config/speedtest.php @@ -7,7 +7,7 @@ return [ |-------------------------------------------------------------------------- */ - 'version' => '1.5.6', + 'version' => '1.6.0', /* |--------------------------------------------------------------------------