python 推送消息到企业微信实现代码
时间: 2023-05-22 22:06:54 浏览: 144
import requests
import json
#填写企业微信的参数,请在企业微信管理后台获取
corpid = '企业ID'
corpsecret = '应用的凭证密钥Secret'
agentid = '企业微信应用的ID'
#获取access_token的请求地址
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
params = {
'corpid': corpid,
'corpsecret': corpsecret
}
r = requests.get(url, params=params)
result = r.json()
access_token = result['access_token']
#发送消息的请求地址
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
headers = {'Content-Type': 'application/json'}
#填写发送消息的参数,请根据具体需要修改
data = {
"toparty": "",#部门ID,不填则发送给全公司所有人
"msgtype": "text",
"agentid": agentid,
"text": {
"content": "这是一条测试消息"
},
"safe": 0
}
r = requests.post(send_url, headers=headers, data=json.dumps(data))
result = r.json()
print(result)
阅读全文