From e3bcbaefd0c41e06aefc86658d30e0430e5f49a4 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 11 Aug 2021 17:51:21 +0800 Subject: [PATCH] refactor: interface of cfop pic and whois query --- cmdRoute.php | 4 +- models/cfopPic.php | 87 +++++++++++++++++++++---------------------- models/whoisQuery.php | 21 +++++------ tgInterface.php | 12 ++++++ 4 files changed, 66 insertions(+), 58 deletions(-) diff --git a/cmdRoute.php b/cmdRoute.php index 0525500..0615c7a 100644 --- a/cmdRoute.php +++ b/cmdRoute.php @@ -31,10 +31,10 @@ function route($cmd, $rawParam) { // 命令请求路由 $entry = new ntpCheckEntry; break; case 'cfop': - cfopPic($rawParam); + $entry = new cfopPicEntry; break; case 'whois': - whoisQuery($rawParam); + $entry = new whoisQueryEntry; break; } if ($entry) { diff --git a/models/cfopPic.php b/models/cfopPic.php index fed6aa0..112367c 100644 --- a/models/cfopPic.php +++ b/models/cfopPic.php @@ -1,23 +1,19 @@ 'CFOP魔方公式合集', 'reply_markup' => json_encode(array( 'inline_keyboard' => array( - array( - array( - 'text' => '网页下载', - 'url' => 'https://res.dnomd343.top/Share/cfop/' - ) - ), - array( - array( - 'text' => '获取全部', - 'callback_data' => '/cfop all' - ) - ), + array([ + 'text' => '网页下载', + 'url' => 'https://res.dnomd343.top/Share/cfop/' + ]), + array([ + 'text' => '获取全部', + 'callback_data' => '/cfop all' + ]), array( array( 'text' => 'GAN', @@ -37,58 +33,59 @@ class cfopPic { ); } - private function sendPic($type) { // 返回图片文件ID + private function getPicId($type) { // 返回图片文件ID switch ($type) { case 'gan': - $fileId = 'BQACAgUAAxkBAAIBtGEOLnr4Q6D4Z_80bgfXq5xsZMeWAAKtAwACWy55VOU-SGKqc7aMIAQ'; - break; + return 'BQACAgUAAxkBAAIBtGEOLnr4Q6D4Z_80bgfXq5xsZMeWAAKtAwACWy55VOU-SGKqc7aMIAQ'; case 'mfg': - $fileId = 'BQACAgUAAxkBAAIB3WEOVHKeYrrGhFo-GffB0W-tQRKlAALQAwACWy55VGny8ArGMkfoIAQ'; - break; + return 'BQACAgUAAxkBAAIB3WEOVHKeYrrGhFo-GffB0W-tQRKlAALQAwACWy55VGny8ArGMkfoIAQ'; case 'yx': - $fileId = 'BQACAgUAAxkBAAIB32EOVISFQbgmir2abj6QkgqaSX1WAALRAwACWy55VMEuU9lCYTYWIAQ'; - break; + return 'BQACAgUAAxkBAAIB32EOVISFQbgmir2abj6QkgqaSX1WAALRAwACWy55VMEuU9lCYTYWIAQ'; } - return array( - 'document' => $fileId - ); } - public function getPic($type) { + private function getPic($type) { // 获取图片 switch ($type) { case 'gan': case 'mfg': case 'yx': - return $this->sendPic($type); + return array( + 'document' => $this->getPicId($type) + ); case '': - return $this->picList(); + return $this->getCfopMsg(); default: return array( 'text' => '未知公式' ); } } -} -function cfopPic($rawParam) { // 发送CFOP图片入口 - global $chatId; - sendAuto($chatId, (new cfopPic)->getPic($rawParam)); -} + private function sendCfopPic($params) { // 发送图片或信息 + if ($params['document']) { + tgApi::sendDocument($params); + } else { + tgApi::sendMessage($params); + } + } + + public function query($rawParam) { // CFOP图片查询入口 + $this->sendCfopPic($this->getPic($rawParam)); + } -function cfopPicCallback($rawParam) { // 发送CFOP图片回调入口 - global $chatId, $messageId; - if ($rawParam === 'all') { - sendAuto($chatId, (new cfopPic)->getPic('gan')); - sendAuto($chatId, (new cfopPic)->getPic('mfg')); - sendAuto($chatId, (new cfopPic)->getPic('yx')); - sendPayload(array( // 删除源消息 - 'method' => 'deleteMessage', - 'chat_id' => $chatId, - 'message_id' => $messageId - )); - return; + public function callback($rawParam) { // CFOP图片回调入口 + if ($rawParam === 'all') { + global $tgEnv; + tgApi::deleteMessage(array( // 删除源消息 + 'message_id' => $tgEnv['messageId'] + )); + $this->sendCfopPic($this->getPic('gan')); + $this->sendCfopPic($this->getPic('mfg')); + $this->sendCfopPic($this->getPic('yx')); + return; + } + $this->sendCfopPic($this->getPic($rawParam)); } - sendAuto($chatId, (new cfopPic)->getPic($rawParam)); } ?> diff --git a/models/whoisQuery.php b/models/whoisQuery.php index 24ceeef..7186303 100644 --- a/models/whoisQuery.php +++ b/models/whoisQuery.php @@ -1,7 +1,7 @@ query($rawParam); - sendMessage($chatId, array( - 'text' => $content, - 'disable_web_page_preview' => 'true' // 不显示页面预览 - )); + public function query($rawParam) { + $content = $this->getWhois($rawParam); + tgApi::sendMessage(array( + 'text' => $content, + 'disable_web_page_preview' => 'true' // 不显示页面预览 + )); + } } -?> \ No newline at end of file +?> diff --git a/tgInterface.php b/tgInterface.php index c83d94f..3b8ea6f 100644 --- a/tgInterface.php +++ b/tgInterface.php @@ -7,6 +7,18 @@ class tgApi { ), $chatId); } + function sendDocument($params, $chatId = 0) { // 发送文件 + if ($chatId === 0) { // 未指定chatId + global $tgEnv; + $chatId = $tgEnv['chatId']; + } + $params += array ( + 'method' => 'sendDocument', + 'chat_id' => $chatId + ); + return tgApi::sendPayload($params); + } + public function sendMessage($params, $chatId = 0) { // 发送消息 if ($chatId === 0) { // 未指定chatId global $tgEnv;