Browse Source

refactor: demo on tgDC for new framework

master
Dnomd343 3 years ago
parent
commit
1220ad66e4
  1. 14
      main.php
  2. 81
      models/tgDC.php
  3. 40
      tgInterface.php

14
main.php

@ -31,6 +31,20 @@ $userName = $messageFrom['first_name'];
$userAccount = $messageFrom['username']; $userAccount = $messageFrom['username'];
$userLanguage = $messageFrom['language_code']; $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) { foreach ($cmds as $cmd) {
if (strpos($messageText, '/' . $cmd) === 0) { // 判断请求开头 if (strpos($messageText, '/' . $cmd) === 0) { // 判断请求开头
$rawParam = substr($messageText, strlen($cmd) + 1); // 获取请求参数 $rawParam = substr($messageText, strlen($cmd) + 1); // 获取请求参数

81
models/tgDC.php

@ -99,55 +99,84 @@ class tgDC {
return $info; return $info;
} }
public function getInfo($account) { // 外部调用入口 public function getInfo($account) { // 查询入口
if (substr($account, 0, 1) === '@') { // 用户名可带有@ if (substr($account, 0, 1) === '@') { // 用户名可带有@
$account = substr($account, 1); $account = substr($account, 1);
} }
if (!$this->checkAccount($account)) { // 用户名不合法 if (!$this->checkAccount($account)) { // 用户名不合法
return array( return array(
'text' => '用户名无效' 'status' => 'error',
'message' => '用户名无效'
); );
} }
$info = $this->getUserInfoCache($account); $info = $this->getUserInfoCache($account);
if (!$info['name'] && !$info['dc']) { // 用户名与头像均无 if (!$info['name'] && !$info['dc']) { // 用户名与头像均无
return array( return array(
'text' => '@' . $info['account'] . ' 无法识别' 'status' => 'error',
'message' => '@' . $account . ' 无法识别'
); );
} else if ($info['name'] && !$info['dc']) { // 存在用户名但未设置头像 } else if ($info['name'] && !$info['dc']) { // 存在用户名但未设置头像
return array( return array(
'text' => '@' . $info['account'] . ' 未设置头像或不可见' 'status' => 'error',
'message' => '@' . $account . ' 未设置头像或不可见'
); );
} }
$msg = '@' . $info['account'] . ' (' . $info['name'] . ')' . PHP_EOL;
$msg .= '<i>' . $info['as'] . '</i> <code>(' . $info['ip'] . ')</code>' . PHP_EOL;
$msg .= '<b>' . $info['dc'] . '</b> - ' . $info['addr'] . PHP_EOL;
return array( return array(
'parse_mode' => 'HTML', // HTML格式输出 'status' => 'ok',
'text' => $msg 'data' => json_encode($info) // 返回查询结果
); );
} }
} }
function tgDC($rawParam) { // DC查询入口 class tgDCEntry {
global $chatId, $userAccount, $isGroup; private function getInfo($account) {
if ($rawParam !== '') { // 请求不为空 $content = (new tgDC)->getInfo($account); // 发起查询
sendMessage($chatId, (new tgDC)->getInfo($rawParam)); // 查询并返回数据 if ($content['status'] === 'ok') {
return; $info = json_decode($content['data'], true);
$msg = '@' . $info['account'] . ' (' . $info['name'] . ')' . PHP_EOL;
$msg .= '<i>' . $info['as'] . '</i> ';
$msg .= '<code>(' . $info['ip'] . ')</code>' . PHP_EOL;
$msg .= '<b>' . $info['dc'] . '</b> - ' . $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', 'parse_mode' => 'Markdown',
'text' => '*Usage:* `/dc username`' '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); function tgDC($rawParam) { // DC查询入口
sendPayload(array( // 删除帮助信息 (new tgDCEntry)->query($rawParam);
'method' => 'deleteMessage',
'chat_id' => $chatId,
'message_id' => $message['result']['message_id']
));
} }
?> ?>

40
tgInterface.php

@ -1,5 +1,45 @@
<?php <?php
class tgApi {
public function sendText($msg, $chatId = 0) { // 发送纯文本
return tgApi::sendMessage(array(
'text' => $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请求 function sendPayload($payload) { // 发送API请求
global $apiPath; global $apiPath;
$url = $apiPath . '/' . $payload['method'] . '?'; $url = $apiPath . '/' . $payload['method'] . '?';

Loading…
Cancel
Save