TicketReplied.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 $reply;
  13. private $url;
  14. private $is_user;
  15. public function __construct($reply, $url, $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)
  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)
  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)
  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)
  48. {
  49. return "📮工单回复提醒 #{$reply->ticket->id}\n———————————————\n主题:\n`{$reply->ticket->title}`\n内容:\n`{$reply->content}`";
  50. }
  51. public function toBark($notifiable)
  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. }