用python制作卡片并发送给企业微信机器人
时间: 2023-10-09 11:14:31 浏览: 56
好的,可以使用 Python 的 requests 库和企业微信机器人的 API 实现。首先,需要获取企业微信机器人的 webhook 地址,可以在企业微信管理后台中创建机器人并获取 webhook 地址。
接下来,可以使用 requests 库发送 POST 请求,将卡片信息作为请求体发送给机器人。以下是一个示例代码:
```python
import requests
# 企业微信机器人 webhook 地址
webhook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
# 卡片消息内容
card_msg = {
"msgtype": "news",
"news": {
"articles": [
{
"title": "这是一条卡片消息",
"description": "用 Python 发送的卡片消息",
"url": "https://www.example.com",
"picurl": "https://www.example.com/image.jpg"
}
]
}
}
# 发送 POST 请求
response = requests.post(webhook_url, json=card_msg)
print(response.text)
```
其中,`webhook_url` 是企业微信机器人的 webhook 地址,需要替换为自己的地址;`card_msg` 是卡片消息内容,可以根据需要自行修改。
发送成功后,机器人将会收到卡片消息。