Quellcode durchsuchen

Merge branch 'master' of http://47.76.126.2:3000/seven/bot-28

Ken vor 1 Woche
Ursprung
Commit
41b257eedc
1 geänderte Dateien mit 46 neuen und 27 gelöschten Zeilen
  1. 46 27
      app/Services/BaseService.php

+ 46 - 27
app/Services/BaseService.php

@@ -130,42 +130,61 @@ class BaseService
     }
 
     /**
-     * @description: 群组通知
-     * @param {string} $text 通知内容
-     * @param {array} $keyboard 操作按钮
-     * @param {*string} $image  图片
-     * @return {*}
-     */    
-    public static function bettingGroupNotice($text ,$keyboard = [], $image = '' ,$isTop = false)
+     * @description: 群组通知(自动分段发送,支持中文与多字节字符)
+     * @param string $text 通知内容
+     * @param array $keyboard 操作按钮
+     * @param string $image 图片路径(可选)
+     * @param bool $isTop 是否置顶第一条消息
+     */
+    public static function bettingGroupNotice($text, $keyboard = [], $image = '', $isTop = false)
     {
         $bettingGroup = Config::where('field', 'betting_group')->first()->val;
+        $telegram = self::telegram();
 
-        $botMsg = [
-            'chat_id' => "@{$bettingGroup}",
-            'text' => $text
-        ];
-        if(count($keyboard)>0){
-            $botMsg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
+        $maxLen = 1024; // 每条最多 1024 个字符(不是字节)
+        $textParts = [];
+
+        $textLength = mb_strlen($text, 'UTF-8');
+        for ($i = 0; $i < $textLength; $i += $maxLen) {
+            $textParts[] = mb_substr($text, $i, $maxLen, 'UTF-8');
         }
-        if(!empty($image)){
-            $botMsg['photo'] = InputFile::create($image);
-            $botMsg['caption'] = $text;
-            $botMsg['protect_content'] = true;  // 防止转发
-            $response = self::telegram()->sendPhoto($botMsg);
-        }else{
-            $response = self::telegram()->sendMessage($botMsg);
+
+        $firstMessageId = null;
+
+        foreach ($textParts as $index => $partText) {
+            $botMsg = [
+                'chat_id' => "@{$bettingGroup}",
+                'text' => $partText,
+            ];
+
+            if (count($keyboard) > 0 && $index === 0) {
+                $botMsg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
+            }
+
+            if (!empty($image) && $index === 0) {
+                // 第一条带图片
+                $botMsg['photo'] = InputFile::create($image);
+                $botMsg['caption'] = $partText;
+                $botMsg['protect_content'] = true;
+                $response = $telegram->sendPhoto($botMsg);
+            } else {
+                $response = $telegram->sendMessage($botMsg);
+            }
+
+            if ($isTop && $index === 0 && $response && $response->get('message_id')) {
+                $firstMessageId = $response->get('message_id');
+            }
+
+            // 防止被限流
+            usleep(300000); // 0.3 秒
         }
 
-        if($isTop == true){
-            // 获取消息ID
-            $messageId = $response->get('message_id');
-            // 置顶消息
-            self::telegram()->pinChatMessage([
+        if ($isTop && $firstMessageId) {
+            $telegram->pinChatMessage([
                 'chat_id' => "@{$bettingGroup}",
-                'message_id' => $messageId
+                'message_id' => $firstMessageId
             ]);
         }
-        
     }
 
     /**