path('TgComm.php'); foreach ($tg_comm as $v) { if (str_starts_with($msg['message_text'], $v['command'])){ $res['ok']= true; $res['id']= $v['id']; $res['command']= $v['command']; return $res; //有的话 返回指令ID } } } 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']); if ($TgGroupUsers) { $res=array_merge($res, ['user_id' => $TgGroupUsers->user_id,'chat_id'=>$msg['message_chat_id']]); //加入用户和群组ID $Ledger=Ledger::AddIncome($res); //写入收支记录 //检查该user_id是否有账户资料,有的话处理余额,没有的话添加相关账户资料 $CheckUserId=UserAccount::CheckUserId($TgGroupUsers->user_id); } else { return $res['usernamer'].'-该用户不存在'; } } else { return false; } } //拆解【@XXX +1000】字串 private static function parseLedgerMessage(string $text): ?array { if (preg_match('/@(\w+)\s*([+-])\s*(\d+(?:\.\d+)?)/', $text, $matches)) { $usernamer = $matches[1]; $type = $matches[2] === '+' ? '0' : '1'; $amount = $matches[3]; return compact('usernamer', 'type', 'amount'); } return false; // 格式不符 } }