Typecho 572 4

    给Typecho添加 有评论通知到QQ 的功能

    AI摘要:该文章介绍了如何给Typecho添加评论通知到QQ的功能。作者使用了基于GO-CQhttp项目的方法来实现。在使用过程中,需要修改主题的`functions.php`文件,并在其中添加相应的代码。代码中包括了获取配置中的QQ机器人API地址和QQ号码,并构建消息内容。最后通过Curl请求将消息发送给QQ机器人。完成设置后,可以在主题设置处设置QQ机器人的API和QQ号码。

    简介

    前文有

    个人看IM的时间比邮件的时间长,所以就还是使用QQ接收通知
    基于 GO-CQhttp 项目
    也可以使用插件实现

    使用

    修改主题 functions.phpfunction themeConfig($form) {下添加

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

    完成之后在主题设置处设置QQ机器人的API 和 QQ号码

    老孙
    资深网民孙先生

    Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /www/sites/imsun.org/index/usr/themes/farallon/comments.php on line 4

    Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/sites/imsun.org/index/usr/themes/farallon/comments.php on line 4
    1. 王云子
      王云子
      2024-07-08 16:15

      这个我以前也弄过,还弄过什么tg通知,微信通知,但是似乎好像没什么大用,哈哈哈

        1. 老孙 作者
          2024-07-08 16:16
          @王云子

          还是有用的, 我一直都挂着呢

    2. 肖寒武
      2024-07-04 11:06

      这个可以,比邮件方便一点。

        1. 老孙 作者
          2024-07-04 11:20
          @肖寒武

          对吧,我也觉得更方便些