python 发送消息给企业微信
时间: 2023-08-12 17:00:19 浏览: 44
你可以使用企业微信提供的官方 API 来发送消息。下面是一个示例代码,演示如何使用 Python 发送消息给企业微信:
```python
import requests
import json
# 设置企业微信的相关参数
corpid = "your_corpid" # 企业微信的 CorpID
corpsecret = "your_corpsecret" # 应用的 CorpSecret
agentid = 1000002 # 应用的 AgentID
# 获取 access token
def get_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"]
return access_token
# 发送消息
def send_message(text):
access_token = get_access_token()
url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}"
data = {
"touser": "@all", # 发送给所有人
"msgtype": "text",
"agentid": agentid,
"text": {
"content": text
},
"safe": 0
}
response = requests.post(url, json.dumps(data))
return json.loads(response.text)
# 示例发送消息
response = send_message("Hello, 企业微信!")
print(response)
```
请确保替换示例代码中的 `your_corpid`、`your_corpsecret` 和 `agentid` 参数为你自己企业微信的相关信息。此外,请确保你的 Python 环境中已安装 `requests` 库。
以上