SendTelegramMessageJob.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Jobs;
  3. use App\Jobs\Concerns\HandlesTelegramJobFailure;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Services\BaseService;
  11. class SendTelegramMessageJob implements ShouldQueue
  12. {
  13. use Dispatchable, HandlesTelegramJobFailure, InteractsWithQueue, Queueable, SerializesModels;
  14. public $tries = 1;
  15. public $timeout = 85;
  16. public $chatId;
  17. public $text;
  18. public $buttons;
  19. public $image;
  20. public $isTop;
  21. /**
  22. * @param string $chatId
  23. * @param string $text
  24. * @param array $buttons
  25. * @param string|null $image
  26. */
  27. public function __construct($chatId, $text, $buttons = [], $image = null ,$isTop = false)
  28. {
  29. $this->chatId = $chatId;
  30. $this->text = $text;
  31. $this->buttons = $buttons;
  32. $this->image = $image;
  33. $this->isTop = $isTop;
  34. }
  35. public function handle()
  36. {
  37. try {
  38. BaseService::sendMessage($this->chatId ,$this->text, $this->buttons, $this->image);
  39. Log::channel('issue')->info('Telegram私聊消息发送成功', [
  40. 'attempt' => $this->attempts(),
  41. 'chat_id' => $this->chatId,
  42. 'text_length' => strlen((string)$this->text),
  43. 'has_image' => !empty($this->image),
  44. ]);
  45. } catch (\Throwable $exception) {
  46. $this->handleTelegramJobFailure($exception, 'Telegram私聊消息发送失败', [
  47. 'chat_id' => $this->chatId,
  48. 'text_length' => strlen((string)$this->text),
  49. 'has_image' => !empty($this->image),
  50. ], false);
  51. }
  52. }
  53. }