修正 Telegram 控制器與帳戶服務相關邏輯

This commit is contained in:
Winchen 2025-05-20 16:07:35 +07:00
parent 5e392ae581
commit 92ad9debe8
6 changed files with 60 additions and 40 deletions

View File

@ -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) {
//
// }
}
}
}

View File

@ -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']);
// }
}

View File

@ -8,7 +8,7 @@
class UserAccount extends Model
{
// 显式指定表名
protected $table = ' user_account';
protected $table = 'user_account';
protected $guarded = ['id'];
// 时间字段类型自动转为 Carbon 实例

View File

@ -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; // 格式不符

View File

@ -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;
}
}

View File

@ -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,
];
}
}