Files
coruna-lab/app/Jobs/SendTelegramMessage.php
2026-09-16 05:35:38 +08:00

61 lines
1.6 KiB
PHP

<?php
namespace App\Jobs;
use App\Services\TelegramNotifier;
use App\Support\SafeLog;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use RuntimeException;
use Throwable;
class SendTelegramMessage implements ShouldQueue
{
use Queueable;
public int $tries = 4;
public int $timeout = 25;
public function __construct(
public string $chatId,
public string $text,
public ?string $token = null,
) {}
/** @return list<int> */
public function backoff(): array
{
return [10, 60, 300];
}
public function handle(TelegramNotifier $telegram): void
{
$result = $telegram->sendToChat($this->chatId, $this->text, $this->token);
if ($result['ok']) {
return;
}
$error = (string) ($result['error'] ?? 'unknown');
SafeLog::channel('telegram')->warning('SendTelegramMessage job failed', [
'chat_id' => $this->chatId,
'error' => $error,
'text_preview' => mb_substr($this->text, 0, 80),
'retryable' => TelegramNotifier::isRetryableSendError($error),
]);
if (TelegramNotifier::isRetryableSendError($error)) {
throw new RuntimeException('telegram send failed: '.$error);
}
}
public function failed(?Throwable $e): void
{
SafeLog::channel('telegram')->error('SendTelegramMessage given up after retries', [
'chat_id' => $this->chatId,
'error' => $e?->getMessage(),
'text_preview' => mb_substr($this->text, 0, 80),
]);
}
}