本文主要是介绍【IoT新乐趣】手把手教程教你搭建企业微信推送消息的demo,文末提供可工作的python版本代码,抓紧时间轮出来耍一耍吧,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 企业微信推送 python版
1.1 功能特点
- 不需要安装企业微信客户端
- 可在微信中直接收到文本消息,内容显全文
1.2 编程基础
基础的 python 语法基础即可
2 企业微信注册
2.1 注册企业
用电脑打开企业微信官网,https://work.weixin.qq.com/, 注册一个企业
2.2 获取企业ID
我的企业 –> 最下边可以看到企业ID: corpid
2.3 获取应用ID
管理企业 –> 应用管理 –> 创建应用
创建完成后可得到应用ID agentid
2.4 获取Secret
还在应用页面, 获取 Secret, 需要在企业微信客户端里接收。
这样就得到了 secret
注意: 一定要在企业微信手机客户端才能查看,这个有点不方便!!!
3 发送文本消息 python
3.1 官方API:
用到了两个API:
获取 access_token : https://work.weixin.qq.com/api/doc/90000/90135/91039
发送应用消息 :
https://work.weixin.qq.com/api/doc/90000/90135/90236
3.2 python 代码
import json
import requests# https://work.weixin.qq.com/wework_admin/frame#profile
corp_id = 'wwxxxxxxxxxxxxxxxxx'
corp_secret = 'Ghxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
agent_id = '10xxxxxx'def get_access_token(corp_id, corp_secret):resp = requests.get(f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corp_id}&corpsecret={corp_secret}')js = json.loads(resp.text)print(js)if js["errcode"] == 0:access_token = js["access_token"]expires_in = js["expires_in"]return access_token, expires_indef wechat_push_text(agent_id, access_token, message):data = {"touser": "@all","msgtype": 'text',"agentid": agent_id,"text": {"content": message},"safe": 0,"enable_id_trans": 0,"enable_duplicate_check": 0,"duplicate_check_interval": 1800}resp = requests.post(f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}', json=data)js = json.loads(resp.text)print(js)if js["errcode"] == 0:return jsaccess_token, expires_in = get_access_token(corp_id, corp_secret)
wechat_push_text(agent_id=agent_id, access_token=access_token, message='wechat notify\ntest')
3.3 代码开源
感兴趣的可以到仓库看下整个工程代码:
https://github.com/shenbo/qiye-wechat-push
4 特别说明
本文转载自:https://shenbo.github.io/2021/08/03/python/wechat-push-%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%8E%A8%E9%80%81/ 版权归原作者所有,侵删!
5 效果图
参考文档:微信开发者中心
这篇关于【IoT新乐趣】手把手教程教你搭建企业微信推送消息的demo,文末提供可工作的python版本代码,抓紧时间轮出来耍一耍吧的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!