1、群机器人功能
- 支持markdown格式、图文卡片等多种消息类型,还可@成员作为单独提醒。
- 员工可自己创建群机器人,也可以添加同事发布到公司的机器人使用。
官方文档说明接口文档
代码说明:
1、因为是用了简单粗暴的定时任务,所以先定义一个开关
config.php
<?php return 1; //0关1开 ?>
2、inde.php
无关紧要的先定义
date_default_timezone_set("Asia/Shanghai"); header('Content-Type:text/html;charset=utf-8'); $run = include 'config.php'; //控制开关 if(!$run) die('process abort'); ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 $times = 60; //等待时间 $urls="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $h = date("H",time()); $i = date("i",time()); $w = date('w'); //时,分,周 //已发送文本例子,图文参考文本 //每天早上8点30推送天气预报 if($h == 8 && $i == 0){ weathers(); //接口自行去获取,我用的是百度的 } //每周五18点提醒周报 if($h == 18 && $i == 0 && $w == 5){ $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=添加机器人提示的KEY'; $cont = "今天是".date("Y-m-d",time())." 星期五,你交周报了吗?"; text($url,$cont,array("@all")); } //每周一10点提醒周例会 if($h == 10 && $i == 0 && $w == 1){ $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=添加机器人提示的KEY'; $cont = "今天是".date("Y-m-d",time())." 星期一,请各部门安排好周例会"; text($url,$cont,array("@all")); } sleep($times); // 等待时间 file_get_contents($urls);
以下是写的方法,可自行封装好
//天气助手 function weathers(){ $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=添加机器人提示的KEY'; $resdata = json_decode(getWeather('城市'),true); $weatherData = $resdata['results'][0]; $tipt = ''; foreach($weatherData['index'] as $indexkey => $indexvalue){ $tipt.="> ##### ".$indexvalue['tipt']."(".$indexvalue['zs'].")\n".$indexvalue['des']."\n"; } $weather_data = $weatherData['weather_data'][0]; $weatherstr = $weather_data['date'].",\n ##### ".$weather_data['weather'].','.$weather_data['wind'].",".$weather_data['temperature']; $data = array( "msgtype"=>"markdown", "markdown"=>array( "content"=>'#### '.$weatherData['currentCity']." :".$weatherstr."\n".$tipt, "mentioned_list"=>array("@all") ) ); $res = request_post($url, json_encode($data,'320'),'json'); print_r($res); } //发送文本 function text($url,$cont,$list){ $data = array( "msgtype"=>"text", "text"=>array( "content"=>$cont, "mentioned_list"=>$list ) ); $res = request_post($url, json_encode($data,'320'),'json'); print_r($res); } /** * 根据城市名称/ID获取详细天气预报 * @param string $city [城市名称/ID] * @return array */ function getWeather($city){ //百度天气接口API $location = $city; //地区 $ak = ""; //秘钥,需要申请,百度为了防止频繁请求 $weatherURL = "http://api.map.baidu.com/telematics/v3/weather?location=$location&output=json&ak=$ak"; $res = httpGet($weatherURL); return $res; } /** * 模拟get进行url请求 * @param string $url * @return json */ function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } /** * 模拟post进行url请求 * @param string $url * @param array $post_data * @param string $dataType * @return bool|mixed */ 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; }
好了,打开浏览器运行大功告成