NodeBlocked.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Notifications;
  3. use Arr;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Notifications\Messages\MailMessage;
  7. use Illuminate\Notifications\Notification;
  8. use NotificationChannels\Telegram\TelegramMessage;
  9. class NodeBlocked extends Notification implements ShouldQueue
  10. {
  11. use Queueable;
  12. private $data;
  13. public function __construct($data)
  14. {
  15. $this->data = $data;
  16. }
  17. public function via($notifiable)
  18. {
  19. return sysConfig('node_blocked_notification');
  20. }
  21. public function toMail($notifiable)
  22. {
  23. return (new MailMessage)
  24. ->subject(trans('notification.node_block'))
  25. ->markdown('mail.simpleMarkdown', ['title' => trans('notification.block_report'), 'content' => $this->markdownMessage(), 'url' => route('admin.node.index')]);
  26. }
  27. private function markdownMessage()
  28. {
  29. $content = '| '.trans('user.attribute.node').' | IP | ICMP | TCP'." |\r\n| :------ | :------: | :------: | :------: | \r\n";
  30. $tail = '';
  31. foreach ($this->data as $node) {
  32. $case = $node;
  33. Arr::forget($case, ['message', 'name']);
  34. foreach ($case as $ip => $info) {
  35. $content .= "| {$node['name']} | {$ip} | ".($info['icmp'] ?? '✔️').' | '.($info['tcp'] ?? '✔️')." |\r\n";
  36. }
  37. if (Arr::hasAny($node, ['message'])) {
  38. $tail .= "- {$node['name']}: {$node['message']}\r\n";
  39. }
  40. }
  41. return $content.$tail;
  42. }
  43. public function toCustom($notifiable)
  44. {
  45. return [
  46. 'title' => trans('notification.node_block'),
  47. 'content' => $this->markdownMessage(),
  48. 'url_type' => 'markdown',
  49. ];
  50. }
  51. public function toTelegram($notifiable)
  52. {
  53. return TelegramMessage::create()
  54. ->token(sysConfig('telegram_token'))
  55. ->content($this->markdownMessage());
  56. }
  57. }