Webhook 通知工具
一个统一的webhook发送工具,支持多种主流平台的webhook通知。
支持的平台
| 平台 | 消息类型 | 支持功能 |
|---|
| 钉钉 | Text/Markdown/Link/ActionCard | @用户、@所有人、按钮交互 |
| 企业微信 |
Text/Markdown/Image/File/News | 图文消息、@用户 |
|
飞书 | Text/Post/Interactive/ShareCard | 富文本、卡片、按钮 |
|
Slack | Text/Block/Attachment | 格式化消息、按钮 |
|
自定义HTTP | JSON/Form-Data/Raw | 完全自定义请求 |
快速开始
1. 发送钉钉消息
CODEBLOCK0
2. 发送企业微信消息
CODEBLOCK1
3. 发送飞书消息
CODEBLOCK2
4. 发送Slack消息
CODEBLOCK3
5. 自定义HTTP请求
CODEBLOCK4
函数参考
Send-WebhookDingTalk
发送钉钉webhook消息。
参数:
- -
WebhookUrl (string): webhook地址 - INLINECODE1 (string): 文本消息内容
- INLINECODE2 (string): 消息类型
text|markdown|actionCard,默认 INLINECODE6 - INLINECODE7 (string): 消息标题(用于markdown和actionCard)
- INLINECODE8 (string): markdown内容
- INLINECODE9 (string): actionCard的文本内容
- INLINECODE10 (string): 按钮方向
0横向|1纵向 - INLINECODE13 (array): 按钮列表 INLINECODE14
- INLINECODE15 (array): @的手机号列表
- INLINECODE16 (array): @的userId列表
- INLINECODE17 (bool): 是否@所有人
- INLINECODE18 (string): 添加关键字(安全限制)
示例:
CODEBLOCK5
Send-WebhookWeCom
发送企业微信webhook消息。
参数:
- -
WebhookUrl (string): webhook地址 - INLINECODE20 (string): 文本消息内容
- INLINECODE21 (string): 消息类型
text|markdown|image|news| INLINECODE26 - INLINECODE27 (string): markdown内容
- INLINECODE28 (string): 媒体ID(用于image/file)
- INLINECODE29 (array): 图文消息列表
- INLINECODE30 (array): @的手机号列表
- INLINECODE31 (bool): 是否@所有人
示例:
CODEBLOCK6
Send-WebhookFeishu
发送飞书webhook消息。
参数:
- -
WebhookUrl (string): webhook地址 - INLINECODE33 (string): 文本消息内容
- INLINECODE34 (string): 消息类型
text|post|interactive| INLINECODE38 - INLINECODE39 (string): 卡片标题
- INLINECODE40 (array): 富文本内容元素
- INLINECODE41 (array): 交互元素(按钮等)
- INLINECODE42 (array): @的open_id列表
示例:
CODEBLOCK7
Send-WebhookSlack
发送Slack webhook消息。
参数:
- -
WebhookUrl (string): webhook地址 - INLINECODE44 (string): 文本消息内容
- INLINECODE45 (string): 消息类型
text| INLINECODE47 - INLINECODE48 (array): Block Kit块
- INLINECODE49 (array): 附件列表
- INLINECODE50 (string): 目标频道(覆盖默认)
- INLINECODE51 (string): 发送者名称
- INLINECODE52 (string): 图标emoji
- INLINECODE53 (string): 图标URL
示例:
CODEBLOCK8
Send-WebhookCustom
发送自定义HTTP请求。
参数:
- -
Url (string): 目标URL - INLINECODE55 (string): HTTP方法
GET|POST|PUT|DELETE,默认 INLINECODE60 - INLINECODE61 (object 或 string): 请求体
- INLINECODE62 (string): Content-Type,默认 INLINECODE63
- INLINECODE64 (hashtable): 请求头
- INLINECODE65 (int): 超时秒数,默认 30
- INLINECODE66 (bool): 使用基础认证
- INLINECODE67 (string): 用户名
- INLINECODE68 (string): 密码
示例:
CODEBLOCK9
高级用法
1. 环境变量配置
为了避免暴露敏感信息,建议将webhook URL存储在环境变量中:
PowerShell 配置:
CODEBLOCK10
使用环境变量:
CODEBLOCK11
Linux/Mac 配置:
# ~/.bashrc 或 ~/.zshrc
export DINGTALK_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=..."
export WECOM_WEBHOOK="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=..."
2. 配置文件
创建配置文件 webhook-config.ps1:
CODEBLOCK13
使用配置:
. .\webhook-config.ps1
Send-WebhookDingTalk -WebhookUrl $WebhookConfig.DingTalk.Monitoring.Url -Message "告警" -Keyword $WebhookConfig.DingTalk.Monitoring.Keyword
3. 模板化消息
创建消息模板 message-templates.ps1:
CODEBLOCK15
4. 批量发送
CODEBLOCK16
5. 重试机制
CODEBLOCK17
6. Webhook测试工具
CODEBLOCK18
安全最佳实践
1. 关键字验证
钉钉和企业微信webhook通常需要设置关键字验证:
CODEBLOCK19
2. 签名验证(企业微信)
如果使用企业微信签名验证:
CODEBLOCK20
常见错误处理
错误1: 关键字缺失
错误信息:钉钉: INLINECODE71
解决:
# 确保消息包含关键字
Send-WebhookDingTalk -WebhookUrl $url -Message "【openclaw】告警" -Keyword "openclaw"
错误2: 超时
错误信息: 请求超时
解决:
# 增加超时时间
Send-WebhookDingTalk -WebhookUrl $url -Message "测试" -Timeout 60
错误3: IP限制
错误信息: INLINECODE72
解决:
在钉钉机器人设置中添加服务器IP到白名单。
完整示例
示例1: 系统监控告警
CODEBLOCK23
示例2: 部署通知
CODEBLOCK24
示例3: Elasticsearch 异常告警
CODEBLOCK25
配置文件位置
将 webhook 函数库保存为:
- - Windows: INLINECODE73
- 引用方式: INLINECODE74
版本历史
- - v1.0 (2026-03-19): 初始版本,支持钉钉、企业微信、飞书、Slack和自定义HTTP
技术支持
如有问题或建议,请检查:
- 1. Webhook URL配置是否正确
- 网络连接是否正常(防火墙/代理)
- 消息格式是否符合平台要求
- 关键字/签名等安全配置是否正确
Webhook 通知工具
一个统一的webhook发送工具,支持多种主流平台的webhook通知。
支持的平台
| 平台 | 消息类型 | 支持功能 |
|---|
| 钉钉 | Text/Markdown/Link/ActionCard | @用户、@所有人、按钮交互 |
| 企业微信 |
Text/Markdown/Image/File/News | 图文消息、@用户 |
|
飞书 | Text/Post/Interactive/ShareCard | 富文本、卡片、按钮 |
|
Slack | Text/Block/Attachment | 格式化消息、按钮 |
|
自定义HTTP | JSON/Form-Data/Raw | 完全自定义请求 |
快速开始
1. 发送钉钉消息
powershell
文本消息
$webhook = https://oapi.dingtalk.com/robot/send?access
token=YOURTOKEN
Send-WebhookDingTalk -WebhookUrl $webhook -Message 【告警】系统异常 -AtAll $true
Markdown 消息
Send-WebhookDingTalk -WebhookUrl $webhook -Type markdown -Title 告警 -Content ## 内容n详情:xxx
包含附件的卡片消息
Send-WebhookDingTalk -WebhookUrl $webhook -Type actionCard -Title 告警详情 -Text 系统CPU使用率超过90% -Buttons @(@{title=查看详情;url=https://example.com})
2. 发送企业微信消息
powershell
$webhook = https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY
文本消息 @指定用户
Send-WebhookWeCom -WebhookUrl $webhook -Message 告警信息 -AtUsers @(13800138000)
Markdown 消息
Send-WebhookWeCom -WebhookUrl $webhook -Type markdown -Content
告警n系统异常,请及时处理
3. 发送飞书消息
powershell
$webhook = https://open.feishu.cn/open-apis/bot/v2/hook/YOUR_WEBHOOK
文本消息
Send-WebhookFeishu -WebhookUrl $webhook -Message 【openclaw】告警
富文本卡片
Send-WebhookFeishu -WebhookUrl $webhook -Type post -Title 系统告警 -Content @(@{tag=text;text=CPU使用率:95%})
4. 发送Slack消息
powershell
$webhook = https://hooks.slack.com/services/YOUR/WEBHOOK/URL
简单文本
Send-WebhookSlack -WebhookUrl $webhook -Message System Alert: CPU high
格式化消息 (Block Kit)
Send-WebhookSlack -WebhookUrl $webhook -Type block -Blocks @(
@{type=section;text=@{type=mrkdwn;text=
AlertnCPU: 95%}}
@{type=actions;elements=@(@{type=button;text=@{type=plain_text;text=View};url=https://example.com})}
)
5. 自定义HTTP请求
powershell
完全自定义
Send-WebhookCustom -Url https://your-webhook.com/endpoint
-Method POST
-ContentType application/json
-Body @{message=custom webhook data}
-Headers @{X-Custom-Header=value}
函数参考
Send-WebhookDingTalk
发送钉钉webhook消息。
参数:
- - WebhookUrl (string): webhook地址
- Message (string): 文本消息内容
- Type (string): 消息类型 text|markdown|actionCard,默认 text
- Title (string): 消息标题(用于markdown和actionCard)
- Content (string): markdown内容
- Text (string): actionCard的文本内容
- BtnOrientation (string): 按钮方向 0横向|1纵向
- Buttons (array): 按钮列表 @(@{title=btn1;url=url1})
- AtMobiles (array): @的手机号列表
- AtUserIds (array): @的userId列表
- IsAtAll (bool): 是否@所有人
- Keyword (string): 添加关键字(安全限制)
示例:
powershell
文本消息 @所有人
Send-WebhookDingTalk -WebhookUrl $url -Message 【openclaw】告警 -IsAtAll $true
Markdown @指定用户
Send-WebhookDingTalk -WebhookUrl $url -Type markdown -Title 告警 -Content 系统异常 -AtMobiles @(13800138000)
ActionCard带按钮
Send-WebhookDingTalk -WebhookUrl $url -Type actionCard -Title 告警
-Text CPU使用率:95%
-Buttons @(@{title=查看详情;url=https://example.com}, @{title=忽略;url=https://example.com/ignore})
Send-WebhookWeCom
发送企业微信webhook消息。
参数:
- - WebhookUrl (string): webhook地址
- Message (string): 文本消息内容
- Type (string): 消息类型 text|markdown|image|news|file
- Content (string): markdown内容
- MdId (string): 媒体ID(用于image/file)
- Articles (array): 图文消息列表
- AtUsers (array): @的手机号列表
- IsAtAll (bool): 是否@所有人
示例:
powershell
文本消息
Send-WebhookWeCom -WebhookUrl $url -Message 系统告警
Markdown
Send-WebhookWeCom -WebhookUrl $url -Type markdown -Content
标题n内容
图文消息
Send-WebhookWeCom -WebhookUrl $url -Type news -Articles @(
@{title=标题;description=描述;url=http://example.com;picurl=http://example.com/img.jpg}
)
Send-WebhookFeishu
发送飞书webhook消息。
参数:
- - WebhookUrl (string): webhook地址
- Message (string): 文本消息内容
- Type (string): 消息类型 text|post|interactive|shareCard
- Title (string): 卡片标题
- Content (array): 富文本内容元素
- Elements (array): 交互元素(按钮等)
- AtUsers (array): @的open_id列表
示例:
powershell
文本消息
Send-WebhookFeishu -WebhookUrl $url -Message 【openclaw】告警
富文本卡片
Send-WebhookFeishu -WebhookUrl $url -Type post -Title 系统告警 -Content @(
@{tag=text;text=CPU使用率:95%}
)
交互卡片
Send-WebhookFeishu -WebhookUrl $url -Type interactive -Title 确认操作
-Elements @(@{tag=button;text=@{tag=plain_text;content=确认};type=primary;url=https://example.com})
Send-WebhookSlack
发送Slack webhook消息。
参数:
- - WebhookUrl (string): webhook地址
- Message (string): 文本消息内容
- Type (string): 消息类型 text|block
- Blocks (array): Block Kit块
- Attachments (array): 附件列表
- Channel (string): 目标频道(覆盖默认)
- Username (string): 发送者名称
- IconEmoji (string): 图标emoji
- IconUrl (string): 图标URL
示例:
powershell
简单文本
Send-WebhookSlack -WebhookUrl $url -Message System Alert
Block Kit
Send-WebhookSlack -WebhookUrl $url -Type block -Blocks @(
@{type=section;text=@{type=mrkdwn;text=
AlertnCPU: 95%}},
@{type=actions;elements=@(@{type=button;text=@{type=plain_text;text=View};url=https://example.com