NodeBlocked.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. ];
  49. }
  50. public function toTelegram($notifiable)
  51. {
  52. return TelegramMessage::create()
  53. ->token(sysConfig('telegram_token'))
  54. ->content($this->markdownMessage());
  55. }
  56. }