NodeOffline.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Notifications;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Notifications\Messages\MailMessage;
  6. use Illuminate\Notifications\Notification;
  7. use NotificationChannels\Telegram\TelegramMessage;
  8. class NodeOffline extends Notification implements ShouldQueue
  9. {
  10. use Queueable;
  11. private $data;
  12. public function __construct($data)
  13. {
  14. $this->data = $data;
  15. }
  16. public function via($notifiable)
  17. {
  18. return sysConfig('node_offline_notification');
  19. }
  20. public function toMail($notifiable)
  21. {
  22. return (new MailMessage)
  23. ->subject(trans('notification.node_offline'))
  24. ->markdown('mail.simpleMarkdown', ['title' => trans('notification.node_offline_content'), 'content' => $this->markdownMessage(), 'url' => route('admin.node.index')]);
  25. }
  26. private function markdownMessage()
  27. {
  28. $content = '';
  29. foreach ($this->data as $node) {
  30. $content .= "- {$node['name']} {$node['host']}\r\n";
  31. }
  32. return $content;
  33. }
  34. public function toCustom($notifiable)
  35. {
  36. return [
  37. 'title' => trans('notification.node_offline'),
  38. 'content' => $this->markdownMessage(),
  39. ];
  40. }
  41. public function toBark($notifiable)
  42. {
  43. return [
  44. 'title' => trans('notification.node_offline'),
  45. 'content' => $this->stringMessage(),
  46. ];
  47. }
  48. private function stringMessage()
  49. {
  50. $content = '';
  51. foreach ($this->data as $node) {
  52. $content .= "{$node['name']} {$node['host']}| ";
  53. }
  54. return $content;
  55. }
  56. /**
  57. * @param $notifiable
  58. * @return TelegramMessage|\NotificationChannels\Telegram\Traits\HasSharedLogic
  59. */
  60. public function toTelegram($notifiable)
  61. {
  62. return TelegramMessage::create()
  63. ->token(sysConfig('telegram_token'))
  64. ->content($this->markdownMessage());
  65. }
  66. }