seven 1 долоо хоног өмнө
parent
commit
e6d510d2bb

+ 85 - 46
app/Services/BaseService.php

@@ -129,62 +129,101 @@ class BaseService
         return app(Api::class);
     }
 
+    // /**
+    //  * @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();
+
+    //     $maxLen = 1024; // Telegram 限制:最多 1024 个字符
+    //     $textParts = [];
+
+    //     $textLength = mb_strlen($text, 'UTF-8');
+    //     for ($i = 0; $i < $textLength; $i += $maxLen) {
+    //         $textParts[] = mb_substr($text, $i, $maxLen, 'UTF-8');
+    //     }
+
+    //     $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);
+    //     }
+
+    //     if ($isTop && $firstMessageId) {
+    //         $telegram->pinChatMessage([
+    //             'chat_id' => "@{$bettingGroup}",
+    //             'message_id' => $firstMessageId
+    //         ]);
+    //     }
+    // }
+
     /**
-     * @description: 群组通知(自动分段发送,支持中文与多字节字符)
-     * @param string $text 通知内容
-     * @param array $keyboard 操作按钮
-     * @param string $image 图片路径(可选)
-     * @param bool $isTop 是否置顶第一条消息
-     */
-    public static function bettingGroupNotice($text, $keyboard = [], $image = '', $isTop = false)
+     * @description: 群组通知
+     * @param {string} $text 通知内容
+     * @param {array} $keyboard 操作按钮
+     * @param {*string} $image  图片
+     * @return {*}
+     */    
+    public static function bettingGroupNotice($text ,$keyboard = [], $image = '' ,$isTop = false)
     {
         $bettingGroup = Config::where('field', 'betting_group')->first()->val;
-        $telegram = self::telegram();
 
-        $maxLen = 1024; // Telegram 限制:最多 1024 个字符
-        $textParts = [];
-
-        $textLength = mb_strlen($text, 'UTF-8');
-        for ($i = 0; $i < $textLength; $i += $maxLen) {
-            $textParts[] = mb_substr($text, $i, $maxLen, 'UTF-8');
+        $botMsg = [
+            'chat_id' => "@{$bettingGroup}",
+            'text' => $text
+        ];
+        if(count($keyboard)>0){
+            $botMsg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
         }
-
-        $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);
+        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);
         }
 
-        if ($isTop && $firstMessageId) {
-            $telegram->pinChatMessage([
+        if($isTop == true){
+            // 获取消息ID
+            $messageId = $response->get('message_id');
+            // 置顶消息
+            self::telegram()->pinChatMessage([
                 'chat_id' => "@{$bettingGroup}",
-                'message_id' => $firstMessageId
+                'message_id' => $messageId
             ]);
         }
+        
     }
 
     /**