想要实现WordPress新评论消息钉钉机器人消息通知,
首先你要有一个钉钉号,再创建一个普通群,群内选择一个自定义机器人,并且获得机器人的webhook和加签秘钥
function request_by_curl($remote_server, $post_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8')); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); $data = curl_exec($ch); curl_close($ch); return $data; } function boxmoe_msg_comment($comment_id){ $comment = get_comment($comment_id); $siteurl = get_bloginfo('url'); $text = '文章《' . get_the_title($comment->comment_post_ID) . '》有新的评论!'; $message = $text . "n" . "作者: $comment->comment_author n邮箱: $comment->comment_author_email n评论: $comment->comment_content n 点击查看:$siteurl/?p=$comment->comment_post_ID#comments"; $time = intval(microtime(true) * 1000); $secret = '机器人加签秘钥'; $sign = urlencode(base64_encode(hash_hmac('sha256', "{$time}n{$secret}", $secret, true))); $apiurl = 'https://oapi.dingtalk.com/robot/send?access_token=你的webhook×tamp='.$time.'&sign='.$sign.''; $data = array ('msgtype' => 'text','text' => array ('content' => $message)); $data_string = json_encode($data); return $result = request_by_curl($apiurl, $data_string); } add_action('comment_post', 'boxmoe_msg_comment', 19, 2);
文章来源于互联网:个人钉钉使用群机器人API实现WordPress评论消息通知代码