seven 2 месяцев назад
Родитель
Сommit
0c6c13b751
2 измененных файлов с 62 добавлено и 1 удалено
  1. 58 0
      app/Jobs/SendTelegramMessageJob.php
  2. 4 1
      app/Services/IssueService.php

+ 58 - 0
app/Jobs/SendTelegramMessageJob.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Jobs;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Telegram\Bot\Api;
+use Illuminate\Support\Facades\Log;
+use App\Services\BaseService;
+use DragonCode\PrettyArray\Services\Formatters\Base;
+
+class SendTelegramMessageJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    public $chatId;
+    public $text;
+    public $buttons;
+    public $image;
+    public $isTop;
+
+    /**
+     * @param string $chatId
+     * @param string $text
+     * @param array $buttons
+     * @param string|null $image
+     */
+    public function __construct( $text, $buttons = [], $image = null ,$isTop = false)
+    {
+        $this->text = $text;
+        $this->buttons = $buttons;
+        $this->image = $image;
+        $this->isTop = $isTop;
+    }
+
+    public function handle()
+    {
+        try {
+            BaseService::bettingGroupNotice($this->text, $this->buttons, $this->image,$this->isTop);
+        } catch (\Telegram\Bot\Exceptions\TelegramResponseException $e) {
+            // 捕获 Too Many Requests
+            if (str_contains($e->getMessage(), 'Too Many Requests')) {
+                preg_match('/retry after (\d+)/', $e->getMessage(), $matches);
+                $retryAfter = $matches[1] ?? 5;
+                Log::warning("Telegram 429 限制,等待 {$retryAfter} 秒重试...");
+                sleep($retryAfter + 1);
+
+                // 重试
+                $this->handle();
+            } else {
+                Log::error('Telegram 消息发送失败: '.$e->getMessage());
+            }
+        }
+    }
+}

+ 4 - 1
app/Services/IssueService.php

@@ -17,6 +17,7 @@ use App\Http\Controllers\admin\Lottery;
 use App\Services\KeyboardService;
 use App\Services\LotteryImageService;
 use Telegram\Bot\FileUpload\InputFile;
+use App\Jobs\SendTelegramMessageJob;
 
 /**
  * 投注
@@ -267,11 +268,13 @@ class IssueService extends BaseService
                     $buttons[] = [['text' => '✅唯一财务', 'callback_data' => "", 'url' => "https://t.me/{$serviceAccount}"]];
 
                 }
-                self::bettingGroupNotice($text, $buttons, $image ,true);
+                dispatch(new SendTelegramMessageJob($text, $buttons, $image, true));
+                // self::bettingGroupNotice($text, $buttons, $image ,true);
             }
             $recordImage = self::lotteryImage($info->issue_no);
             if($recordImage){
                 self::bettingGroupNotice('', [], url($recordImage));
+                dispatch(new SendTelegramMessageJob('', [], url($recordImage)));
             }
 
             BetService::betSettled($info->issue_no, $awards);