TicketCreated.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. public function toTelegram($notifiable)
  41. {
  42. return TelegramMessage::create()
  43. ->token(sysConfig('telegram_token'))
  44. ->content($this->markdownMessage($this->ticket))
  45. ->button(trans('notification.view_ticket'), $this->url);
  46. }
  47. private function markdownMessage($ticket)
  48. {
  49. return "📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->title}`\n内容:\n`{$ticket->content}`";
  50. }
  51. }