zoukankan      html  css  js  c++  java
  • 超简单!两步实现Wordpress评论微信通知~

    WordPress收到评论的时候可以通过邮件发送评论通知,但是邮件通知的可能不是那么能及时的查看,所以增加一个微信通知。

    实现方式:

    1、第一种就是通过企业微信群聊机器人来通知。
    2、第二种就是通过server酱来实现通知

    这两种方式各有好坏,通过server酱通知的方式很多,选择性很多。但是毕竟是用的别人的服务,涉及安全,稳定以及需要会员的问题。用自己的企业微信来通知就不需要考虑服务不可用的问题,通知频率也不会限制。缺点是选择性就一个。

    代码:

    企业微信版本:

    date_default_timezone_set("Asia/Shanghai");
    
    // 请求体
    function request_post($url = '', $post_data = array(),$dataType='') {
    	if (empty($url) || empty($post_data)) {
    		return false;
    	}
    	$curlPost = $post_data;
    	$ch = curl_init($url);
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	if($dataType=='json'){
    		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    				'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
    				'Content-Length: ' . strlen($curlPost)
    			)
    		);
    	}
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    	$data = curl_exec($ch);
    	return $data;
    }
    
    // 调用代码
    function commit_send($comment_id)  {  
        $comment = get_comment($comment_id); 
        $url = '企业微信机器人webHook地址';
        // 评论内容
    	$cont = $comment->comment_content; 
    	// 评论人昵称
    	$title = $comment->comment_author;
    	
    	
    	// 文本消息
        $data = array(
        	"msgtype"=>"text",
            "text"=>array(
                "content"=>$cont,
            	"mentioned_list"=>array("@all") // 可以@群全部成员
            )
        );
        
        // 图文消息(二选一)
    	$data = array(
    		"msgtype"=>"news",
    		"news"=>array(
    		    "articles"=>array(
    		        array(
        		        "title"=>date("Y-m-d H:i:s",time())."@".$title."给你发来一条评论",
                        "description"=>$cont,
                        "url"=>"https://cuixinxin.cn/about", // 跳转地址
                        "picurl"=>"http://blogimg.lieme.cn/FmgJkCHkCJWHQdLXAXWjGL204IDx", // 图文消息封面
                    )
    		    )
            )
    	);
    	$res = request_post($url, json_encode($data,'320'),'json');
    }
    add_action('comment_post', 'commit_send', 19, 2);
    

    server酱版本:

    function commit_send($comment_id)  {  
        // 	server酱
        $comment = get_comment($comment_id);  
        $text = '@'.$comment->comment_author.'发来了一条评论';  
        $desp = $comment->comment_content;  
        $key = server酱的key;  
        $postdata = http_build_query(  
            array(  
                'text' => $text,  
                'desp' => $desp  
            )  
         );  
        
        $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('https://sctapi.ftqq.com/'.$key.'.send', false, $context);  
    }  
    add_action('comment_post', 'commit_send', 19, 2);
    

    实现效果

    实现效果

    企业微信注册方式

    20210208150738.png

    Server酱·Turbo版.png

    我是卖坚果的怪叔叔—— 一枚佛系前端开发,会一丢丢摄影,喜欢折腾,爱好美食。分享点前端技巧、笔记以及各种有趣的APP和资源教程♥♥

  • 相关阅读:
    严援朝座右铭
    王治郅 请让爱国走下神坛
    Java 事件处理实例
    SAP ERP 与 Oracle ERP 比较
    Gantt Component for Delphi Pure Pascal code(TsyGantt VCL)
    XMLRPC vs. SOAP
    Interfaces with Constants Only(java中通用常量定义)
    船舶设计软件简介
    DelphiARX 2000i 简介
    JAVA事件适配器用内部类,匿名类实现事件处理
  • 原文地址:https://www.cnblogs.com/lieone/p/15040904.html
Copyright © 2011-2022 走看看