From 78811540db3dba403e152aa49fb0eadeeb9421ed Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 20 Sep 2021 19:54:38 +0800 Subject: [PATCH] refactor: basic part of bot --- env.php | 19 ------------ functions/TgInterface.php | 34 ++++++++++++--------- init.php | 63 +++++++++++++++++++++++++++++++++++++++ main.php | 37 ++++------------------- route.php | 6 ++-- 5 files changed, 91 insertions(+), 68 deletions(-) delete mode 100644 env.php create mode 100644 init.php diff --git a/env.php b/env.php deleted file mode 100644 index b6d55f3..0000000 --- a/env.php +++ /dev/null @@ -1,19 +0,0 @@ - \ No newline at end of file diff --git a/functions/TgInterface.php b/functions/TgInterface.php index 894b838..d621578 100644 --- a/functions/TgInterface.php +++ b/functions/TgInterface.php @@ -41,8 +41,7 @@ class tgApi { // Telegram消息发送接口 } public function sendPayload($payload) { // 发送原始数据 - global $apiPath; - $url = $apiPath . '/' . $payload['method'] . '?'; + $url = 'https://api.telegram.org/bot' . $GLOBALS['env']['BOT_TOKEN'] . '/' . $payload['method'] . '?'; foreach ($payload as $param => $content) { $url .= '&' . $param . '=' . urlencode($content); } @@ -50,18 +49,25 @@ class tgApi { // Telegram消息发送接口 } function debug() { // 调试接口 - global $webhook, $tgEnv; - $msg .= 'isCallback: ' . ($tgEnv['isCallback'] ? 'true' : 'false') . PHP_EOL; - $msg .= 'isGroup: ' . ($tgEnv['isGroup'] ? 'true' : 'false') . PHP_EOL; - $msg .= 'messageText: ' . $tgEnv['messageText'] . PHP_EOL; - $msg .= 'messageId: ' . $tgEnv['messageId'] . PHP_EOL; - $msg .= 'chatId: ' . $tgEnv['chatId'] . PHP_EOL; - $msg .= 'userId: ' . $tgEnv['userId'] . PHP_EOL; - $msg .= 'userName: ' . $tgEnv['userName'] . PHP_EOL; - $msg .= 'userAccount: ' . $tgEnv['userAccount'] . PHP_EOL; - $msg .= 'userLanguage: ' . $tgEnv['userLanguage'] . PHP_EOL; - tgApi::sendText($msg); - tgApi::sendText(json_encode($webhook)); + global $tgEnv; + $msg = '---- tgEnv Content ----' . PHP_EOL; + $msg .= 'myId: ' . $tgEnv['myInfo']['id'] . PHP_EOL; + $msg .= 'myName: ' . $tgEnv['myInfo']['name'] . PHP_EOL; + $msg .= 'myAccount: ' . $tgEnv['myInfo']['account'] . PHP_EOL; + $msg .= 'isCallback: ' . ($tgEnv['isCallback'] ? 'true' : 'false') . PHP_EOL; + $msg .= 'isGroup: ' . ($tgEnv['isGroup'] ? 'true' : 'false') . PHP_EOL; + $msg .= 'messageText: ' . $tgEnv['messageText'] . PHP_EOL; + $msg .= 'messageId: ' . $tgEnv['messageId'] . PHP_EOL; + $msg .= 'chatId: ' . $tgEnv['chatId'] . PHP_EOL; + $msg .= 'userId: ' . $tgEnv['userId'] . PHP_EOL; + $msg .= 'userName: ' . $tgEnv['userName'] . PHP_EOL; + $msg .= 'userAccount: ' . $tgEnv['userAccount'] . PHP_EOL; + $msg .= 'demo: ' . 'dnomd343' . PHP_EOL; + $msg .= 'userLanguage: ' . $tgEnv['userLanguage'] . PHP_EOL; + tgApi::sendMessage(array( + 'text' => $msg, + 'parse_mode' => 'HTML', // HTML格式输出 + )); } } diff --git a/init.php b/init.php new file mode 100644 index 0000000..9a4daef --- /dev/null +++ b/init.php @@ -0,0 +1,63 @@ + getMyself($GLOBALS['env']['BOT_TOKEN']), // bot信息 + 'isGroup' => ($message['chat']['type'] === 'group') ? true : false, // 是否为群组 + 'isCallback' => $isCallback, // 是否为回调请求 + 'messageText' => $messageText, // 请求/回调 文本内容 + 'messageId' => $message['message_id'], // 请求/回调 消息ID + 'chatId' => $message['chat']['id'], // 会话ID + 'userId' => $messageFrom['id'], // 请求者用户ID + 'userName' => $messageFrom['first_name'], // 请求者名字 + 'userAccount' => $messageFrom['username'], // 请求者用户名 + 'userLanguage' => $messageFrom['language_code'] // 请求者语言 + ); +} + +function getMyself($token) { // 获取bot信息 + $redis = new RedisCache('me'); + $info = $redis->getData('info'); // 查询缓存数据 + if (!$info) { // 缓存未命中 + $url = 'https://api.telegram.org/bot' . $token . '/getMe'; // API查询 + $info = json_decode(file_get_contents($url), true)['result']; + $info = array( + 'id' => $info['id'], + 'name' => $info['first_name'], + 'account' => $info['username'] + ); + $redis->setData('info', json_encode($info), 2 * 3600); // 缓存2小时 + } else { // 缓存命中 + $info = json_decode($info, true); // 使用缓存数据 + } + return $info; +} + +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[trim($record[0])] = trim($record[1]); // 合法记录 + } + } + fclose($file); + return $data; +} + +?> \ No newline at end of file diff --git a/main.php b/main.php index 5c6b19a..c65419e 100644 --- a/main.php +++ b/main.php @@ -1,6 +1,6 @@ ($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'] // 请求者语言 -); - -route($messageText); // 发往请求路由 +$tgEnv = initBot(file_get_contents("php://input")); // 初始化bot配置 +tgApi::debug(); +route($tgEnv['messageText']); // 发往请求路由 ?> diff --git a/route.php b/route.php index 9e8a58e..7b78c4b 100644 --- a/route.php +++ b/route.php @@ -36,7 +36,8 @@ function cmdRoute($cmd) { // 命令功能模块路由 } function route($message) { // 请求路由 - global $tgEnv, $botAccount; + global $tgEnv; + $botAccount = $tgEnv['myInfo']['account']; $message = trim($message); // 去除前后空字符 if (!$tgEnv['isGroup']) { // 当前为私聊模式 $reply = tgReply::match(); @@ -51,14 +52,13 @@ function route($message) { // 请求路由 $rawParam = ''; } else { // 命令带有参数 unset($temp[0]); - $rawParam = implode(' ', $temp); // 获得参数 + $rawParam = trim(implode(' ', $temp)); // 获得参数 } if ($tgEnv['isGroup']) { // 当前为群组 if (substr($cmd, -strlen($botAccount) - 1) === '@' . $botAccount) { $cmd = substr($cmd, 0, strlen($cmd) - strlen($botAccount) - 1); // 分离@机器人 } } - $rawParam = trim($rawParam); $entry = cmdRoute($cmd); // 获取功能模块入口 if (!$entry) { return; } // 命令不存在 if ($tgEnv['isCallback']) {