seven 4 дней назад
Родитель
Сommit
0cea319729
2 измененных файлов с 71 добавлено и 46 удалено
  1. 67 45
      app/Http/Controllers/api/TelegramWebHook.php
  2. 4 1
      app/Providers/AppServiceProvider.php

+ 67 - 45
app/Http/Controllers/api/TelegramWebHook.php

@@ -26,6 +26,13 @@ use Illuminate\Support\Facades\Log;
 
 class TelegramWebHook extends Controller
 {
+    protected $telegram;
+
+    public function __construct(Api $telegram)
+    {
+        $this->telegram = $telegram;
+    }
+
     public function handle(Request $request)
     {
         Log::error('Telegram 日志写入测试: ' . json_encode([$request->ip()], JSON_UNESCAPED_UNICODE));
@@ -64,49 +71,7 @@ class TelegramWebHook extends Controller
                     $m->json = json_encode($update);
                     $m->save();
 
-                    //用户发送图片,结算截图
-                    if (isset($message['photo'])) {
-                        $stepStatus = Cache::get(get_step_key($chatId), -1);
-                        $stepStatus = intval($stepStatus);
-                        // //结算截图
-                        // if ($stepStatus === StepStatus::INPUT_IMAGE) {
-                        //     $photo = $message['photo'][count($message['photo']) - 1];
-                        //     $res = (new SettlementService())->photo($photo, $chatId);
-                        //     if ($res) $telegram->sendMessage($res);
-                        // }//
-                        // //充值截图
-                        // else if ($stepStatus === StepStatus::INPUT_TOP_UP_IMAGE) {
-                        //     $photo = $message['photo'][count($message['photo']) - 1];
-                        //     $res = TopUpService::photo($chatId, $photo);
-                        //     if (isset($res['message_id'])) $telegram->editMessageText($res);
-                        //     else $telegram->sendMessage($res);
-                        // }
-
-                    } //用户发送了消息
-                    else if (isset($message['text'])) {
-                        $text = $message['text'];
-                        if ($message['chat']['type'] === 'private') {
-                            switch ($text) {
-                                case "/start":
-                                    Util::delCache($chatId);
-                                    $user = User::where('member_id', $chatId)->first();
-                                    if (!$user) {
-                                        $user = new User();
-                                        $user->member_id = $chatId;
-                                    }
-                                    $user->first_name = $message['chat']['first_name'];
-                                    $user->save();
-                                    //给每个用户生成一个专属的USDT钱包
-                                    WalletService::getUserWallet($chatId);
-
-                                    $username = config('services.telegram.username');
-                                    $this->setReplyKeyboard($chatId);
-                                    break;
-                                default:
-                                    //输入结算分数
-                            }
-                        }
-                    }
+                    $this->processChatMessage($chatId, $messageId, $message, $message['from']);
 
                 } catch (\Exception $e) {
                     DB::rollBack();
@@ -123,10 +88,67 @@ class TelegramWebHook extends Controller
         return response()->json(['status' => 'ok']);
     }
 
-
+    /**
+     * @description: 处理聊天消息
+     * @param {*} $chatId
+     * @param {*} $messageId
+     * @param {*} $message
+     * @param {*} $from
+     * @return {*}
+     */    
     public function processChatMessage($chatId, $messageId, $message, $from)
     {
-
+        //用户发送图片,结算截图
+        if (isset($message['photo'])) {
+            $stepStatus = Cache::get(get_step_key($chatId), -1);
+            $stepStatus = intval($stepStatus);
+            // //结算截图
+            // if ($stepStatus === StepStatus::INPUT_IMAGE) {
+            //     $photo = $message['photo'][count($message['photo']) - 1];
+            //     $res = (new SettlementService())->photo($photo, $chatId);
+            //     if ($res) $telegram->sendMessage($res);
+            // }//
+            // //充值截图
+            // else if ($stepStatus === StepStatus::INPUT_TOP_UP_IMAGE) {
+            //     $photo = $message['photo'][count($message['photo']) - 1];
+            //     $res = TopUpService::photo($chatId, $photo);
+            //     if (isset($res['message_id'])) $telegram->editMessageText($res);
+            //     else $telegram->sendMessage($res);
+            // }
+
+        } //用户发送了消息
+        else if (isset($message['text'])) {
+            $text = $message['text'];
+            if ($message['chat']['type'] === 'private') {
+                switch ($text) {
+                    case "/start":
+                        Util::delCache($chatId);
+                        $user = User::where('member_id', $chatId)->first();
+                        if (!$user) {
+                            $user = new User();
+                            $user->member_id = $chatId;
+                        }
+                        $user->first_name = $message['chat']['first_name'];
+                        $user->save();
+                        //给每个用户生成一个专属的USDT钱包
+                        WalletService::getUserWallet($chatId);
+
+                        $username = config('services.telegram.username');
+                        $this->setReplyKeyboard($chatId);
+                        break;
+                    case "/tutorial":
+                        Util::delCache($chatId);
+                        
+                        break;
+                    default:
+                        //输入结算分数
+                        $this->telegram->sendMessage([
+                            'chat_id' => $chatId,
+                            'text' => '‼️‼️未知命令,请点击下方菜单按钮选择功能'
+                        ]);
+                }
+            }
+        }
     }
 
     /**

+ 4 - 1
app/Providers/AppServiceProvider.php

@@ -5,6 +5,7 @@ namespace App\Providers;
 use Illuminate\Support\ServiceProvider;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Database\Eloquent\Model;
+use Telegram\Bot\Api;
 
 class AppServiceProvider extends ServiceProvider
 {
@@ -15,7 +16,9 @@ class AppServiceProvider extends ServiceProvider
      */
     public function register()
     {
-        
+        $this->app->singleton(Api::class, function ($app) {
+            return new Api(config('services.telegram.token'));
+        });
     }
 
     /**