企业微信通知服务 API文档

返回首页

详细的API使用说明文档,帮助您快速集成企业微信消息推送功能

API概述

企业微信通知服务提供了简单易用的HTTP API,用于向企业微信应用发送消息通知。通过配置您的企业微信应用信息,系统会生成一个唯一的API地址,您可以通过该地址发送消息。

使用前,您需要先在首页创建配置并获取唯一的API地址。

认证与安全

API使用唯一的配置Code作为认证凭证,该Code包含在API URL中。请妥善保管您的配置Code,不要泄露给未授权的人员。

配置Code仅在创建时显示一次,请务必保存。如果遗失,您需要重新创建配置。

发送消息API

请求地址

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调用失败

集成示例

Shell脚本示例

#!/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

Python示例

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)

Node.js示例

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));

常见问题

Q: 如何获取配置Code?

A: 在首页填写您的企业微信应用信息,验证并选择接收成员后,点击"生成通知API"按钮即可获取配置Code。

Q: 消息发送失败怎么办?

A: 请检查以下几点:

  • 配置Code是否正确
  • 请求参数格式是否正确
  • 企业微信应用的配置是否有效
  • 网络连接是否正常

Q: 是否支持发送图片或文件?

A: 目前仅支持发送文本消息,不支持图片或文件附件。

Q: 消息内容有长度限制吗?

A: 根据企业微信API的限制,消息内容不应超过2048个字符。