discord-webhook

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Discord Webhook

Discord Webhook

Use Discord Webhooks via direct
curl
calls to send messages to Discord channels without setting up a bot.
Official docs:
https://discord.com/developers/docs/resources/webhook

通过直接调用
curl
来使用Discord Webhook,无需设置机器人即可向Discord频道发送消息
官方文档:
https://discord.com/developers/docs/resources/webhook

When to Use

适用场景

Use this skill when you need to:
  • Send notifications to Discord channels
  • Post alerts from CI/CD pipelines
  • Share updates with rich embeds
  • Upload files to channels
  • Simple integrations without bot complexity

当你需要以下操作时,可以使用此方法:
  • 发送通知到Discord频道
  • 从CI/CD流水线推送告警
  • 通过富嵌入内容分享更新
  • 向频道上传文件
  • 无需机器人复杂度的简单集成

Prerequisites

前提条件

  1. In Discord, go to Server Settings → Integrations → Webhooks
  2. Click "New Webhook"
  3. Choose a name and target channel
  4. Click "Copy Webhook URL"
bash
export DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/1234567890/abcdefg..."
Security: Never expose webhook URLs publicly - they require no authentication.

Important: When using
$VAR
in a command that pipes to another command, wrap the command containing
$VAR
in
bash -c '...'
. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"'
  1. 在Discord中,进入服务器设置 → 集成 → Webhooks
  2. 点击「新建Webhook」
  3. 选择名称和目标频道
  4. 点击「复制Webhook URL」
bash
export DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/1234567890/abcdefg..."
安全提示: 切勿公开暴露Webhook URL——它们无需认证即可使用。

重要提示: 当在包含管道的命令中使用
$VAR
时,请将包含
$VAR
的命令用
bash -c '...'
包裹。由于Claude Code的一个bug,直接使用管道时环境变量会被静默清除。
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"'

How to Use

使用方法

All examples below assume you have
DISCORD_WEBHOOK_URL
set.

以下所有示例均假设你已设置好
DISCORD_WEBHOOK_URL
环境变量。

1. Send Simple Message

1. 发送简单消息

