Ken 2 ماه پیش
والد
کامیت
da136a8ea6
3فایلهای تغییر یافته به همراه68 افزوده شده و 49 حذف شده
  1. 17 48
      app/Http/Controllers/api/TelegramWebHook.php
  2. 30 0
      app/Services/KeyboardService.php
  3. 21 1
      app/Services/PublicService.php

+ 17 - 48
app/Http/Controllers/api/TelegramWebHook.php

@@ -61,20 +61,11 @@ class TelegramWebHook extends BaseController
                 // Log::error('Telegram 回调数据(JSON): ' . json_encode($update, JSON_UNESCAPED_UNICODE));
                 DB::beginTransaction();
                 try {
-                    $chatId = $message->chat->id;
-                    $firstName = $message->chat->firstName;
-                    $username = '';
                     $messageId = $message->messageId;
-                    if (!$from->isBot) {
-                        $chatId = $from->id;
-                        $firstName = $from->firstName;
-                        $username = $from->username;
-                    }
-
+                    list($chatId, $firstName, $username) = PublicService::getChatInfo($message, $from);
                     //用户注册和初始化用户钱包
                     $user = PublicService::index($chatId, $username, $firstName);
                     App::setLocale($user->language);
-
                     PublicService::init($telegram, $data, $chatId, $firstName, $messageId);
                     WalletService::init($telegram, $data, $chatId, $firstName, $messageId, $callbackId);
                     TopUpService::init($telegram, $data, $chatId, $firstName, $messageId);
@@ -86,8 +77,6 @@ class TelegramWebHook extends BaseController
                     BetService::init($telegram, $data, $chatId, $firstName, $messageId, $callbackId);
                     BalanceLogService::init($telegram, $data, $chatId, $firstName, $messageId, $callbackId);
                     IssueService::init($telegram, $data, $chatId, $firstName, $messageId);
-
-
                     DB::commit();
                 } //
                 catch (MessageException $e) {
@@ -204,7 +193,7 @@ class TelegramWebHook extends BaseController
      */
     public function processChatMessage($chatId, $messageId, $message, $from)
     {
-        $returnMsg = [];
+
         //用户发送图片,结算截图
         if (isset($message['photo'])) {
             $stepStatus = Cache::get(get_step_key($chatId), -1);
@@ -225,64 +214,44 @@ class TelegramWebHook extends BaseController
         } //用户发送了消息
         else if (isset($message['text'])) {
             $text = $message['text'];
-            $user = PublicSerVICE::index($chatId, $message['chat']['username'], $message['chat']['first_name']);
+            $user = PublicService::index($chatId, $message['chat']['username'], $message['chat']['first_name']);
             App::setLocale($user->language);
             if ($message['chat']['type'] === 'private') {
                 // 校验开始菜单事件
                 $returnMsg = KeyboardService::checkStart($chatId, $text);
-                if ($returnMsg) {
-                    return $returnMsg;
-                }
+                if ($returnMsg) return $returnMsg;
                 switch ($text) {
                     case "/start":
                         Util::delCache($chatId);
-                        //给每个用户生成一个专属的USDT钱包
-                        WalletService::getUserWallet($chatId);
                         self::setReplyKeyboard($chatId, $user->language);
                         break;
                     default:
-                        // 关键字回复
-                        $keyboardText = KeyboardService::findOne(['button' => $text]);
-                        if ($keyboardText) {
-                            $keyboard = [];
-                            if ($keyboardText['buttons']) {
-                                $keyboard = json_decode($keyboardText['buttons'], true);
-                            }
-                            Util::delCache($chatId);
-                            $res = [
-                                'chat_id' => $chatId,
-                                'text' => $keyboardText['reply'],
-                                // 'reply_to_message_id' => $messageId
-                            ];
-                            if ($keyboard) {
-                                $res['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
-                            }
-                            if ($keyboardText['image']) {
-                                $res['photo'] = InputFile::create(url($keyboardText['image']));
-                                unset($res['text']);
-                                $res['caption'] = $text;
-                                $res['protect_content'] = true;  // 防止转发
-                            }
-                            return $res;
-                        }
-                        $stepStatus = Cache::get(get_step_key($chatId), -1);
-                        $stepStatus = intval($stepStatus);
+                        //关键字回复
+                        $res = KeyboardService::getKeyWordReply($chatId, $text);
+                        if (!empty($res)) return $res;
+
+                        $stepStatus = intval(Cache::get(get_step_key($chatId), -1));
+
                         $res = QianBaoWithdrawService::onMessage($chatId, $text, $messageId, $stepStatus);
                         if (empty($res)) $res = SanJinRechargeService::onMessage($chatId, $text, $messageId, $stepStatus);
                         if (empty($res)) $res = SecretService::onMessage($chatId, $text, $messageId, $stepStatus);
                         if (!empty($res)) return $res;
+
+
                         switch ($stepStatus) {
                             case StepStatus::INPUT_TOP_UP_MONEY:
                                 return TopUpService::inputAmount($chatId, $text, $messageId);
                             case StepStatus::INPUT_WITHDRAW_MONEY:
-                                $res = (new WithdrawService())->inputAmount($chatId, $text, $messageId);
-                                return $res[0];
+                                return (new WithdrawService())->inputAmount($chatId, $text, $messageId)[0];
                             case StepStatus::INPUT_ADDRESS_TRC20:
                                 return WithdrawService::inputAddress($chatId, $text, $messageId);
                             case StepStatus::INPUT_ADDRESS_ALIAS:
                                 return WithdrawService::inputAlias($chatId, $text, $messageId);
+                            default:
+                                $returnMsg = BetService::bet($chatId, $text, $messageId);
+                                break;
                         }
-                        $returnMsg = BetService::bet($chatId, $text, $messageId);
+
                 }
 
                 return $returnMsg;

+ 30 - 0
app/Services/KeyboardService.php

@@ -15,6 +15,7 @@ use App\Services\WalletService;
 use App\Constants\Util;
 use Illuminate\Support\Facades\Log;
 use App\Services\SanJinRechargeService;
+use Telegram\Bot\FileUpload\InputFile;
 
 /**
  * 菜单
@@ -230,4 +231,33 @@ class KeyboardService extends BaseService
                 return false;
         }
     }
+
+    public static function getKeyWordReply($chatId,$text): ?array
+    {
+        // 关键字回复
+        $keyboardText = KeyboardService::findOne(['button' => $text]);
+        if ($keyboardText) {
+            $keyboard = [];
+            if ($keyboardText['buttons']) {
+                $keyboard = json_decode($keyboardText['buttons'], true);
+            }
+            Util::delCache($chatId);
+            $res = [
+                'chat_id' => $chatId,
+                'text' => $keyboardText['reply'],
+                // 'reply_to_message_id' => $messageId
+            ];
+            if ($keyboard) {
+                $res['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
+            }
+            if ($keyboardText['image']) {
+                $res['photo'] = InputFile::create(url($keyboardText['image']));
+                unset($res['text']);
+                $res['caption'] = $text;
+                $res['protect_content'] = true;  // 防止转发
+            }
+            return $res;
+        }
+        return null;
+    }
 }

+ 21 - 1
app/Services/PublicService.php

@@ -18,6 +18,26 @@ class PublicService extends BaseService
         }
     }
 
+    /**
+     * 获取Tg用户的 id 昵称 用户名
+     * @param $message
+     * @param $from
+     * @return array
+     */
+    public static function getChatInfo($message, $from): array
+    {
+        $chatId = $message->chat->id;
+        $firstName = $message->chat->firstName;
+        $username = '';
+        if (!$from->isBot) {
+            $chatId = $from->id;
+            $firstName = $from->firstName;
+            $username = $from->username;
+        }
+        return [$chatId, $firstName, $username];
+    }
+
+
     /**
      * 设置用户的访客ID
      * @param  $memberId string 会员编号
@@ -47,7 +67,7 @@ class PublicService extends BaseService
                 $user->register_ip = $registerIp;
             }
             $user->save();
-        }catch (Exception $e){
+        } catch (Exception $e) {
             throw new Exception('验证失败', HttpStatus::CUSTOM_ERROR);
         }
     }