Files
portal-api/app/Jobs/Ticket/TicketJobs.php
T
2025-07-24 16:18:27 +07:00

62 lines
2.2 KiB
PHP

<?php
namespace App\Jobs\Ticket;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class TicketJobs implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $ticket;
protected $customer;
/**
* Create a new job instance.
*/
public function __construct($ticket, $customer)
{
$this->ticket = $ticket;
$this->customer = $customer;
}
/**
* Execute the job.
*/
public function handle(): void
{
// Loại bỏ base64 images và strip HTML tags
$cleanDescription = $this->ticket->description;
// Loại bỏ base64 images
$cleanDescription = preg_replace('/<img[^>]+src="data:image\/[^;]+;base64,[^"]+"[^>]*>/i', '[Hình ảnh]', $cleanDescription);
// Strip HTML tags còn lại
$cleanDescription = strip_tags($cleanDescription);
// Gửi thông báo Telegram khi tạo ticket mới
$message = "🆕 Ticket mới được tạo!\n";
$message .= "👤 Khách: {$this->customer->firstname} {$this->customer->lastname} ({$this->customer->email})\n";
$message .= "🎫 Số ticket: #{$this->ticket->ticket_num}\n";
$message .= "📄 Tiêu đề: {$this->ticket->subject}\n";
$message .= "🏢 Phòng ban: {$this->ticket->room}\n";
$message .= "📝 Nội dung: " . $cleanDescription;
// Kiểm tra độ dài message (Telegram giới hạn 4096 ký tự)
if (strlen($message) > 4000) {
$message = substr($message, 0, 3990) . "...[cắt ngắn]";
}
app(\App\Http\Services\Telegram::class)->SendMessage($message);
// Nếu có file đính kèm, gửi hình ảnh lên Telegram
$data = json_decode($this->ticket->data, true);
if (!empty($data['file'])) {
$caption = "[Ticket #{$this->ticket->ticket_num}] " . ($this->ticket->subject ?? '');
app(\App\Http\Services\Telegram::class)->SendPhoto($data['file'], $caption);
}
}
}