用python 写一个企业微信推送信息
时间: 2023-09-26 12:09:55 浏览: 60
要使用Python发送企业微信消息,你可以使用企业微信提供的API接口。以下是一个示例代码:
```python
import requests
import json
# 企业微信应用的相关配置信息
corpid = 'your_corpid' # 企业ID
corpsecret = 'your_corpsecret' # 应用的凭证密钥
agentid = 'your_agentid' # 应用的AgentId
def send_msg(content):
# 获取access_token
url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}'
response = requests.get(url)
access_token = json.loads(response.text)['access_token']
# 构造消息体
msg_data = {
"touser": "@all", # 发送给所有人
"msgtype": "text",
"agentid": agentid,
"text": {
"content": content
}
}
# 发送消息
send_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
response = requests.post(send_url, data=json.dumps(msg_data))
result = json.loads(response.text)
if result['errcode'] == 0:
print("消息发送成功!")
else:
print(f"消息发送失败,错误信息:{result['errmsg']}")
# 调用示例
send_msg("这是一条测试消息")
```
在上述代码中,你需要将`your_corpid`、`your_corpsecret`和`your_agentid`替换为你自己企业微信应用的相关配置信息。然后,调用`send_msg`函数即可发送消息,消息内容通过参数传入。这里的示例是发送给所有人,你也可以根据需要修改接收人的参数。