SendTelegramGroupMessageJob.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 SendTelegramGroupMessageJob 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. public $separator;
  22. public $meta = [];
  23. /**
  24. * @param string $chatId
  25. * @param string $text
  26. * @param array $buttons
  27. * @param string|null $image
  28. * @param string $separator 分隔符
  29. * @param array $meta 日志上下文
  30. */
  31. public function __construct($text, $buttons = [], $image = null, $isTop = false, $separator = "\n", $meta = [])
  32. {
  33. $this->text = $text;
  34. $this->buttons = $buttons;
  35. $this->image = $image;
  36. $this->isTop = $isTop;
  37. $this->separator = $separator;
  38. $this->meta = $meta;
  39. }
  40. public function handle()
  41. {
  42. $meta = is_array($this->meta) ? $this->meta : [];
  43. try {
  44. BaseService::bettingGroupNotice($this->text, $this->buttons, $this->image, $this->isTop, $this->separator);
  45. Log::channel('issue')->info('Telegram群消息发送成功', array_merge([
  46. 'attempt' => $this->attempts(),
  47. 'text_length' => strlen((string)$this->text),
  48. 'has_image' => !empty($this->image),
  49. 'is_top' => $this->isTop,
  50. 'message_type' => $meta['message_type'] ?? 'unknown',
  51. ], $meta));
  52. } catch (\Throwable $exception) {
  53. $this->handleTelegramJobFailure($exception, 'Telegram群消息发送失败', array_merge([
  54. 'text_length' => strlen((string)$this->text),
  55. 'has_image' => !empty($this->image),
  56. 'is_top' => $this->isTop,
  57. 'message_type' => $meta['message_type'] ?? 'unknown',
  58. ], $meta), false);
  59. }
  60. }
  61. }