From 92ad9debe84baaf4fbab095f5338eb8cd251bfb7 Mon Sep 17 00:00:00 2001 From: Winchen Date: Tue, 20 May 2025 16:07:35 +0700 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20Telegram=20=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=99=A8=E8=88=87=E5=B8=B3=E6=88=B6=E6=9C=8D=E5=8B=99?= =?UTF-8?q?=E7=9B=B8=E9=97=9C=E9=82=8F=E8=BC=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/TgController.php | 39 ++++++++++++--------------- app/Models/Ledger.php | 7 +++++ app/Models/UserAccount.php | 2 +- app/Services/TgComm.php | 19 ++++++++----- app/Services/TgMsg.php | 19 +++++++++---- app/Services/UserFun.php | 14 +++++----- 6 files changed, 60 insertions(+), 40 deletions(-) diff --git a/app/Http/Controllers/TgController.php b/app/Http/Controllers/TgController.php index c89fc69..2234497 100755 --- a/app/Http/Controllers/TgController.php +++ b/app/Http/Controllers/TgController.php @@ -18,31 +18,26 @@ class TgController extends Controller public function Msg($token,Request $request) { // 立即回应 Telegram(避免超时) - response()->json(['ok' => true])->send(); - flush(); // 立即把回应送出 +// response()->json(['ok' => true])->send(); +// flush(); // 立即把回应送出 // 接着执行后续处理(异步逻辑、写 log、存数据库等) $data = $request->all(); - $data='{"update_id": 250041637, "message_date": 1746710794, "message_text": "@winchen1688 +2000", "message_chat_id": -4661252768, "message_from_id": 5909523353, "message_chat_type": "group", "message_chat_title": "一路发测试群", "message_message_id": 8, "message_from_is_bot": false, "message_from_username": "winchen1688", "message_from_last_name": "阿江", "message_entities_0_type": "mention", "message_from_first_name": "流浪的", "message_entities_0_length": 12, "message_entities_0_offset": 0, "message_from_language_code": "zh-hans", "message_chat_all_members_are_administrators": true, "message_chat_accepted_gift_types_unique_gifts": false, "message_chat_accepted_gift_types_limited_gifts": false, "message_chat_accepted_gift_types_unlimited_gifts": false, "message_chat_accepted_gift_types_premium_subscription": false}'; - $msg=UserFun::Json2Arr($data); //将JSON转换成一維扁平陣列 - $msg['token']=$token; - $tgmsg=TgMsg::storeFromWebhookJson($msg); //写入对话消息 - $UserResponse=TgApi::GetChatMember($msg); //获取用户资料 - $TgGroupUsers=TgGroupUsers::updateOrCreateFromTelegram($UserResponse['raw'], $msg['message_chat_id']); //更新写入用户资料 - $chatResponse=TgApi::GetChatInfo($msg); //获取群资料 - if($chatResponse['ok'] && isset($chatResponse['result']['id'])){ - $chatData = $chatResponse['result']; - $chatModel = TgGroups::updateOrCreateFromTelegram($chatData); //更新写入群资料 - $isComm=TgComm::TgComm($msg); //判断是不是指令之一 - if ($isComm['ok']){ //如果是指令进行指令的处理 - $botMsg = TgComm::{'comm_' . $isComm['id']}($msg); - if ($botMsg) { - - } - } - return true; - } else { - return true; +// return $data; + $data = '{"update_id": 250041637, "message_date": 1746710794, "message_text": "@winchen1688 +2000", "message_chat_id": -4661252768, "message_from_id": 5909523353, "message_chat_type": "group", "message_chat_title": "一路发测试群", "message_message_id": 8, "message_from_is_bot": false, "message_from_username": "winchen1688", "message_from_last_name": "阿江", "message_entities_0_type": "mention", "message_from_first_name": "流浪的", "message_entities_0_length": 12, "message_entities_0_offset": 0, "message_from_language_code": "zh-hans", "message_chat_all_members_are_administrators": true, "message_chat_accepted_gift_types_unique_gifts": false, "message_chat_accepted_gift_types_limited_gifts": false, "message_chat_accepted_gift_types_unlimited_gifts": false, "message_chat_accepted_gift_types_premium_subscription": false}'; + $msg = UserFun::Json2Arr($data); //将JSON转换成一維扁平陣列 + $msg['token'] = $token; + $tgmsg = TgMsg::storeFromWebhookJson($msg); //写入对话消息 + $UserResponse = TgApi::GetChatMember($msg); //获取用户资料 + $TgGroupUsers = TgGroupUsers::updateOrCreateFromTelegram($UserResponse['raw'], $msg['message_chat_id']); //更新用户资料 + $chatResponse = TgApi::GetChatInfo($msg); //获取群资料 + $chatModel = TgGroups::updateOrCreateFromTelegram($chatResponse['result']); //更新写入群资料 + $isComm = TgComm::TgComm($msg); //判断是不是指令之一 + if ($isComm['ok']) { //如果是指令进行指令的处理 + $botMsg = TgComm::{'comm_' . $isComm['id']}($msg); +// if ($botMsg) { +// +// } } } } diff --git a/app/Models/Ledger.php b/app/Models/Ledger.php index feccf93..0e42250 100755 --- a/app/Models/Ledger.php +++ b/app/Models/Ledger.php @@ -30,4 +30,11 @@ public static function AddIncome($data){ } } +// public static function UpdateUser($data){ +// return UserAccount::where([ +// 'chat_id' => $data['chat_id'], +// 'user_id' => $data['user_id'], +// ])->update('balance', $data['amount']); +// } + } diff --git a/app/Models/UserAccount.php b/app/Models/UserAccount.php index 796d68b..1939a98 100755 --- a/app/Models/UserAccount.php +++ b/app/Models/UserAccount.php @@ -8,7 +8,7 @@ class UserAccount extends Model { // 显式指定表名 - protected $table = ' user_account'; + protected $table = 'user_account'; protected $guarded = ['id']; // 时间字段类型自动转为 Carbon 实例 diff --git a/app/Services/TgComm.php b/app/Services/TgComm.php index 9395918..8b79c26 100755 --- a/app/Services/TgComm.php +++ b/app/Services/TgComm.php @@ -29,20 +29,27 @@ public static function comm_1($msg){ public static function comm_2($msg){ $res = self::parseLedgerMessage($msg['message_text']); // if ($res) { - $TgGroupUsers=TgGroupUsers::GetUsername($res['usernamer']); + $TgGroupUsers=TgGroupUsers::GetUsername($res['username']); //获取用户资料 if ($TgGroupUsers) { $res=array_merge($res, ['user_id' => $TgGroupUsers->user_id,'chat_id'=>$msg['message_chat_id'],'token'=>$msg['token']]); //加入用户和群组ID $Ledger=Ledger::AddIncome($res); //写入收支记录 $CheckUser=UserAccount::CheckUserId($res); //检查该user_id是否有账户资料,有的话处理余额,没有的话添加相关账户资料 if ($CheckUser){ - $balance=Ledger::UpdateUser($res); //更新账号余额 - $data=array_merge($res,['comm' => 2,'balance' => $balance,'token' => $msg['token'],'chat_id'=>$msg['chat_id']]); // + $balance=UserAccount::UpdateUser($res); //更新账号余额 + $data=array_merge($res,['comm' => 2,'balance' => $balance,'token' => $msg['token']]); //添加资料到数组 $Msg=TgMsg::MakeMsg($data); //构成发送数据 - return $Msg; +// dd($Msg); + $res=UserFun::CurlPost($Msg['url'],$Msg['text']); //发送Tg消息 + dd($res); + return $res; } else { $balance=UserAccount::CreatUser($res); //新建用户账号,并写入充值金额 $res=array_merge($res,['balance' => $balance]); //发送Tg通知 + $Msg=TgMsg::MakeMsg($res); //构成发送数据 + dd($Msg); + $res=CurlPost($Msg); + return $res; } } else { return $res['usernamer'].'-该用户不存在'; @@ -56,11 +63,11 @@ public static function comm_2($msg){ private static function parseLedgerMessage(string $text): ?array { if (preg_match('/@(\w+)\s*([+-])\s*(\d+(?:\.\d+)?)/', $text, $matches)) { - $usernamer = $matches[1]; + $username = $matches[1]; $type = $matches[2] === '+' ? '0' : '1'; $amount = (float) str_replace(',', '', $matches[3]); - return compact('usernamer', 'type', 'amount'); + return compact('username', 'type', 'amount'); } return null; // 格式不符 diff --git a/app/Services/TgMsg.php b/app/Services/TgMsg.php index 32323fa..60ec554 100755 --- a/app/Services/TgMsg.php +++ b/app/Services/TgMsg.php @@ -5,13 +5,13 @@ class TgMsg { public static function MakeMsg($data){ - $url = "https://api.telegram.org/bot" . $data['token'] . "/sendMessage"; + $MsgTxt['url'] = "https://api.telegram.org/bot" . $data['token'] . "/sendMessage"; // $keyb['inline_keyboard'][0][0]['text'] = '点击查询账单'; // $keyb['inline_keyboard'][0][0]['url'] = 'https://'.$BotUrl.'/bill/' . $data['message_from_id'] . '/' . $data['message_chat_id'] . '/T/Inquiry'; // $keyb = json_encode($keyb); - $MsgTxt['chat_id'] = $data['chat_id']; -// $MsgTxt['reply_markup'] = $keyb; - $MsgTxt['text'] = self::{'comm_' . $data['comm']}($data); + + $MsgTxt['text']= self::{'comm_' . $data['comm']}($data); + return $MsgTxt; } @@ -20,7 +20,16 @@ public static function Comm_1($data) { } public static function Comm_2($data) { - $Msg = $data['username'].'-你已经成功充值 '.$data['amount'].',当前余额为'.$data['balance']; + $Msg['chat_id'] = $data['chat_id']; + $Msg['text'] = $data['username'].'-你已经成功充值 '.$data['amount']; + + $Msg['reply_markup'] = json_encode([ + 'inline_keyboard' => [[[ + 'text' => '查看余额及详情', + 'url' => 'https://test.com' + ]]] + ]); +; return $Msg; } } diff --git a/app/Services/UserFun.php b/app/Services/UserFun.php index eeb4248..5e82c71 100755 --- a/app/Services/UserFun.php +++ b/app/Services/UserFun.php @@ -69,13 +69,15 @@ public static function CurlPost(string $url, array $data = []): array curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // POST的資料 $response = curl_exec($ch); - $err = curl_error($ch); + $error = curl_error($ch); + $errno = curl_errno($ch); curl_close($ch); - if ($err) { - return ['ok' => false, 'error' => $err]; - } - - return json_decode($response, true); + return [ + 'ok' => $errno === 0, + 'error' => $error, + 'code' => $errno, + 'body' => $response, + ]; } }