diff --git a/main.php b/main.php
index a2ca512..7ba4cc0 100644
--- a/main.php
+++ b/main.php
@@ -31,6 +31,20 @@ $userName = $messageFrom['first_name'];
$userAccount = $messageFrom['username'];
$userLanguage = $messageFrom['language_code'];
+$tgEnv = array(
+ 'apiPath' => $apiPath,
+ 'botAccount' => $botAccount,
+ 'isCallback' => $isCallback,
+ 'isGroup' => $isGroup,
+ 'messageText' => $messageText,
+ 'messageId' => $messageId,
+ 'chatId' => $chatId,
+ 'userId' => $userId,
+ 'userName' => $userName,
+ 'userAccount' => $userAccount,
+ 'userLanguage' => $userLanguage
+);
+
foreach ($cmds as $cmd) {
if (strpos($messageText, '/' . $cmd) === 0) { // 判断请求开头
$rawParam = substr($messageText, strlen($cmd) + 1); // 获取请求参数
diff --git a/models/tgDC.php b/models/tgDC.php
index 061311e..2556817 100644
--- a/models/tgDC.php
+++ b/models/tgDC.php
@@ -99,55 +99,84 @@ class tgDC {
return $info;
}
- public function getInfo($account) { // 外部调用入口
- if (substr($account, 0, 1) === '@') { // 用户名前可带有@
+ public function getInfo($account) { // 查询入口
+ if (substr($account, 0, 1) === '@') { // 用户名可带有@
$account = substr($account, 1);
}
if (!$this->checkAccount($account)) { // 用户名不合法
return array(
- 'text' => '用户名无效'
+ 'status' => 'error',
+ 'message' => '用户名无效'
);
}
$info = $this->getUserInfoCache($account);
if (!$info['name'] && !$info['dc']) { // 用户名与头像均无
return array(
- 'text' => '@' . $info['account'] . ' 无法识别'
+ 'status' => 'error',
+ 'message' => '@' . $account . ' 无法识别'
);
} else if ($info['name'] && !$info['dc']) { // 存在用户名但未设置头像
return array(
- 'text' => '@' . $info['account'] . ' 未设置头像或不可见'
+ 'status' => 'error',
+ 'message' => '@' . $account . ' 未设置头像或不可见'
);
}
- $msg = '@' . $info['account'] . ' (' . $info['name'] . ')' . PHP_EOL;
- $msg .= '' . $info['as'] . ' (' . $info['ip'] . ')
' . PHP_EOL;
- $msg .= '' . $info['dc'] . ' - ' . $info['addr'] . PHP_EOL;
return array(
- 'parse_mode' => 'HTML', // HTML格式输出
- 'text' => $msg
+ 'status' => 'ok',
+ 'data' => json_encode($info) // 返回查询结果
);
}
}
-function tgDC($rawParam) { // DC查询入口
- global $chatId, $userAccount, $isGroup;
- if ($rawParam !== '') { // 请求不为空
- sendMessage($chatId, (new tgDC)->getInfo($rawParam)); // 查询并返回数据
- return;
+class tgDCEntry {
+ private function getInfo($account) {
+ $content = (new tgDC)->getInfo($account); // 发起查询
+ if ($content['status'] === 'ok') {
+ $info = json_decode($content['data'], true);
+ $msg = '@' . $info['account'] . ' (' . $info['name'] . ')' . PHP_EOL;
+ $msg .= '' . $info['as'] . ' ';
+ $msg .= '(' . $info['ip'] . ')
' . PHP_EOL;
+ $msg .= '' . $info['dc'] . ' - ' . $info['addr'] . PHP_EOL;
+ return array(
+ 'parse_mode' => 'HTML', // HTML格式输出
+ 'text' => $msg
+ );
+ } else {
+ return array(
+ 'text' => $content['message'] // 返回错误信息
+ );
+ }
}
- if (!$isGroup) { // 群组不发送帮助信息
- $message = json_decode(sendMessage($chatId, array( // 发送帮助信息
+
+ public function query($rawParam) { // tgDC查询入口
+ $helpMsg = array( // 使用说明
'parse_mode' => 'Markdown',
'text' => '*Usage:* `/dc username`'
- )), true);
+ );
+ if ($rawParam === 'help') { // 查询使用说明
+ tgApi::sendMessage($helpMsg);
+ return;
+ }
+ if ($rawParam !== '') { // 查询指定用户数据
+ tgApi::sendMessage($this->getInfo($rawParam));
+ return;
+ }
+ global $tgEnv;
+ if (!$tgEnv['isGroup']) { // 群组不发送帮助信息
+ $message = json_decode(tgApi::sendMessage($helpMsg), true); // 发送使用说明
+ }
+ tgApi::sendMessage($this->getInfo($tgEnv['userAccount'])); // 查询对方用户名
+ if ($tgEnv['isGroup']) { return; }
+ fastcgi_finish_request(); // 断开连接
+ sleep(10); // 延迟10s
+ tgApi::deleteMessage(array( // 删除使用说明
+ 'message_id' => $message['result']['message_id']
+ ));
}
- sendMessage($chatId, (new tgDC)->getInfo($userAccount)); // 查询并返回数据
- if ($isGroup) { return; }
- sleep(15);
- sendPayload(array( // 删除帮助信息
- 'method' => 'deleteMessage',
- 'chat_id' => $chatId,
- 'message_id' => $message['result']['message_id']
- ));
+}
+
+function tgDC($rawParam) { // DC查询入口
+ (new tgDCEntry)->query($rawParam);
}
?>
diff --git a/tgInterface.php b/tgInterface.php
index 3641d40..2a32200 100644
--- a/tgInterface.php
+++ b/tgInterface.php
@@ -1,5 +1,45 @@
$msg
+ ), $chatId);
+ }
+
+ public function sendMessage($params, $chatId = 0) { // 发送文字消息
+ if ($chatId === 0) { // 未指定chatId
+ global $tgEnv;
+ $chatId = $tgEnv['chatId'];
+ }
+ $params += array (
+ 'method' => 'sendMessage',
+ 'chat_id' => $chatId
+ );
+ return tgApi::sendPayload($params);
+ }
+
+ public function deleteMessage($params, $chatId = 0) { // 删除消息
+ if ($chatId === 0) { // 未指定chatId
+ global $tgEnv;
+ $chatId = $tgEnv['chatId'];
+ }
+ $params += array (
+ 'method' => 'deleteMessage',
+ 'chat_id' => $chatId
+ );
+ return tgApi::sendPayload($params);
+ }
+ public function sendPayload($payload) { // 发送原始数据
+ global $tgEnv;
+ $url = $tgEnv['apiPath'] . '/' . $payload['method'] . '?';
+ foreach ($payload as $param => $content) {
+ $url .= '&' . $param . '=' . urlencode($content);
+ }
+ return file_get_contents($url);
+ }
+}
+
function sendPayload($payload) { // 发送API请求
global $apiPath;
$url = $apiPath . '/' . $payload['method'] . '?';