TicketCreated.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Notifications;
  3. use App\Models\Ticket;
  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 TicketCreated extends Notification implements ShouldQueue
  10. {
  11. use Queueable;
  12. private $ticket;
  13. private $url;
  14. private $is_user;
  15. public function __construct(Ticket $ticket, $url, $is_user = false)
  16. {
  17. $this->ticket = $ticket;
  18. $this->url = $url;
  19. $this->is_user = $is_user;
  20. }
  21. public function via($notifiable)
  22. {
  23. return $this->is_user ? ['mail'] : sysConfig('ticket_created_notification');
  24. }
  25. public function toMail($notifiable)
  26. {
  27. return (new MailMessage)
  28. ->subject(trans('notification.new_ticket', ['title' => $this->ticket->title]))
  29. ->line(trans('notification.ticket_content'))
  30. ->line($this->ticket->content)
  31. ->action(trans('notification.view_ticket'), $this->url);
  32. }
  33. public function toCustom($notifiable)
  34. {
  35. return [
  36. 'title' => trans('notification.new_ticket', ['title' => $this->ticket->title]),
  37. 'content' => trans('notification.ticket_content').strip_tags($this->ticket->content),
  38. ];
  39. }
  40. /**
  41. * @param $notifiable
  42. * @return TelegramMessage|\NotificationChannels\Telegram\Traits\HasSharedLogic
  43. */
  44. public function toTelegram($notifiable)
  45. {
  46. return TelegramMessage::create()
  47. ->token(sysConfig('telegram_token'))
  48. ->content($this->markdownMessage($this->ticket))
  49. ->button(trans('notification.view_ticket'), $this->url);
  50. }
  51. private function markdownMessage($ticket)
  52. {
  53. return "📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->title}`\n内容:\n`{$ticket->content}`";
  54. }
  55. }