TicketReplied.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Notifications;
  3. use App\Models\TicketReply;
  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 TicketReplied extends Notification implements ShouldQueue
  10. {
  11. use Queueable;
  12. private TicketReply $reply;
  13. private string $url;
  14. private bool $is_user;
  15. public function __construct(TicketReply $reply, string $url, bool $is_user = false)
  16. {
  17. $this->reply = $reply;
  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_replied_notification');
  24. }
  25. public function toMail($notifiable): MailMessage
  26. {
  27. return (new MailMessage)
  28. ->subject(trans('notification.reply_ticket', ['title' => $this->reply->ticket->title]))
  29. ->line(trans('notification.ticket_content'))
  30. ->line(strip_tags($this->reply->content))
  31. ->action(trans('notification.view_ticket'), $this->url);
  32. }
  33. public function toCustom($notifiable): array
  34. {
  35. return [
  36. 'title' => trans('notification.reply_ticket', ['title' => $this->reply->ticket->title]),
  37. 'content' => trans('notification.ticket_content').strip_tags($this->reply->content),
  38. ];
  39. }
  40. public function toTelegram($notifiable): TelegramMessage
  41. {
  42. return TelegramMessage::create()
  43. ->token(sysConfig('telegram_token'))
  44. ->content($this->markdownMessage($this->reply))
  45. ->button(trans('notification.view_ticket'), $this->url);
  46. }
  47. private function markdownMessage(TicketReply $reply): string
  48. {
  49. return "📮工单回复提醒 #{$reply->ticket->id}\n———————————————\n主题:\n`{$reply->ticket->title}`\n内容:\n`$reply->content`";
  50. }
  51. public function toBark($notifiable): array
  52. {
  53. return [
  54. 'title' => trans('notification.reply_ticket', ['title' => $this->reply->ticket->title]),
  55. 'content' => trans('notification.ticket_content').strip_tags($this->reply->content),
  56. 'group' => '工单',
  57. 'icon' => asset('assets/images/notification/ticket.png'),
  58. 'url' => $this->url,
  59. ];
  60. }
  61. }