首次提交 Laravel 專案
This commit is contained in:
Executable
+73
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
class TgApi{
|
||||
|
||||
//获取群成员资料
|
||||
public static function GetChatMember($msg){
|
||||
$ApiUrl= "https://api.telegram.org/bot{$msg['token']}/getChatMember?chat_id={$msg['message_chat_id']}&user_id={$msg['message_from_id']}";
|
||||
$ChatInfo=UserFun::CurlGet($ApiUrl);
|
||||
if ($ChatInfo['ok'] && isset($ChatInfo['result'])) {
|
||||
// 成功取得群成员资料
|
||||
return [
|
||||
'ok' => true,
|
||||
'user_id' => $ChatInfo['result']['user']['id'],
|
||||
'first_name' => $ChatInfo['result']['user']['first_name'] ?? null,
|
||||
'username' => $ChatInfo['result']['user']['username'] ?? null,
|
||||
'status' => $ChatInfo['result']['status'], // 例如 creator / administrator / member / left / kicked
|
||||
'raw' => $ChatInfo['result'] // 可选:保留原始数据
|
||||
];
|
||||
} else {
|
||||
// 获取失败,回传错误信息
|
||||
return [
|
||||
'ok' => false,
|
||||
'error' => $ChatInfo['error'] ?? '未知错误',
|
||||
'raw' => $ChatInfo
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
//获取群资料
|
||||
public static function GetChatInfo($msg){
|
||||
$ApiUrl= "https://api.telegram.org/bot{$msg['token']}/getChat?chat_id={$msg['message_chat_id']}";
|
||||
|
||||
$ChatInfo=UserFun::CurlGet($ApiUrl);
|
||||
if($ChatInfo['ok'] && isset($ChatInfo['result']['id'])){
|
||||
$ApiUrl= "https://api.telegram.org/bot{$msg['token']}/getChatAdministrators?chat_id={$ChatInfo['result']['id']}";
|
||||
$AdminInfo=UserFun::CurlGet($ApiUrl);
|
||||
if ($AdminInfo['ok'] && isset($AdminInfo['result'])) {
|
||||
foreach ($AdminInfo['result'] as $admin) {
|
||||
if (isset($admin['status']) && $admin['status'] === 'creator') {
|
||||
// 找到群主,提取 user_id
|
||||
$ChatInfo['result']['creator'] = $admin['user']['id'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $ChatInfo;
|
||||
}
|
||||
|
||||
//获取机器人资料
|
||||
public static function getMe($msg){
|
||||
$ApiUrl= "https://api.telegram.org/bot{$msg['token']}/getMe";
|
||||
|
||||
$BotInfo=UserFun::CurlGet($ApiUrl);
|
||||
if($BotInfo['ok'] && isset($BotInfo['result']['id'])){
|
||||
return [
|
||||
'success' => true,
|
||||
'bot_id' => $BotInfo['result']['id'],
|
||||
'username' => $BotInfo['result']['username'] ?? null,
|
||||
'first_name' => $BotInfo['result']['first_name'] ?? null,
|
||||
'can_join_groups' => $BotInfo['result']['can_join_groups'] ?? null,
|
||||
'can_read_all_group_messages' => $BotInfo['result']['can_read_all_group_messages'] ?? null,
|
||||
'supports_inline_queries' => $BotInfo['result']['supports_inline_queries'] ?? null,
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $BotInfo['description'] ?? 'Unknown error',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Ledger;
|
||||
use App\Models\tg_comm;
|
||||
use App\Models\TgGroupUsers;
|
||||
use App\Models\UserAccount;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class TgComm
|
||||
{
|
||||
public static function TgComm($msg){
|
||||
$tg_comm = include Storage::disk('dataconfig')->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'],'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']]); //
|
||||
$Msg=TgMsg::MakeMsg($data); //构成发送数据
|
||||
return $Msg;
|
||||
} else {
|
||||
$balance=UserAccount::CreatUser($res); //新建用户账号,并写入充值金额
|
||||
$res=array_merge($res,['balance' => $balance]);
|
||||
//发送Tg通知
|
||||
}
|
||||
} 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 = (float) str_replace(',', '', $matches[3]);
|
||||
|
||||
return compact('usernamer', 'type', 'amount');
|
||||
}
|
||||
|
||||
return null; // 格式不符
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+26
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
class TgMsg
|
||||
{
|
||||
|
||||
public static function MakeMsg($data){
|
||||
$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);
|
||||
|
||||
}
|
||||
|
||||
public static function Comm_1($data) {
|
||||
|
||||
}
|
||||
|
||||
public static function Comm_2($data) {
|
||||
$Msg = $data['username'].'-你已经成功充值 '.$data['amount'].',当前余额为'.$data['balance'];
|
||||
return $Msg;
|
||||
}
|
||||
}
|
||||
Executable
+81
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
class UserFun
|
||||
{
|
||||
public static function success($code = 200, $message = 'ok') {
|
||||
return response()->json([
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function error($code = 500,$message = 'error') {
|
||||
return response()->json([
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public static function Json2Arr($data, $prefix = '') {
|
||||
// 如果傳進來的是字串,就 decode 一次
|
||||
if (is_string($data)) {
|
||||
$arr = json_decode($data, true);
|
||||
if (!is_array($arr)) {
|
||||
return []; // 無效 JSON,回傳空陣列或你可自訂錯誤處理
|
||||
}
|
||||
} elseif (is_array($data)) {
|
||||
$arr = $data;
|
||||
} else {
|
||||
return []; // 不是 JSON 字串也不是陣列,回傳空
|
||||
}
|
||||
$result = [];
|
||||
foreach ($arr as $key => $value) {
|
||||
$fullKey = $prefix === '' ? $key : "{$prefix}_{$key}";
|
||||
if (is_array($value)) {
|
||||
$result += self::Json2Arr($value, $fullKey);
|
||||
} else {
|
||||
$result[$fullKey] = $value;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function CurlGet(string $url): array
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url); // 设定请求网址
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 回传结果而不是直接输出
|
||||
|
||||
$response = curl_exec($ch); // 执行请求
|
||||
$err = curl_error($ch); // 错误信息
|
||||
curl_close($ch); // 关闭连接
|
||||
|
||||
if ($err) {
|
||||
return ['ok' => false, 'error' => $err];
|
||||
}
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
public static function CurlPost(string $url, array $data = []): array
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url); // 設定請求網址
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 回傳內容不直接輸出
|
||||
curl_setopt($ch, CURLOPT_POST, true); // 啟用 POST
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // POST的資料
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$err = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($err) {
|
||||
return ['ok' => false, 'error' => $err];
|
||||
}
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user