Browse Source

feat: demo of reply mode

master
Dnomd343 3 years ago
parent
commit
0267e24485
  1. 10
      functions/RedisCache.php
  2. 2
      functions/TgInterface.php
  3. 21
      functions/TgReply.php
  4. 1
      main.php
  5. 9
      models/ipInfo.php
  6. 6
      route.php

10
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);
}
}
?>

2
functions/TgInterface.php

@ -1,6 +1,6 @@
<?php
class tgApi {
class tgApi { // Telegram消息发送接口
public function sendText($msg, $chatId = 0) { // 发送纯文本
return tgApi::sendMessage(['text' => $msg], $chatId);
}

21
functions/TgReply.php

@ -0,0 +1,21 @@
<?php
class tgReply { // Telegram消息待回复记录
function match() { // 匹配用户回复
$redis = new RedisCache('reply');
$userId = $GLOBALS['tgEnv']['userId'];
$reply = $redis->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)
));
}
}
?>

1
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';

9
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)) { // 参数为域名

6
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];

Loading…
Cancel
Save