SendTelegramMessageJob.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 SendTelegramMessageJob 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. /**
  21. * @param string $chatId
  22. * @param string $text
  23. * @param array $buttons
  24. * @param string|null $image
  25. */
  26. public function __construct( $text, $buttons = [], $image = null ,$isTop = false)
  27. {
  28. $this->text = $text;
  29. $this->buttons = $buttons;
  30. $this->image = $image;
  31. $this->isTop = $isTop;
  32. }
  33. public function handle()
  34. {
  35. try {
  36. BaseService::bettingGroupNotice($this->text, $this->buttons, $this->image,$this->isTop);
  37. } catch (\Telegram\Bot\Exceptions\TelegramResponseException $e) {
  38. // 捕获 Too Many Requests
  39. if (str_contains($e->getMessage(), 'Too Many Requests')) {
  40. preg_match('/retry after (\d+)/', $e->getMessage(), $matches);
  41. $retryAfter = $matches[1] ?? 5;
  42. Log::warning("Telegram 429 限制,等待 {$retryAfter} 秒重试...");
  43. sleep($retryAfter + 1);
  44. // 重试
  45. $this->handle();
  46. } else {
  47. Log::error('Telegram 消息发送失败: '.$e->getMessage());
  48. }
  49. }
  50. }
  51. }