mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-21 13:23:04 +01:00
Added telegram notifications
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
58
app/Notifications/SpeedtestAbsoluteThresholdTelegram.php
Normal file
58
app/Notifications/SpeedtestAbsoluteThresholdTelegram.php
Normal 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']);
|
||||
}
|
||||
}
|
||||
58
app/Notifications/SpeedtestPercentageThresholdTelegram.php
Normal file
58
app/Notifications/SpeedtestPercentageThresholdTelegram.php
Normal 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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user