之前有QQ机器人实现网站推送评论,需要搭建机器人(虽然比较简单),但也提高了门槛(可能需要服务器)。喜欢折腾可以去试试。
Go语言全平台QQ机器人搭建 Go语言全平台QQ机器人搭建 go-cqhttp使用 mirai 以及 MiraiGo 开发的 cqhttp golang 原生实现。 [gitcard type="1" url="https://github.com/Mrs4s... 时间:2021/8/29 分类:网络技术 阅读:6260

今天给搭建分享使用企业微信推送WordPress网站评论,理论上只需要能运行php即可。与此功能相似的Server酱大可不用(后期在割韭菜,付费才无限使用)
Server酱-网站小帮手 Server酱-网站小帮手 现已不推荐Server酱,因为限定免费额度为5个太少,更甚的是两条消息后就不工作,于是推荐永久免费的Qmsg酱。 [yx_embed_post ids=4351] 喜欢玩微信的有钱人仍可选择使用Ser... 时间:2020/9/3 分类:网络技术 阅读:3548

配置步骤

1、注册企业微信,个人也可以注册

2、注册后,登陆企业微信,完善基本信息

3、点击应用管理->自建->创建应用

4、创建成功后,简单修改下自己的资料

5、获取企业ID和应用的AgentId和Secret

 

6、添加核心文件

服务器(或虚拟主机等)新建一个目录,创建文件access_token.php和index.php

进入index.php,添加以下内容

<?php

$url = $_POST['url'];
$title = $_POST['title'];
$description = $_POST['description'];
// 声明页面header
header("Content-type:text/html;charset=utf-8");
 
// 获取access_token
function getToken(){
 
    // 定义id和secret
    $corpid='你的企业微信企业ID';
    $corpsecret='你的应用的secret';
 
    // 读取access_token
    include './access_token.php';
 
    // 判断是否过期
    if (time() > $access_token['expires']){
 
        // 如果已经过期就得重新获取并缓存
        $access_token = array();
        $access_token['access_token'] = getNewToken($corpid,$corpsecret);
        $access_token['expires']=time()+7000;
         
        // 将数组写入php文件
        $arr = '<?php'.PHP_EOL.'$access_token = '.var_export($access_token,true).';'.PHP_EOL.'?>';
        $arrfile = fopen("./access_token.php","w");
        fwrite($arrfile,$arr);
        fclose($arrfile);
 
        // 返回当前的access_token
        return $access_token['access_token'];
 
    }else{
 
        // 如果没有过期就直接读取缓存文件
        return $access_token['access_token'];
    }
}
 
// 获取新的access_token
function getNewToken($corpid,$corpsecret){
    $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$corpsecret}";
    $access_token_Arr =  https_request($url);
    return $access_token_Arr['access_token'];
}
 
// curl请求函数
function https_request ($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $out = curl_exec($ch);
    curl_close($ch);
    return  json_decode($out,true);
}
 
// 发送应用消息函数
function send($data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.getToken());
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    return curl_exec($ch);
}
 
// 文本卡片消息体
$postdata = array(
    'touser' => '@all',
    'msgtype' => 'textcard',
    'agentid' => '应用的AgentId',
    'textcard' => array(
        'title' => $title,
        'description' => $description,
        'url' => $url,
        'btntxt' => '阅读全文',
    ),
    'enable_id_trans' => 0,
    'enable_duplicate_check' => 0,
    'duplicate_check_interval' => 1800
);
 
// 调用发送函数
echo send(json_encode($postdata));
?>

7、编辑WordPress功能函数

WordPress的主题functions.php添加以下内容

//微信推送消息
function push_weixin($comment_id)
{
    // 通过 comment_id 获取 comment 全部信息
    $comment = get_comment($comment_id);
    $siteurl = get_bloginfo('url');
    //  根据自己需求,产生相关描述,可以包括文章内容、评论人、IP、评论内容等
    $title = '文章 《' . get_the_title($comment->comment_post_ID) . '》 有新评论啦!';
    $desp = "作者: $comment->comment_author \n邮箱: $comment->comment_author_email \n评论: $comment->comment_content";
    $url = "$siteurl/?p=$comment->comment_post_ID#comments";
    // 封装一个 Object 对象,其 msg 字段是我们需要推送到 QQ 的消息内容
    $postdata = http_build_query(
        array(
            'title' => $title,
            'description' => $desp,
            'url' => $url
        )
    );
    // 一个 POST 请求
    $opts = array('http' =>
        array(
            'method' => 'POST',
            'header' => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );
    $context = stream_context_create($opts);  
    // 将自己的接口地址填在这里
    return $result = file_get_contents('你创建的接口地址/index.php', false, $context);
}
// 挂载 WordPress 评论提交的接口
add_action('comment_post', 'push_weixin', 19, 2);

网站留言测试成功

参考资料

一个php文件实现企业微信推送通知,企业微信实现发送应用消息,推送通知到微信 

Invitation
QQ Group
1095632335

created:04/01/2020

Welcome to the Group

Use this card to join us and participate in a pleasant discussion together .

Welcome to JISHUSONGSHU Group,wish you a nice day .