在 Python使用Server酱实现微信接收消息 这篇文章中是使用第三方平台来实现微信消息推送,但现在我们可以使用Python调用企业微信api搭建自己的平台,实现推送消息到微信
配置
注册企业
企业微信官网
创建应用
点击 应用管理
> 创建应用
> 创建后复制 AgentId
、Secret

获取企业ID
点击 我的企业
,在最底部可以看到 企业ID
然后再点击 微信插件
,使用微信扫码并关注就可以接收消息
注:如果微信接收不到消息,打开企业微信,关闭 我
> 设置
> 新消息通知
> 仅在企业微信中接受信息
> 会话消息
、应用消息
获取账号
点击 通讯录
再点击自己的名字就可以看到 账号
Python代码
将 账号
、AgentId
、Secret
、企业ID
填入代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import json import requests
message = '我就试一下'
def wx_push(message): touser = '账号' agentid = 'AgentId' secret = 'Secret' corpid = '企业ID'
json_dict = { "touser": touser, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe": 0, "enable_id_trans": 0, "enable_duplicate_check": 0, "duplicate_check_interval": 1800 }
response = requests.get( f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}") data = json.loads(response.text) access_token = data['access_token']
json_str = json.dumps(json_dict) response_send = requests.post( f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}", data=json_str) return json.loads(response_send.text)['errmsg'] == 'ok'
wx_push(message)
|
参考资料
企业微信内部开发简易教程
更多消息类型