登录  /  注册
博主信息
博文 56
粉丝 7
评论 11
访问量 215386
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP群机器人定时推送企业微信
樂成的开发笔记
原创
9888人浏览过

1、群机器人功能

- 支持markdown格式、图文卡片等多种消息类型,还可@成员作为单独提醒。

e8e7e4a3588a11493752abe8566cf110dda09ba0_ceshijiqiren.png

- 员工可自己创建群机器人,也可以添加同事发布到公司的机器人使用。

24a2fc60f6342bfb0954c1a4ce1fb00d9ae37e06_ceshijiqiren2.png

官方文档说明接口文档

代码说明:
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;
}

好了,打开浏览器运行大功告成

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
6条评论
再回首 2022-01-20 10:31:10
看着不错,现在一般用PC版企微模拟操作了,提供一些接口 2645542961还可以二次开发
6楼
kanglecheng 2020-04-17 10:27:18
回4楼:这是本地调试用的,正确使用的话,可以在服务器加定时任务去推送
5楼
移动用户-1017974 2020-04-15 13:56:23
一条信息重复发该怎么办?
4楼
移动用户-1124777 2019-10-30 17:20:21
你好博主,你可以实现消息的推送之外,能否实现针对用户的消息内容进行特定的反馈呢?
3楼
kanglecheng 2019-10-30 16:59:45
@all是企业微信@群聊中所有成员的意思
2楼
手机用户1571929779 2019-10-24 23:13:03
请问“@all”这个是哪个知识点,表示啥意思呢?
1楼
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学