From 0267e244853516219f3670a3c010c5f965da8b9e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 27 Aug 2021 21:18:46 +0800 Subject: [PATCH] feat: demo of reply mode --- functions/RedisCache.php | 10 ++++++++++ functions/TgInterface.php | 2 +- functions/TgReply.php | 21 +++++++++++++++++++++ main.php | 1 + models/ipInfo.php | 9 ++++++++- route.php | 6 ++++++ 6 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 functions/TgReply.php diff --git a/functions/RedisCache.php b/functions/RedisCache.php index 5c7a34a..6d0df7b 100644 --- a/functions/RedisCache.php +++ b/functions/RedisCache.php @@ -37,6 +37,16 @@ class RedisCache { } return $status; } + + public function delData($key) { // 删除Redis缓存 + $redis = new Redis(); + $redis->connect($this->redisSetting['host'], $this->redisSetting['port']); + if ($this->redisSetting['passwd'] !== '') { + $redis->auth($this->redisSetting['passwd']); // 密码认证 + } + $redisKey = $this->redisSetting['prefix'] . $key; + return $redis->del($redisKey); + } } ?> diff --git a/functions/TgInterface.php b/functions/TgInterface.php index f91ad72..894b838 100644 --- a/functions/TgInterface.php +++ b/functions/TgInterface.php @@ -1,6 +1,6 @@ $msg], $chatId); } diff --git a/functions/TgReply.php b/functions/TgReply.php new file mode 100644 index 0000000..5b2f55f --- /dev/null +++ b/functions/TgReply.php @@ -0,0 +1,21 @@ +getData($userId); // 查询用户是否有待回复记录 + if (!$reply) { return null; } // 无记录返回null + $redis->delData($userId); + return json_decode($reply, true); // 返回待回复命令 + } + + function add($cmd) { // 添加待回复记录 + $redis = new RedisCache('reply'); + $redis->setData($GLOBALS['tgEnv']['userId'], json_encode( // 缓存记录 + array('cmd' => $cmd) + )); + } +} + +?> \ No newline at end of file diff --git a/main.php b/main.php index 5cf0cab..5c6b19a 100644 --- a/main.php +++ b/main.php @@ -6,6 +6,7 @@ require_once 'functions/DNS.php'; require_once 'functions/Curl.php'; require_once 'functions/Domain.php'; require_once 'functions/DNSSEC.php'; +require_once 'functions/TgReply.php'; require_once 'functions/Punycode.php'; require_once 'functions/SqliteDB.php'; require_once 'functions/RedisCache.php'; diff --git a/models/ipInfo.php b/models/ipInfo.php index e41d724..97980e9 100644 --- a/models/ipInfo.php +++ b/models/ipInfo.php @@ -134,8 +134,15 @@ class ipInfoEntry { // IP信息查询入口 } public function query($rawParam) { // ipInfo查询入口 - if ($rawParam == '' || $rawParam === 'help') { + if ($rawParam === 'help') { $this->sendHelp(); // 显示使用说明 + } else if ($rawParam == '') { + if ($GLOBALS['tgEnv']['isGroup']) { // 此时为群组 + $this->sendHelp(); // 显示使用说明 + } else { + tgReply::add('/ip'); + tgApi::sendText('Please send the IP / Domain'); + } } else if (filter_var($rawParam, FILTER_VALIDATE_IP)) { // 参数为IP地址 $this->sendInfo($rawParam); // 查询并发送IP信息 } else if ((new Domain)->isDomain($rawParam)) { // 参数为域名 diff --git a/route.php b/route.php index 5a20ff0..9e8a58e 100644 --- a/route.php +++ b/route.php @@ -38,6 +38,12 @@ function cmdRoute($cmd) { // 命令功能模块路由 function route($message) { // 请求路由 global $tgEnv, $botAccount; $message = trim($message); // 去除前后空字符 + if (!$tgEnv['isGroup']) { // 当前为私聊模式 + $reply = tgReply::match(); + if ($reply !== null) { // 有待回复记录 + $message = $reply['cmd'] . ' ' . $message; + } + } if (strpos($message, '/') !== 0) { return; } // 命令必须以 / 开头 $temp = explode(' ', $message); $cmd = $temp[0];