NodeDailyReport.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 NodeDailyReport 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_daily_notification');
  19. }
  20. public function toMail($notifiable): MailMessage
  21. {
  22. return (new MailMessage)
  23. ->subject(__('Daily Data Usage Report'))
  24. ->markdown('mail.simpleMarkdown', ['title' => __('Daily Data Usage Report'), 'content' => $this->markdownMessage(), 'url' => route('admin.node.index')]);
  25. }
  26. private function markdownMessage(): string
  27. {
  28. $content = '| '.trans('user.attribute.node').' | '.trans('notification.node.upload').' | '.trans('notification.node.download').' | '.trans('notification.node.total')." |\r\n| :------ | :------: | :------: | ------: |\r\n";
  29. foreach ($this->data as $node) {
  30. $content .= "| {$node['name']} | {$node['upload']} | {$node['download']} | {$node['total']} |\r\n";
  31. }
  32. return $content;
  33. }
  34. public function toCustom($notifiable): array
  35. {
  36. return [
  37. 'title' => __('Daily Data Usage Report'),
  38. 'content' => $this->markdownMessage(),
  39. 'url_type' => 'markdown',
  40. ];
  41. }
  42. public function toTelegram($notifiable): TelegramMessage
  43. {
  44. return TelegramMessage::create()
  45. ->token(sysConfig('telegram_token'))
  46. ->content($this->markdownMessage());
  47. }
  48. }