SendTelegramGroupMessageJob.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Telegram\Bot\Api;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Services\BaseService;
  11. use DragonCode\PrettyArray\Services\Formatters\Base;
  12. class SendTelegramGroupMessageJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. public $chatId;
  16. public $text;
  17. public $buttons;
  18. public $image;
  19. public $isTop;
  20. public $separator;
  21. /**
  22. * @param string $chatId
  23. * @param string $text
  24. * @param array $buttons
  25. * @param string|null $image
  26. * @param string $separator 分隔符
  27. */
  28. public function __construct($text, $buttons = [], $image = null, $isTop = false, $separator = "\n")
  29. {
  30. $this->text = $text;
  31. $this->buttons = $buttons;
  32. $this->image = $image;
  33. $this->isTop = $isTop;
  34. $this->separator = $separator;
  35. }
  36. public function handle()
  37. {
  38. try {
  39. BaseService::bettingGroupNotice($this->text, $this->buttons, $this->image, $this->isTop, $this->separator);
  40. } catch (\Telegram\Bot\Exceptions\TelegramResponseException $e) {
  41. // // 捕获 Too Many Requests
  42. // if (str_contains($e->getMessage(), 'Too Many Requests')) {
  43. // preg_match('/retry after (\d+)/', $e->getMessage(), $matches);
  44. // $retryAfter = $matches[1] ?? 5;
  45. // Log::warning("Telegram 429 限制,等待 {$retryAfter} 秒重试...");
  46. // sleep($retryAfter + 1);
  47. // // 重试
  48. // $this->handle();
  49. // } else {
  50. Log::error('Telegram 消息发送失败: ' . $e->getMessage());
  51. // }
  52. }
  53. }
  54. }