Added telegram notifications

This commit is contained in:
Henry Whitaker
2020-08-21 23:14:41 +01:00
parent 6e712cbbdc
commit 30657a07e8
3 changed files with 156 additions and 15 deletions

View File

@@ -5,9 +5,11 @@ namespace App\Listeners;
use App\Helpers\SettingsHelper;
use App\Helpers\SpeedtestHelper;
use App\Notifications\SpeedtestAbsoluteThresholdNotificationSlack;
use App\Notifications\SpeedtestAbsoluteThresholdTelegram;
use App\Notifications\SpeedtestCompleteSlack;
use App\Notifications\SpeedtestCompleteTelegram;
use App\Notifications\SpeedtestPercentageThresholdNotificationSlack;
use App\Notifications\SpeedtestPercentageThresholdTelegram;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
@@ -35,33 +37,56 @@ class SpeedtestCompleteListener
*/
public function handle($event)
{
Log::info('handling event');
Log::info(SettingsHelper::get('threshold_alert_percentage_notifications')->value);
if((bool)SettingsHelper::get('threshold_alert_percentage_notifications')->value == true) {
$data = $event->speedtest;
$errors = SpeedtestHelper::testIsLowerThanThreshold('percentage', $data);
if(sizeof($errors) > 0) {
try {
Notification::route('slack', SettingsHelper::get('slack_webhook')->value)
->notify(new SpeedtestPercentageThresholdNotificationSlack($errors));
} catch(Exception $e) {
//
if(SettingsHelper::get('slack_webhook')->value) {
try {
Notification::route('slack', SettingsHelper::get('slack_webhook')->value)
->notify(new SpeedtestPercentageThresholdNotificationSlack($errors));
} catch(Exception $e) {
Log::notice('Your sleck webhook is invalid');
Log::notice($e);
}
}
if(SettingsHelper::get('telegram_bot_token')->value == true && SettingsHelper::get('telegram_chat_id')->value == true) {
try {
config([ 'services.telegram-bot-api' => [ 'token' => SettingsHelper::get('telegram_bot_token')->value ] ]);
Notification::route(TelegramChannel::class, SettingsHelper::get('telegram_chat_id')->value)
->notify(new SpeedtestPercentageThresholdTelegram($errors));
} catch(Exception $e) {
Log::notice('Your telegram settings are invalid');
Log::notice($e);
}
}
}
}
if((bool)SettingsHelper::get('threshold_alert_absolute_notifications')->value == true) {
$data = $event->speedtest;
Log::info('absolute nots enabled');
Log::info($data);
$errors = SpeedtestHelper::testIsLowerThanThreshold('absolute', $data);
Log::info($errors);
if(sizeof($errors) > 0) {
try {
Notification::route('slack', SettingsHelper::get('slack_webhook')->value)
->notify(new SpeedtestAbsoluteThresholdNotificationSlack($errors));
} catch(Exception $e) {
//
if(SettingsHelper::get('slack_webhook')->value) {
try {
Notification::route('slack', SettingsHelper::get('slack_webhook')->value)
->notify(new SpeedtestAbsoluteThresholdNotificationSlack($errors));
} catch(Exception $e) {
Log::notice('Your sleck webhook is invalid');
Log::notice($e);
}
}
if(SettingsHelper::get('telegram_bot_token')->value == true && SettingsHelper::get('telegram_chat_id')->value == true) {
try {
config([ 'services.telegram-bot-api' => [ 'token' => SettingsHelper::get('telegram_bot_token')->value ] ]);
Notification::route(TelegramChannel::class, SettingsHelper::get('telegram_chat_id')->value)
->notify(new SpeedtestAbsoluteThresholdTelegram($errors));
} catch(Exception $e) {
Log::notice('Your telegram settings are invalid');
Log::notice($e);
}
}
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Notifications;
use App\Helpers\NotificationsHelper;
use App\Helpers\SettingsHelper;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramChannel;
use NotificationChannels\Telegram\TelegramMessage;
class SpeedtestAbsoluteThresholdTelegram extends Notification
{
use Queueable;
private $errors;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($errors)
{
$this->errors = $errors;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [
TelegramChannel::class
];
}
/**
* Format telegram notification
*
* @param mixed $notifiable
* @return TelegramMessage
*/
public function toTelegram($notifiable)
{
$msg = NotificationsHelper::formatAbsoluteThresholdMessage($this->errors);
return TelegramMessage::create()
->to(SettingsHelper::get('telegram_chat_id')->value)
->content($msg)
->options(['parse_mode' => 'Markdown']);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Notifications;
use App\Helpers\NotificationsHelper;
use App\Helpers\SettingsHelper;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramChannel;
use NotificationChannels\Telegram\TelegramMessage;
class SpeedtestPercentageThresholdTelegram extends Notification
{
use Queueable;
private $errors;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($errors)
{
$this->errors = $errors;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [
TelegramChannel::class
];
}
/**
* Format telegram notification
*
* @param mixed $notifiable
* @return TelegramMessage
*/
public function toTelegram($notifiable)
{
$msg = NotificationsHelper::formatAbsoluteThresholdMessage($this->errors);
return TelegramMessage::create()
->to(SettingsHelper::get('telegram_chat_id')->value)
->content($msg)
->options(['parse_mode' => 'Markdown']);
}
}