跳转到内容

Webhook

Webhook events provider 让你的数字员工能够接收来自 GitHub、Stripe、Shopify 等外部服务的 HTTP webhook。

  1. 配置 Provider

    answers.json
    {
    "events-webhook": {
    "enabled": true,
    "public_base_url": "https://your-domain.ngrok-free.app"
    }
    }
  2. 运行设置

    Terminal window
    gtc setup --answers answers.json ./my-bundle
  3. 注册 Webhook

    在你的外部服务中使用生成的 webhook URL:

    https://your-domain.com/events/webhook/{tenant}/{event_type}
选项必填说明
enabled启用/禁用 provider
public_base_urlwebhook 的公网 URL
secret_header用于签名校验的 header 名称
secret_key用于签名校验的密钥
POST https://your-domain.com/events/webhook/{tenant}/{event_type}

示例:

POST https://your-domain.com/events/webhook/demo/order.created
flows/on_webhook.ygtc
name: handle_webhook
version: "1.0"
nodes:
- id: process
type: script
config:
script: |
let data = event.payload;
// Process webhook data
data
next: respond
- id: respond
type: http_response
config:
status: 200
body:
success: true
triggers:
- type: event
event_type: "order.created"
target: process
nodes:
- id: check_event
type: branch
config:
conditions:
- expression: "headers['x-github-event'] == 'push'"
next: handle_push
- expression: "headers['x-github-event'] == 'pull_request'"
next: handle_pr
default: ignore
- id: handle_push
type: script
config:
script: |
let commits = event.payload.commits;
format!("Received {} commits", commits.len())
triggers:
- type: event
event_type: "github"
target: check_event
nodes:
- id: handle_stripe
type: branch
config:
conditions:
- expression: "event.payload.type == 'payment_intent.succeeded'"
next: payment_success
- expression: "event.payload.type == 'payment_intent.failed'"
next: payment_failed
triggers:
- type: event
event_type: "stripe"
target: handle_stripe
answers.json
{
"events-webhook": {
"enabled": true,
"public_base_url": "https://your-domain.com",
"signatures": {
"github": {
"header": "X-Hub-Signature-256",
"algorithm": "sha256",
"secret": "your-github-secret"
},
"stripe": {
"header": "Stripe-Signature",
"algorithm": "stripe",
"secret": "whsec_xxx"
}
}
}
}

大多数 webhook provider 都期望快速返回响应:

- id: ack
type: http_response
config:
status: 200
next: async_process # Process async after responding

对于耗时较长的任务,先确认接收:

nodes:
- id: acknowledge
type: http_response
config:
status: 202
body:
message: "Processing"
next: process_async
- id: process_async
type: async
config:
flow: "process_webhook_data"
payload: "{{event.payload}}"
Terminal window
curl -X POST https://your-domain.com/events/webhook/demo/test \
-H "Content-Type: application/json" \
-d '{"message": "Hello from webhook"}'
Terminal window
ngrok http 8080
# Visit http://localhost:4040 to inspect requests
  1. 检查 URL 是否正确
  2. 确认公网 URL 可访问
  3. 检查防火墙/安全组
  4. 查看外部服务日志
  1. 确认密钥一致
  2. 检查 header 名称是否正确
  3. 确保校验时使用原始请求体