Added slack notifications

This commit is contained in:
Henry Whitaker
2020-04-27 18:50:01 +01:00
parent 4764aae1e4
commit fc35da9f8d
10 changed files with 221 additions and 4 deletions

View File

@@ -21,3 +21,5 @@ SPEEDTEST_PASSWORD=
# Number of days you stay logged in for
REMEMBER_DAYS=30
SLACK_WEBHOOK=

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class SpeedtestCompleteEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($speedtest)
{
$this->speedtest = $speedtest;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Jobs;
use App\Events\SpeedtestCompleteEvent;
use App\Helpers\SpeedtestHelper;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -31,7 +32,8 @@ class SpeedtestJob implements ShouldQueue
public function handle()
{
$output = shell_exec('speedtest-cli');
return SpeedtestHelper::runSpeedtest($output);
$speedtest = SpeedtestHelper::runSpeedtest($output);
event(new SpeedtestCompleteEvent($speedtest));
return $speedtest;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Listeners;
use App\Notifications\SpeedtestComplete;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
class SpeedtestCompleteListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
if(env('SLACK_WEBHOOK')) {
$data = $event->speedtest;
try {
Notification::route('slack', env('SLACK_WEBHOOK'))
->notify(new SpeedtestComplete($data));
} catch(Exception $e) {
Log::notice('Your sleck webhook is invalid');
Log::notice($e);
}
}
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;
class SpeedtestComplete extends Notification
{
use Queueable;
protected $speedtest;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($speedtest)
{
$this->speedtest = $speedtest;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}
public function toSlack($notifiable)
{
$speedtest = $this->speedtest;
return (new SlackMessage)
->warning()
->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',
]);
});
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View File

@@ -2,6 +2,8 @@
namespace App\Providers;
use App\Events\SpeedtestCompleteEvent;
use App\Listeners\SpeedtestCompleteListener;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -18,6 +20,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
SpeedtestCompleteEvent::class => [
SpeedtestCompleteListener::class,
],
];
/**

View File

@@ -1,4 +1,10 @@
{
"1.3.0": [
{
"description": "Added discord notifications",
"link": ""
}
],
"1.2.9": [
{
"description": "Updated laravel framework",

View File

@@ -13,6 +13,7 @@
"fruitcake/laravel-cors": "^1.0",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^7.0",
"laravel/slack-notification-channel": "^2.0",
"laravel/tinker": "^2.0",
"laravel/ui": "^2.0",
"tymon/jwt-auth": "^1.0"

59
composer.lock generated
View File

@@ -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": "376dfceee60ad21d948e52a97a25faaf",
"content-hash": "7de35f5352b6b3050eb2e7cdf6fd1fec",
"packages": [
{
"name": "asm89/stack-cors",
@@ -841,6 +841,63 @@
],
"time": "2020-04-24T17:21:56+00:00"
},
{
"name": "laravel/slack-notification-channel",
"version": "v2.0.2",
"source": {
"type": "git",
"url": "https://github.com/laravel/slack-notification-channel.git",
"reference": "ecc90a70791195d6f5e20b2732a5eb1eb9619d10"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/slack-notification-channel/zipball/ecc90a70791195d6f5e20b2732a5eb1eb9619d10",
"reference": "ecc90a70791195d6f5e20b2732a5eb1eb9619d10",
"shasum": ""
},
"require": {
"guzzlehttp/guzzle": "^6.0",
"illuminate/notifications": "~5.8.0|^6.0|^7.0",
"php": "^7.1.3"
},
"require-dev": {
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^7.0|^8.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
},
"laravel": {
"providers": [
"Illuminate\\Notifications\\SlackChannelServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Illuminate\\Notifications\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "Slack Notification Channel for laravel.",
"keywords": [
"laravel",
"notifications",
"slack"
],
"time": "2019-08-27T14:40:26+00:00"
},
{
"name": "laravel/tinker",
"version": "v2.4.0",

View File

@@ -7,7 +7,7 @@ return [
|--------------------------------------------------------------------------
*/
'version' => '1.2.9',
'version' => '1.3.0',
/*
|--------------------------------------------------------------------------