python 企业微信消息推送
时间: 2023-08-17 21:15:55 浏览: 67
要在 Python 中推送企业微信消息,你可以使用企业微信提供的 API 接口。以下是一个示例代码,可以帮助你实现消息推送功能:
```python
import requests
def send_wechat_message(corpid, corpsecret, agentid, touser, content):
# 获取 access_token
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
response = requests.get(url)
access_token = response.json().get("access_token")
# 发送消息
url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}"
data = {
"touser": touser,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": content
}
}
response = requests.post(url, json=data)
return response.json()
# 使用示例
corpid = "your_corpid"
corpsecret = "your_corpsecret"
agentid = "your_agentid"
touser = "user_id1|user_id2" # 接收消息的用户,多个用户用竖线分隔
content = "这是一条测试消息"
response = send_wechat_message(corpid, corpsecret, agentid, touser, content)
print(response)
```
在上面的代码中,你需要将 `your_corpid`、`your_corpsecret`、`your_agentid` 替换为你在企业微信中创建的应用的相应信息。另外,`touser` 变量中可以指定多个用户 ID,用竖线 `|` 分隔。
注意:你需要先在企业微信中创建应用并获取到相应的 corpid、corpsecret 和 agentid 才能使用上述代码。