详细的API使用说明文档,帮助您快速集成企业微信消息推送功能
企业微信通知服务提供了简单易用的HTTP API,用于向企业微信应用发送消息通知。通过配置您的企业微信应用信息,系统会生成一个唯一的API地址,您可以通过该地址发送消息。
API使用唯一的配置Code作为认证凭证,该Code包含在API URL中。请妥善保管您的配置Code,不要泄露给未授权的人员。
POST /api/notify/{your_code}
其中 {your_code} 是您创建配置时获得的唯一Code
Content-Type: application/json
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| title | String | 否 | 消息标题,可选。如果提供,将作为消息的第一行显示,并会加粗处理。 |
| content | String | 是 | 消息内容,必填。支持简单的markdown格式,如加粗、链接等。 |
curl -X POST "http://your-server.com/api/notify/your-code-here" \
-H "Content-Type: application/json" \
-d '{
"title": "服务器告警",
"content": "CPU使用率超过90%,请及时处理!\n\n**详细信息**:\n- 服务器:web-server-01\n- 时间:2023-09-15 14:30:45\n- 当前负载:95%"
}'
{
"message": "发送成功",
"response": {
"errcode": 0,
"errmsg": "ok",
"msgid": "MSGID1234567890"
}
}
| HTTP状态码 | 说明 |
|---|---|
| 200 | 请求成功 |
| 400 | 参数错误,如缺少必填参数 |
| 404 | 配置不存在,请检查Code是否正确 |
| 500 | 服务器内部错误或企业微信API调用失败 |
#!/bin/bash
# 监控服务器CPU使用率并发送告警
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
THRESHOLD=90
if (( $(echo "$CPU_USAGE > $THRESHOLD" | bc -l) )); then
curl -X POST "http://your-server.com/api/notify/your-code-here" \
-H "Content-Type: application/json" \
-d "{
\"title\": \"CPU告警\",
\"content\": \"服务器CPU使用率: ${CPU_USAGE}%,超过阈值${THRESHOLD}%\"
}"
fi
import requests
import json
def send_notification(title, content):
url = "http://your-server.com/api/notify/your-code-here"
headers = {"Content-Type": "application/json"}
data = {
"title": title,
"content": content
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
# 使用示例
result = send_notification(
"数据库备份完成",
"数据库备份已完成\n- 数据库:user_db\n- 备份大小:1.2GB\n- 备份时间:2023-09-15 02:00:00"
)
print(result)
const axios = require('axios');
async function sendNotification(title, content) {
try {
const response = await axios.post('http://your-server.com/api/notify/your-code-here', {
title,
content
}, {
headers: {
'Content-Type': 'application/json'
}
});
return response.data;
} catch (error) {
console.error('发送通知失败:', error.response ? error.response.data : error.message);
throw error;
}
}
// 使用示例
sendNotification('部署完成', '项目已成功部署到生产环境\n\n版本: v1.2.3\n部署时间: 2023-09-15 15:30:00')
.then(result => console.log('通知发送成功:', result))
.catch(err => console.error('通知发送失败:', err));
A: 在首页填写您的企业微信应用信息,验证并选择接收成员后,点击"生成通知API"按钮即可获取配置Code。
A: 请检查以下几点:
A: 目前仅支持发送文本消息,不支持图片或文件附件。
A: 根据企业微信API的限制,消息内容不应超过2048个字符。