|
超详细企业微信API推送(所有东西都可推送)
转载作者:l煎饼果子
一、背景
最近在研究推送每日课表,一开始是使用 微信公众号接口测试号 来推送的,但是现在公众号消息展示改了,不会显示在消息列表,而是隐藏在“订阅号消息”里面,不仔细看都看不到,很容易被忽略。所以就一直在找其他方法,想到了企业微信 - 可以显示在微信消息主页,且提醒很明显,如下图。
注意:如果你想每日定时推送,需要有自己的服务器,或者使用阿里云函数
二、企业微信注册与配置
1.注册
每个人都可以注册企业微信(免费版企业最大人数是200人,不认证不会影响api使用,足够我们个人使用了)
网址: 企业微信 (qq.com) 注册:
同时,手机或电脑下载企业微信客户端
2.配置
(1)加入企业
注册成功后进入管理后台。让自己和想要推送的人加入企业
(2)开启微信插件
为了让我们的企业微信能直接在微信上看,需要开启微信插件
(3)测试是否能在微信接收消息
能在微信正常收到公告就代表已经成功进入企业,且成功使用微信插件
三、通过自建应用发送消息
注:这个方法需要的要求- 有自己的服务器和域名(且服务器ip为固定ip,域名通过ICP备案)
1.添加自建应用
按照下图添加即可
2.获取应用接口凭证(access_token)
这个凭证可以拥有该应用的全部权限,请妥善保管,不要在前端暴露access_token和secret
(1)获取企业id(corp_id)
这个页面拉到最底部
(2)获取应用的Secret
回到应用管理页面,找到我们刚刚的自建应用,点开,就能看到secret了
发送后,打开企业微信客户端(必须是企业微信,不能是微信),在里面找到“企业微信团队”发给你的secret
(3)获取access_token
向https://qyapi.weixin.qq.com/cgi-bin/gettoken 发送get请求,query参数为刚刚获取的企业id和应用密匙 (参考文档 获取access_token - 文档 - 企业微信开发者中心 (qq.com))
以下是nodejs示例 (注意,access_token有效期为两小时,可以缓存起来,减少请求次数)
官方文档的代码中有一处错误,我改正过来了 (请求参数名写错)
const corp_id = '';// 企业 corp_id
const secret = ''// 当前应用的 secret
// 获取的 access_token
let {data} = await axios.get(`https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corp_id}&corpsecret=${secret}`);
let {access_token}= data;
if(access_token){
console.log('获取 access_token 成功',access_token);
}
else{
res.render('error');
}
# 下方是python获取access_token案例代码:
if pro_first == True:
print("程序第一次运行获取token")
pro_first=False
# 获取企业微信access_token接口:
access_token_get_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corp_id}&corpsecret={secret}"
get_access_token = requests.get(access_token_get_url)
print(f"access_token={get_access_token.json()['access_token']},token过期时间{get_access_token.json()['expires_in']}")
access_token = get_access_token.json()['access_token']
access_token_lose_timestamp = get_access_token.json()['expires_in'] + timestamp
print(f"当前时间戳{timestamp},token过期时间戳{access_token_lose_timestamp}")
else:
if time.time() > access_token_lose_timestamp:
print("企业微信token时效失效,重新获取token")
# 获取企业微信access_token接口:
access_token_get_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corp_id}&corpsecret={secret}"
get_access_token = requests.get(access_token_get_url)
print(f"access_token={get_access_token.json()['access_token']},token过期时间{get_access_token.json()['expires_in']}")
access_token = get_access_token.json()['access_token']
access_token_lose_timestamp = get_access_token.json()['expires_in'] + timestamp
print(f"当前时间戳{timestamp},token过期时间戳{access_token_lose_timestamp}")
else:
print(f"企业微信token时效正在生效中,不获取token,当前时间戳{time.time()},token过期时间戳{access_token_lose_timestamp}")
3.配置ip白名单和可信域名
这一步很关键,官方文档并没有说明,不配置的话,即使有access_token也做不了任何事(这是2022年6月新增的规定,在此之前的自建应用不需要这些限制) 。在应用管理-我们的自建应用界面,拉到最下面,可以看到配置入口。
(1)配置可信域名
输入经过ICP备案的域名,然后点击申请校验
然后就会出现校验流程,下载文件后,放到自己的域名根目录下
放好文件后部署,然后点击确定按钮,提示成功即可。
(2)配置可信ip白名单
在这里面输入你的服务器的ip
4.发送消息
(1)获取应用id(agent_id)
在自建应用界面,可以复制应用id
(2)发送消息
POST请求,nodejs示例如下: (除了文本消息,还可以发送多种颜色的图片、卡片等,这里不一一介绍了,详情可以看官方文档 发送应用消息 - 文档 - 企业微信开发者中心 (qq.com))
const agent_id = '1000063';// 自建应用的 agent_id
const access_token = 'xxxxxx';// access_token
let {data:message_data} = await axios.post(`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${access_token}`,{
"touser" : '@all',//意思是发给所有人
"agentid" : agent_id,
"msgtype" : "text",//类型为文本,可以是其他的,详情可见官方文档
"text" : {
"content" : `Hello World!` //里面填写文本
},
});
在content里面,可以填写你通过请求其他api获得的天气数据(比如百度天气api),也可以输入自己的数据(比如课表)
参数列表:放在请求体中
# 以下是python发送文本的示例:
post_date={
"touser":"@all",
"agentid":"xxxxxx",
"msgtype":"text",
"text":{"content":"测试内容"},
}
# 发送请求
req = requests.post(f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}",json=post_date)
# 以下是python发送图片的示例:(发送图片需要先上传本地图片至企微临时素材库)
# 构造上传临时素材--图片
files = {
'media': open('/data/1.png', 'rb'),
}
# 调用上传临时素材上传图片至企业微信
req = requests.post(f'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=image', files=files)
print(req.text)
# 获取临时图片media_id
media_id = req.json()['media_id']
post_date={
"touser":"@all",
"agentid":"xxxxxx",
"msgtype":"image",
"image":{"media_id":media_id},
}
# 发送图片消息
req = requests.post(f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}",json=post_date)
|