WordPress评论回复微信提醒

用Wordpress建站的朋友都知道文章有评论回复是可以加上邮件提醒的,用php或smtp的方式都可以。但是邮件通知的可能不是那么能及时的查看,所以增加一个微信通知。但是能不能别人给你留言了也有微信推送提醒呢,答案是可以的。

实现方式:

1、第一种就是通过企业微信群聊机器人来通知。

2、第二种就是通过server酱来实现通知

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

3、效果演示:

企业微信版本:

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://yyink.cn", // 跳转地址
                    "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);

 

 

|| 版权声明
作者:云言
链接:https://yyink.cn/archives/141.html
声明:如无特别声明本文即为原创文章仅代表个人观点,版权归《云言博客》所有,欢迎转载,转载请保留原文链接。
THE END
分享
二维码
海报
WordPress评论回复微信提醒
用Wordpress建站的朋友都知道文章有评论回复是可以加上邮件提醒的,用php或smtp的方式都可以。但是邮件通知的可能不是那么能及时的查看,所以增加一个微信通知……
<<上一篇
下一篇>>