Browse Source

refactor: command route of bot request

master
Dnomd343 3 years ago
parent
commit
bc0bc85088
  1. 60
      cmdRoute.php
  2. 19
      env.php
  3. 59
      main.php
  4. 61
      route.php

60
cmdRoute.php

@ -1,60 +0,0 @@
<?php
require_once 'models/tgDC.php';
require_once 'models/ipInfo.php';
require_once 'models/cfopPic.php';
require_once 'models/kmsCheck.php';
require_once 'models/ntpCheck.php';
require_once 'models/icpQuery.php';
require_once 'models/punycode.php';
require_once 'models/whoisQuery.php';
$cmds = array( // 命令列表
'ip',
'dc',
'kms',
'ntp',
'icp',
'cfop',
'whois',
'punycode'
);
function route($cmd, $rawParam) { // 命令请求路由
switch ($cmd) {
case 'ip':
$entry = new ipInfoEntry;
break;
case 'dc':
$entry = new tgDCEntry;
break;
case 'kms':
$entry = new kmsCheckEntry;
break;
case 'ntp':
$entry = new ntpCheckEntry;
break;
case 'icp':
$entry = new icpQueryEntry;
break;
case 'cfop':
$entry = new cfopPicEntry;
break;
case 'whois':
$entry = new whoisQueryEntry;
break;
case 'punycode':
$entry = new punycodeEntry;
break;
}
if ($entry) {
global $tgEnv;
if (!$tgEnv['isCallback']) {
$entry->query($rawParam);
} else {
$entry->callback($rawParam);
}
}
}
?>

19
env.php

@ -0,0 +1,19 @@
<?php
function loadEnv($filename) { // 读取环境变量文件
$file = fopen($filename, 'r');
$data = array();
while (!feof($file)) { // 逐行读入文件
$raw = trim(fgets($file));
if ($raw == '') { continue; } // 跳过空行
if (substr($raw, 0, 1) === '#') { continue; } // 跳过注释
$record = explode('=', $raw);
if (count($record) === 2) {
$data[$record[0]] = $record[1]; // 合法记录
}
}
fclose($file);
return $data;
}
?>

59
main.php

@ -1,67 +1,42 @@
<?php
require_once 'cmdRoute.php';
require_once 'env.php';
require_once 'route.php';
require_once 'functions/Punycode.php';
require_once 'functions/SqliteDB.php';
require_once 'functions/RedisCache.php';
require_once 'functions/TgInterface.php';
require_once 'functions/ExtractDomain.php';
$env = loadEnv();
$env = loadEnv('.env'); // 载入环境变量
$apiToken = $env['BOT_TOKEN'];
$botAccount = $env['BOT_NAME'];
$botAccount = $env['BOT_NAME']; // 机器人用户名
$apiPath = 'https://api.telegram.org/bot' . $apiToken; // Telegram API接口
$webhook = json_decode(file_get_contents("php://input"), TRUE); // Webhook接受信息
$isCallback = isset($webhook['callback_query']) ? true : false;
if ($isCallback) {
$isCallback = isset($webhook['callback_query']) ? true : false; // 是否为回调请求
if ($isCallback) { // 回调请求模式
$message = $webhook['callback_query']['message'];
$messageText = $webhook['callback_query']['data'];
$messageFrom = $webhook['callback_query']['from'];
} else {
} else { // 直接请求模式
$message = $webhook['message'];
$messageText = $webhook['message']['text'];
$messageFrom = $webhook['message']['from'];
}
$chat = $message['chat'];
$tgEnv = array(
'isCallback' => $isCallback,
'isGroup' => ($chat['type'] === 'group') ? true : false,
'messageText' => $messageText,
'messageId' => $message['message_id'],
'chatId' => $chat['id'],
'userId' => $messageFrom['id'],
'userName' => $messageFrom['first_name'],
'userAccount' => $messageFrom['username'],
'userLanguage' => $messageFrom['language_code']
'isGroup' => ($chat['type'] === 'group') ? true : false, // 是否为群组
'isCallback' => $isCallback, // 是否为回调请求
'messageText' => $messageText, // 请求/回调 文本内容
'messageId' => $message['message_id'], // 请求/回调 消息ID
'chatId' => $chat['id'], // 会话ID
'userId' => $messageFrom['id'], // 请求者用户ID
'userName' => $messageFrom['first_name'], // 请求者名字
'userAccount' => $messageFrom['username'], // 请求者用户名
'userLanguage' => $messageFrom['language_code'] // 请求者语言
);
foreach ($cmds as $cmd) {
if (strpos($messageText, '/' . $cmd) === 0) { // 判断请求开头
$rawParam = substr($messageText, strlen($cmd) + 1); // 获取请求参数
if ($tgEnv['isGroup'] && strpos($rawParam, '@' . $botAccount) === 0) {
$rawParam = substr($rawParam, strlen($botAccount) + 1); // 去除群组中的@
}
if (strlen($rawParam) != 0 && substr($rawParam, 0, 1) !== ' ') { break; } // 命令后必须带空格
$rawParam = trim($rawParam); // 消除前后空格
route($cmd, $rawParam); // 路由命令
}
}
function loadEnv() { // 载入环境变量
$file = fopen('.env', 'r');
$data = array();
while (!feof($file)) { // 逐行读入文件
$raw = trim(fgets($file));
if ($raw == '') { continue; } // 跳过空行
if (substr($raw, 0, 1) === '#') { continue; } // 跳过注释
$record = explode('=', $raw);
if (count($record) === 2) { // 合法记录
$data[$record[0]] = $record[1];
}
}
fclose($file);
return $data;
}
route($messageText); // 发往请求路由
?>

61
route.php

@ -0,0 +1,61 @@
<?php
require_once 'models/tgDC.php';
require_once 'models/ipInfo.php';
require_once 'models/cfopPic.php';
require_once 'models/kmsCheck.php';
require_once 'models/ntpCheck.php';
require_once 'models/icpQuery.php';
require_once 'models/punycode.php';
require_once 'models/whoisQuery.php';
function cmdRoute($cmd) { // 命令功能模块路由
switch ($cmd) {
case '/ip':
return (new ipInfoEntry);
case '/dc':
return (new tgDCEntry);
case '/kms':
return (new kmsCheckEntry);
case '/ntp':
return (new ntpCheckEntry);
case '/icp':
return (new icpQueryEntry);
case '/cfop':
return (new cfopPicEntry);
case '/whois':
return (new whoisQueryEntry);
case '/punyc':
case '/punycode':
return (new punycodeEntry);
}
return null;
}
function route($message) { // 请求路由
global $tgEnv, $botAccount;
$message = trim($message); // 去除前后空字符
if (strpos($message, '/') !== 0) { return; } // 命令必须以 / 开头
$temp = explode(' ', $message);
$cmd = $temp[0];
if (count($temp) === 1) { // 命令无参数
$rawParam = '';
} else { // 命令带有参数
unset($temp[0]);
$rawParam = implode(' ', $temp); // 获得参数
}
if ($tgEnv['isGroup']) { // 当前为群组
if (substr($cmd, -strlen($botAccount) - 1) === '@' . $botAccount) {
$cmd = substr($cmd, 0, strlen($cmd) - strlen($botAccount) - 1); // 分离@机器人
}
}
$entry = cmdRoute($cmd); // 获取功能模块入口
if (!$entry) { return; } // 命令不存在
if ($tgEnv['isCallback']) {
$entry->callback($rawParam); // 回调请求
} else {
$entry->query($rawParam); // 普通请求
}
}
?>
Loading…
Cancel
Save