ExpinBot/app/Services/TgApi.php

74 lines
3.0 KiB
PHP

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