首次提交 Laravel 專案
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\tg_chats;
|
||||
use App\Models\tg_Msg;
|
||||
use App\Services\TgApi;
|
||||
use App\Services\UserFun;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TgController extends Controller
|
||||
{
|
||||
public function Msg($token,Request $request)
|
||||
{
|
||||
$data = $request->all();
|
||||
$data='{"token": "7710672705:AAEX1RWhBoLA2L5EZuTLajQ---tRjahNr9w", "update_id": 250041634, "message_date": 1746524847, "message_text": "@winchen1688 +2000", "message_chat_id": -4661252768, "message_from_id": 7559940376, "message_chat_type": "group", "message_chat_title": "一路发测试群", "message_message_id": 5, "message_from_is_bot": false, "message_from_username": "xiongia", "message_from_last_name": "xiong", "message_entities_0_type": "mention", "message_from_first_name": "ab ni", "message_entities_0_length": 12, "message_entities_0_offset": 0, "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=tg_Msg::storeFromWebhookJson($msg);
|
||||
$chatResponse=TgApi::GetChatInfo($msg);
|
||||
if($chatResponse['ok'] && isset($chatResponse['result']['id'])){
|
||||
$chatData = $chatResponse['result'];
|
||||
$chatModel = tg_chats::updateOrCreateFromTelegram($chatData);
|
||||
return ['ok' => true, 'chat_id' => $chatModel->chat_id];
|
||||
} else {
|
||||
return ['ok' => false, 'error' => '无法取得群资料', 'raw' => $chatResponse];
|
||||
}
|
||||
// $tg_chats=tg_chats::updateOrCreateFromTelegram($chat_id);
|
||||
// $chat=UserFun::Json2Arr($chat_id);
|
||||
// dd($chat_id);
|
||||
// return $tg_chats;
|
||||
|
||||
// return $tgmsg;
|
||||
// return response()->json(['status' => 'ok']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('cache', function (Blueprint $table) {
|
||||
$table->string('key')->primary();
|
||||
$table->mediumText('value');
|
||||
$table->integer('expiration');
|
||||
});
|
||||
|
||||
Schema::create('cache_locks', function (Blueprint $table) {
|
||||
$table->string('key')->primary();
|
||||
$table->string('owner');
|
||||
$table->integer('expiration');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('cache');
|
||||
Schema::dropIfExists('cache_locks');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('tg_group_users', function (Blueprint $table) {
|
||||
$table->increments('id'); // 主键 + AUTO_INCREMENT
|
||||
$table->string('chat_id', 50)->comment('隶属群ID');
|
||||
$table->string('user_id', 15)->comment('tg的ID');
|
||||
$table->string('first_name')->nullable();
|
||||
$table->string('last_name')->nullable();;
|
||||
$table->string('username')->nullable()->comment('tg的username');
|
||||
$table->string('status')->comment('用户在群的角色'); // creator, administrator, member, left, kicked
|
||||
$table->boolean('is_bot')->default(false)->comment('0:真人 1:机器人');
|
||||
$table->string('language_code')->nullable();
|
||||
$table->integer('operator')->default(0)->comment('操作员 0不是1是');
|
||||
$table->integer('competence')->default(0)->comment('0普通权限,1管理员');
|
||||
$table->string('inviter', 50)->nullable()->comment('邀请人');
|
||||
$table->json('raw_data')->nullable();
|
||||
$table->timestamps();
|
||||
$table->unique(['chat_id', 'user_id']); // 防止重复记录
|
||||
// $table->decimal('rate', 10, 2)->default(0)->comment('费率');
|
||||
// $table->decimal('exchange_rate', 10, 2)->default(0)->comment('汇率');
|
||||
// $table->integer('end_time')->nullable()->comment('日结束时间');
|
||||
// 设置字符集与排序规则
|
||||
$table->charset = 'utf8mb4';
|
||||
$table->collation = 'utf8mb4_0900_ai_ci';
|
||||
});
|
||||
|
||||
// 若需要设置表注释(Laravel 原生不支持)
|
||||
// 可借助 doctrine/dbal 手动添加,或使用迁移后执行 SQL 语句
|
||||
DB::statement("ALTER TABLE `tg_group_users` COMMENT = '群用户人员'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('group_users');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,48 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user