diff --git a/app/Listeners/SpeedtestCompleteListener.php b/app/Listeners/SpeedtestCompleteListener.php index 42158fbf..010dcca0 100644 --- a/app/Listeners/SpeedtestCompleteListener.php +++ b/app/Listeners/SpeedtestCompleteListener.php @@ -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); + } } } } diff --git a/app/Notifications/SpeedtestAbsoluteThresholdTelegram.php b/app/Notifications/SpeedtestAbsoluteThresholdTelegram.php new file mode 100644 index 00000000..a0dc004f --- /dev/null +++ b/app/Notifications/SpeedtestAbsoluteThresholdTelegram.php @@ -0,0 +1,58 @@ +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']); + } +} diff --git a/app/Notifications/SpeedtestPercentageThresholdTelegram.php b/app/Notifications/SpeedtestPercentageThresholdTelegram.php new file mode 100644 index 00000000..0789fa7f --- /dev/null +++ b/app/Notifications/SpeedtestPercentageThresholdTelegram.php @@ -0,0 +1,58 @@ +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']); + } +}