NodeRenewal.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Notifications;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Notifications\Messages\MailMessage;
  5. use Illuminate\Notifications\Notification;
  6. use NotificationChannels\Telegram\TelegramMessage;
  7. class NodeRenewal extends Notification
  8. {
  9. use Queueable;
  10. private array $nodes;
  11. public function __construct(array $nodes)
  12. {
  13. $this->nodes = $nodes;
  14. }
  15. public function via(object $notifiable): array
  16. {
  17. return sysConfig('node_renewal_notification');
  18. }
  19. public function toMail(object $notifiable): MailMessage
  20. {
  21. return (new MailMessage)
  22. ->subject(trans('notification.node_renewal'))
  23. ->markdown('mail.simpleMarkdown', ['title' => trans('notification.node_renewal_content'), 'content' => $this->markdownMessage(), 'url' => route('admin.node.index')]);
  24. }
  25. private function markdownMessage(): string
  26. {
  27. $content = '';
  28. foreach ($this->nodes as $node) {
  29. $content .= "- $node".PHP_EOL;
  30. }
  31. return trim($content);
  32. }
  33. public function toBark(object $notifiable): array
  34. {
  35. return [
  36. 'title' => trans('notification.node_renewal'),
  37. 'content' => trans('notification.node_renewal_blade', ['nodes' => $this->stringMessage()]),
  38. 'group' => trans('common.bark.node_status'),
  39. 'icon' => asset('assets/images/notification/renewal.png'),
  40. ];
  41. }
  42. private function stringMessage(): string
  43. {
  44. $content = '';
  45. foreach ($this->nodes as $node) {
  46. $content .= "$node | ";
  47. }
  48. return rtrim($content, ' | '); // Remove trailing separator
  49. }
  50. public function toCustom($notifiable): array
  51. {
  52. return [
  53. 'title' => trans('notification.node_renewal'),
  54. 'content' => trans('notification.node_renewal_blade', ['nodes' => $this->stringMessage()]),
  55. 'url_type' => 'markdown',
  56. ];
  57. }
  58. public function toTelegram(object $notifiable): TelegramMessage
  59. {
  60. return TelegramMessage::create()
  61. ->token(sysConfig('telegram_token'))
  62. ->content(trans('notification.node_renewal').":\n".trans('notification.node_renewal_content')."\n".$this->markdownMessage());
  63. }
  64. public function toDataBase(object $notifiable): array
  65. {
  66. return [
  67. 'nodes' => $this->stringMessage(),
  68. ];
  69. }
  70. }