NodeOffline.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 array $data;
  12. public function __construct(array $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): MailMessage
  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(): string
  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): array
  35. {
  36. return [
  37. 'title' => trans('notification.node_offline'),
  38. 'content' => $this->markdownMessage(),
  39. 'url_type' => 'markdown',
  40. ];
  41. }
  42. public function toBark($notifiable): array
  43. {
  44. return [
  45. 'title' => trans('notification.node_offline'),
  46. 'content' => $this->stringMessage(),
  47. 'group' => '节点状态',
  48. 'icon' => asset('assets/images/notification/offline.png'),
  49. ];
  50. }
  51. private function stringMessage(): string
  52. {
  53. $content = '';
  54. foreach ($this->data as $node) {
  55. $content .= "{$node['name']} {$node['host']}| ";
  56. }
  57. return $content;
  58. }
  59. public function toTelegram($notifiable): TelegramMessage
  60. {
  61. return TelegramMessage::create()
  62. ->token(sysConfig('telegram_token'))
  63. ->content($this->markdownMessage());
  64. }
  65. }