Write to
/tmp/discord_webhook_request.json
:
json
{
  "content": "Hello from webhook!"
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

写入
/tmp/discord_webhook_request.json
json
{
  "content": "Hello from webhook!"
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

2. Send with Custom Username and Avatar

2. 发送带自定义用户名和头像的消息

Write to
/tmp/discord_webhook_request.json
:
json
{
  "content": "Alert!",
  "username": "Alert Bot",
  "avatar_url": "https://i.imgur.com/4M34hi2.png"
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

写入
/tmp/discord_webhook_request.json
json
{
  "content": "Alert!",
  "username": "Alert Bot",
  "avatar_url": "https://i.imgur.com/4M34hi2.png"
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

3. Send Rich Embed

3. 发送富嵌入内容

Write to
/tmp/discord_webhook_request.json
:
json
{
  "embeds": [
    {
      "title": "Deployment Complete",
      "description": "Version 1.2.3 deployed to production",
      "color": 5763719,
      "fields": [
        {
          "name": "Environment",
          "value": "Production",
          "inline": true
        },
        {
          "name": "Status",
          "value": "Success",
          "inline": true
        }
      ],
      "timestamp": "2025-01-01T12:00:00.000Z"
    }
  ]
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json
Common colors (decimal):
  • Green:
    5763719
  • Red:
    15548997
  • Blue:
    5793266
  • Yellow:
    16776960
  • Orange:
    16744192

写入
/tmp/discord_webhook_request.json
json
{
  "embeds": [
    {
      "title": "Deployment Complete",
      "description": "Version 1.2.3 deployed to production",
      "color": 5763719,
      "fields": [
        {
          "name": "Environment",
          "value": "Production",
          "inline": true
        },
        {
          "name": "Status",
          "value": "Success",
          "inline": true
        }
      ],
      "timestamp": "2025-01-01T12:00:00.000Z"
    }
  ]
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json
常用颜色(十进制):
  • 绿色:
    5763719
  • 红色:
    15548997
  • 蓝色:
    5793266
  • 黄色:
    16776960
  • 橙色:
    16744192

4. Send Error Alert

4. 发送错误告警

Write to
/tmp/discord_webhook_request.json
:
json
{
  "embeds": [
    {
      "title": "Error Alert",
      "description": "Database connection failed",
      "color": 15548997,
      "fields": [
        {
          "name": "Service",
          "value": "api-server"
        },
        {
          "name": "Error",
          "value": "Connection timeout"
        }
      ],
      "footer": {
        "text": "Monitor"
      }
    }
  ]
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

写入
/tmp/discord_webhook_request.json
json
{
  "embeds": [
    {
      "title": "Error Alert",
      "description": "Database connection failed",
      "color": 15548997,
      "fields": [
        {
          "name": "Service",
          "value": "api-server"
        },
        {
          "name": "Error",
          "value": "Connection timeout"
        }
      ],
      "footer": {
        "text": "Monitor"
      }
    }
  ]
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

5. Send File Attachment

5. 发送附件文件

Write to
/tmp/discord_webhook_payload.json
:
json
{
  "content": "Screenshot attached"
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -F "file1=@screenshot.png" -F 'payload_json=@/tmp/discord_webhook_payload.json'

写入
/tmp/discord_webhook_payload.json
json
{
  "content": "Screenshot attached"
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -F "file1=@screenshot.png" -F 'payload_json=@/tmp/discord_webhook_payload.json'

6. Send Multiple Files

6. 发送多个文件

Write to
/tmp/discord_webhook_payload.json
:
json
{
  "content": "Log files attached"
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -F "file1=@error.log" -F "file2=@debug.log" -F 'payload_json=@/tmp/discord_webhook_payload.json'

写入
/tmp/discord_webhook_payload.json
json
{
  "content": "Log files attached"
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -F "file1=@error.log" -F "file2=@debug.log" -F 'payload_json=@/tmp/discord_webhook_payload.json'

7. Send Multiple Embeds

7. 发送多个嵌入内容

Write to
/tmp/discord_webhook_request.json
:
json
{
  "embeds": [
    {
      "title": "Build Started",
      "color": 16776960
    },
    {
      "title": "Tests Passed",
      "color": 5763719
    },
    {
      "title": "Deployed",
      "color": 5793266
    }
  ]
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

写入
/tmp/discord_webhook_request.json
json
{
  "embeds": [
    {
      "title": "Build Started",
      "color": 16776960
    },
    {
      "title": "Tests Passed",
      "color": 5763719
    },
    {
      "title": "Deployed",
      "color": 5793266
    }
  ]
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

8. Send with Mention

8. 发送带提及的消息

Write to
/tmp/discord_webhook_request.json
:
json
{
  "content": "<@<your-user-id>> Check this out!",
  "allowed_mentions": {
    "users": ["<your-user-id>"]
  }
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json
Replace
<your-user-id>
with the actual Discord user ID.

写入
/tmp/discord_webhook_request.json
json
{
  "content": "<@<your-user-id>> Check this out!",
  "allowed_mentions": {
    "users": ["<your-user-id>"]
  }
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json
<your-user-id>
替换为实际的Discord用户ID。

9. Send Silent Message (No Notification)

9. 发送静默消息(无通知)

Write to
/tmp/discord_webhook_request.json
:
json
{
  "content": "Silent update",
  "flags": 4096
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

写入
/tmp/discord_webhook_request.json
json
{
  "content": "Silent update",
  "flags": 4096
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

10. CI/CD Pipeline Notification

10. CI/CD流水线通知

Write to
/tmp/discord_webhook_request.json
:
json
{
  "username": "GitHub Actions",
  "embeds": [
    {
      "title": "Pipeline Status",
      "color": 5763719,
      "fields": [
        {
          "name": "Repository",
          "value": "myorg/myrepo",
          "inline": true
        },
        {
          "name": "Branch",
          "value": "main",
          "inline": true
        },
        {
          "name": "Commit",
          "value": "abc1234",
          "inline": true
        },
        {
          "name": "Status",
          "value": "Success"
        }
      ],
      "timestamp": "2025-01-01T12:00:00.000Z"
    }
  ]
}
Then run:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

写入
/tmp/discord_webhook_request.json
json
{
  "username": "GitHub Actions",
  "embeds": [
    {
      "title": "Pipeline Status",
      "color": 5763719,
      "fields": [
        {
          "name": "Repository",
          "value": "myorg/myrepo",
          "inline": true
        },
        {
          "name": "Branch",
          "value": "main",
          "inline": true
        },
        {
          "name": "Commit",
          "value": "abc1234",
          "inline": true
        },
        {
          "name": "Status",
          "value": "Success"
        }
      ],
      "timestamp": "2025-01-01T12:00:00.000Z"
    }
  ]
}
然后运行:
bash
curl -s -X POST "${DISCORD_WEBHOOK_URL}" -H "Content-Type: application/json" -d @/tmp/discord_webhook_request.json

Embed Structure

嵌入内容结构

json
{
  "title": "Title text",
  "description": "Description text",
  "url": "https://example.com",
  "color": 5763719,
  "fields": [
  {"name": "Field 1", "value": "Value 1", "inline": true}
  ],
  "author": {"name": "Author", "icon_url": "https://..."},
  "footer": {"text": "Footer text"},
  "thumbnail": {"url": "https://..."},
  "image": {"url": "https://..."},
  "timestamp": "2025-01-01T12:00:00.000Z"
}

json
{
  "title": "Title text",
  "description": "Description text",
  "url": "https://example.com",
  "color": 5763719,
  "fields": [
  {"name": "Field 1", "value": "Value 1", "inline": true}
  ],
  "author": {"name": "Author", "icon_url": "https://..."},
  "footer": {"text": "Footer text"},
  "thumbnail": {"url": "https://..."},
  "image": {"url": "https://..."},
  "timestamp": "2025-01-01T12:00:00.000Z"
}

Guidelines

注意事项

  1. Rate limits: 30 requests per 60 seconds per webhook
  2. Message limit: 2000 characters for content
  3. Embed limits: Max 10 embeds, 6000 total characters
  4. File limits: Max 8MB per file (50MB with Nitro boost)
  5. Security: Treat webhook URLs like passwords
  1. 频率限制:每个Webhook每分钟最多30次请求
  2. 消息长度限制:内容最多2000字符
  3. 嵌入内容限制:最多10个嵌入内容,总字符数不超过6000
  4. 文件大小限制:单文件最大8MB(开通Nitro会员后为50MB)
  5. 安全提示:将Webhook URL视为密码一样妥善保管