From f26bc907d462dc2766adb0441d26f5daa3878b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=AA=E5=AD=90?= Date: Fri, 14 Jun 2024 16:33:47 +0800 Subject: [PATCH] =?UTF-8?q?add=E6=9C=89=E6=96=B0=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E9=80=9A=E7=9F=A5QQ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions.php | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/functions.php b/functions.php index e982c41..7b96732 100644 --- a/functions.php +++ b/functions.php @@ -55,6 +55,10 @@ function themeConfig($form) { array('0'=> _t('否'), '1'=> _t('是')), '0', _t('是否显示页面加载时间'), _t('选择“是”将在页脚显示加载时间。')); $form->addInput($showtime); + $qqboturl = new Typecho_Widget_Helper_Form_Element_Text('qqboturl', NULL, 'https://bot.asbid.cn', _t('QQ机器人API,保持默认则需添加 2280858259 为好友'), _t('基于cqhttp,有评论时QQ通知')); + $form->addInput($qqboturl); + $qqnum = new Typecho_Widget_Helper_Form_Element_Text('qqnum', NULL, '80116747', _t('QQ号码'), _t('用于接收QQ通知的号码')); + $form->addInput($qqnum); } function get_post_view($archive) { $cid = $archive->cid; @@ -201,6 +205,63 @@ function getPermalinkFromCoid($coid) { if (empty($row)) return ''; return '@'.$row['author'].''; } +// 评论提交通知函数 +function notifyQQBot($comment) { + $options = Helper::options(); + // 检查评论是否已经审核通过 + if ($comment->status != "approved") { + error_log('Comment is not approved.'); + return; + } + // 获取配置中的QQ机器人API地址 + $cq_url = $options->qqboturl; + // 检查API地址是否为空 + if (empty($cq_url)) { + error_log('QQ Bot URL is empty. Using default URL.'); + $cq_url = 'https://bot.asbid.cn'; + } + // 获取QQ号码 + $qqnum = $options->qqnum; + // 检查QQ号码是否为空 + if (empty($qqnum)) { + error_log('QQ number is empty.'); + return; + } + // 如果是管理员自己发的评论则不发送通知 + if ($comment->authorId === $comment->ownerId) { + error_log('This comment is by the post owner.'); + return; + } + // 构建消息内容 + $msg = '「' . $comment->author . '」在文章《' . $comment->title . '》中发表了评论!'; + $msg .= "\n评论内容:\n{$comment->text}\n永久链接地址:{$comment->permalink}"; + // 准备发送消息的数据 + $_message_data_ = [ + 'user_id' => (int) trim($qqnum), + 'message' => str_replace(["\r\n", "\r", "\n"], "\r\n", htmlspecialchars_decode(strip_tags($msg))) + ]; + // 输出调试信息 + error_log('Sending message to QQ Bot: ' . print_r($_message_data_, true)); + // 初始化Curl请求 + $ch = curl_init(); + curl_setopt_array($ch, [ + CURLOPT_URL => "{$cq_url}/send_msg?" . http_build_query($_message_data_, '', '&'), + CURLOPT_CONNECTTIMEOUT => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HEADER => false, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => 0 + ]); + $response = curl_exec($ch); + if (curl_errno($ch)) { + error_log('Curl error: ' . curl_error($ch)); + } else { + error_log('Response: ' . $response); + } + curl_close($ch); +} +Typecho_Plugin::factory('Widget_Feedback')->finishComment = 'notifyQQBot'; //开始增加某些奇怪的东西 // 获取月份 function getMonth() {