API 与 Webhook
Webhook
在你的服务器上接收 STINGIM 事件:事件类型、载荷、签名校验与重试策略。
Webhooks 会在 STINGIM 项目中发生事件时通知你自己的系统(CRM、数据仓库、Zapier、工单工具等)。
配置端点
- 在控制台打开 设置 → Webhooks(需要集成(Integration)权限)。
- 添加端点:填写公网
https://URL、可选的描述,并选择要订阅的事件类型。 - 复制签名密钥(
whsec_…)。之后可以再次查看或轮换密钥——轮换立即生效,旧密钥随即失效,请马上部署新密钥。 - 点击发送测试,投递一条
webhook.test事件。
每个项目最多可添加 20 个端点。订阅变更约 15 秒内生效。
投递
| 项目 | 行为 |
|---|---|
| 请求 | POST,Content-Type: application/json,User-Agent: Stingim-Webhooks/1.0 |
| URL | 仅支持 443 端口的公网 https:// URL,URL 中不得包含凭据。私有地址、回环地址、链路本地地址、运营商级 NAT 地址和云元数据地址均会被拒绝,且不跟随重定向。 |
| 超时 | 每次尝试 10 秒(连接 5 秒,TLS 握手 5 秒) |
| 成功 | 任意 2xx。其余响应(包括 3xx 和 4xx)均视为失败并重试。 |
| 重试 | 约 24 小时内最多 8 次尝试:分别在上一次尝试后 1 分钟、5 分钟、30 分钟、2 小时、4 小时、8 小时、9 小时重试。 |
| 自动停用 | 连续 10 次投递在重试耗尽后仍失败时,端点会被停用,项目管理员会收到站内通知。可在 设置 → Webhooks 中重新启用。测试发送和手动重新投递不计入。 |
| 语义 | 至少一次(at-least-once)。 请按信封中的 id 去重。 |
| 顺序 | 不保证顺序。 请依据 created_at 和资源状态处理,而不是到达顺序。 |
| 日志 | 每次投递的状态、HTTP 状态码、错误和请求体保留 30 天。任意一次投递都可以手动重新投递。 |
请尽快响应,耗时操作放到异步处理;响应体会被忽略。
请求头
| 请求头 | 示例 |
|---|---|
X-Stingim-Event | message.created |
X-Stingim-Delivery | whd_4f0c…——同一次投递的所有自动重试使用相同的值;手动重新投递会生成新值 |
X-Stingim-Signature | t=1763600000,v1=5d41402abc4b2a76b9719d911017c592… |
信封
{
"id": "evt_9b1d0c3e8f2a5b6c7d8e9f0a1b2c3d4e",
"type": "message.created",
"api_version": "2026-11-20",
"created_at": "2026-11-20T08:30:12.345Z",
"tenant_id": "1790000000000000001",
"project_id": "1790000000000000002",
"data": { }
}ID 均为字符串。时间戳为 UTC(RFC 3339)。同一个 api_version 内只会新增字段——请忽略不认识的字段。
校验签名
X-Stingim-Signature: t=<unix seconds>,v1=<hex>,其中 v1 = hex(HMAC_SHA256(secret, t + "." + raw_request_body))。密钥是包含 whsec_ 前缀的完整字符串。
- 使用原始请求体字节,不要对解析后的 JSON 重新序列化。
- 使用常量时间比较。
- 拒绝与本机时钟相差超过 5 分钟的时间戳(防重放)。每次尝试的
t都是新生成的。
import crypto from 'node:crypto';
import express from 'express';
const SECRET = process.env.STINGIM_WEBHOOK_SECRET; // whsec_...
const app = express();
app.post('/webhooks/stingim', express.raw({ type: 'application/json' }), (req, res) => {
const header = req.get('X-Stingim-Signature') || '';
const parts = Object.fromEntries(header.split(',').map((p) => p.trim().split('=')));
const t = Number(parts.t);
if (!t || Math.abs(Date.now() / 1000 - t) > 300) return res.sendStatus(400);
const expected = crypto.createHmac('sha256', SECRET).update(`${t}.`).update(req.body).digest('hex');
const given = Buffer.from(parts.v1 || '', 'hex');
const want = Buffer.from(expected, 'hex');
if (given.length !== want.length || !crypto.timingSafeEqual(given, want)) return res.sendStatus(401);
const event = JSON.parse(req.body.toString('utf8'));
// 按 event.id 去重,然后放入队列异步处理
res.sendStatus(204);
});事件类型
| 事件 | 触发时机 |
|---|---|
conversation.created | 会话开始。 |
conversation.assigned | 会话被分配或转接给坐席。 |
conversation.closed | 会话被关闭。 |
message.created | 访客、坐席或机器人发送了消息(系统提示不触发)。 |
contact.created / contact.updated | 联系人被创建或其信息发生变化。 |
contact.tag_added | 联系人被添加了标签。 |
ticket.created / ticket.status_changed | 工单被创建或状态发生变化。 |
rating.submitted | 访客对会话进行了评价。 |
feedback.submitted | 提交了反馈问卷(NPS / CES / CSAT)。 |
lead.submitted | 提交了挂件的聊前调查或离线留言表单。 |
lead.created / lead.overdue | 通过网站邀请打开的线索收集表单已填写完成 / 未在时限内跟进。 |
email.bounced / email.complained | 营销邮件被退信,或收件人投诉。 |
ai_usage.threshold | AI 用量告警(配额、突增或熔断)。 |
webhook.test | 由发送测试发出;不可订阅。 |
conversation.created / conversation.closed
{
"conversation_id": "1861…", "contact_id": "1859…",
"channel": { "plugin_id": "1772…", "channel_id": "1772…", "channel_code": "web" },
"status": 3, "priority": "normal", "assigned_agent_id": "", "robot_id": "",
"created_at": "2026-11-20T08:30:00Z"
}status:1 待处理,2 机器人接待,3 待排队,4 排队中,5 会话中,6 已关闭。priority(normal 或 vip)在 created 时设定。conversation.closed 的 status 始终为 6,并额外包含 close_reason(关闭会话的坐席或自动关闭规则填写的自由文本,可能为空)和 closed_at。
conversation.assigned
{ "conversation_id": "1861…", "contact_id": "1859…", "agent_id": "1770…", "previous_agent_id": "", "reason": "assign" }reason:assign(自动或手动分配、坐席接起会话、机器人转人工)或 transfer(转接)。
message.created
{
"message_id": "1862…", "conversation_id": "1861…", "contact_id": "1859…",
"direction": "inbound", "sender_type": "visitor", "sender_id": "1859…", "sender_name": "",
"message_type": "image", "type_code": 2, "text": "",
"attachment": { "object_key": "tenant/…/photo.jpg", "name": "photo.jpg", "size": 48213, "mime_type": "image/jpeg" },
"channel": { "channel_code": "web" }, "sent_at": "2026-11-20T08:31:02Z"
}direction:inbound(访客)或 outbound(坐席或机器人)。sender_type:visitor、agent 或 bot。message_type:text、image、audio、video、file、email 或 other(对应 type_code 1、2、3、4、5、33)。附件只包含元数据——不会携带文件内容。
contact.created / contact.updated
{
"contact_id": "1859…", "name": "Ada Lovelace", "nickname": "", "email": "[email protected]",
"phone_country": "+44", "phone": "7700900000", "country": "GB", "city": "London", "language": "en",
"channel": { "plugin_id": "1772…", "channel_id": "1772…", "channel_code": "web" },
"changed_fields": ["email", "phone"],
"created_at": "2026-11-20T08:29:58Z", "updated_at": "2026-11-20T09:02:11Z"
}changed_fields 仅出现在 contact.updated 中。contact.created 对每个联系人只触发一次。匿名网站访客仅打开挂件不会触发该事件:访客首次发送消息或留下联系方式时才会触发,因此挂件访客可能在相近的时间同时出现在 contact.updated 或 lead.submitted 中。
contact.tag_added
{ "contact_id": "1859…", "tags": [ { "id": "1801…", "name": "VIP" } ] }ticket.created / ticket.status_changed
{
"ticket_id": "6c1e…", "title": "Refund request", "status": "closed", "previous_status": "in_progress",
"priority": "high", "template_id": "5a2b…", "contact_id": "1859…", "conversation_id": "1861…",
"assignee_id": "1770…", "creator_id": "1770…", "cause": "workflow"
}status:in_progress(处理中)或 closed(已关闭)。previous_status 和 cause(workflow、force_close 或 approval_timeout)仅出现在 status_changed 中。
rating.submitted
{
"rating_id": "1863…", "conversation_id": "1861…", "contact_id": "1859…", "agent_id": "1770…", "robot_id": "",
"score": 5, "max_score": 5, "label": "Very satisfied", "tags": ["fast"], "comment": "Thanks!",
"submitted_at": "2026-11-20T08:53:00Z"
}feedback.submitted
客户反馈问卷的提交结果,来源可以是挂件的会话结束表单(source: widget,同时会触发 rating.submitted),也可以是通过邮件、WhatsApp、流程或群发消息发送的免登录反馈链接(source: link)。在允许的时间窗口内修改问卷会再次触发该事件,feedback_id 不变,revision 递增。
{
"feedback_id": "1871…", "source": "link", "subject_kind": "ticket",
"conversation_id": "1861…", "ticket_id": "1868…", "contact_id": "1859…",
"channel_code": "whatsapp", "agent_id": "1770…", "handled_by": "human",
"nps_score": 9, "nps_category": "promoter", "ces_score": 6, "csat_score": 5, "csat_max": 5,
"polarity": "positive", "reason_codes": ["fast_reply"], "comment": "Great help",
"language": "en", "revision": 1, "submitted_at": "2026-11-20T09:10:00Z"
}subject_kind:conversation、ticket 或 contact(群发问卷,同时携带 campaign_id)。handled_by:ai、human 或 none。nps_category:promoter、passive 或 detractor。polarity:positive、negative 或 neutral。未提问的题目对应的分数字段会被省略。
lead.submitted
{
"lead_id": "1864…", "contact_id": "1859…", "form_type": "offline_message",
"name": "Ada", "email": "[email protected]", "phone": "", "remark": "",
"message": "Please call me back tomorrow",
"custom_fields": [ { "field_id": "1805…", "value": "Enterprise" } ],
"channel": { "plugin_id": "1772…", "channel_id": "1772…", "channel_code": "web" }
}form_type:pre_chat(聊前调查)或 offline_message(离线留言)。
lead.created / lead.overdue
访客填写完成了从网站邀请打开的线索收集表单(提交最后一步时触发一次)。对于超过跟进截止时间仍处于 new 状态的线索,lead.overdue 最多触发一次,数据相同并额外包含 overdue_at。
{
"lead_id": "1870…", "contact_id": "1859…", "form_id": "1866…", "invite_rule_id": "1865…",
"name": "Ada", "email": "[email protected]", "phone": "+6591234567", "whatsapp": "+6591234567",
"company": "Acme", "custom_fields": [ { "field_id": "1805…", "value": "50-200" } ],
"consent": true, "page_url": "https://shop.example.com/pricing",
"score": 65, "grade": "hot", "status": "new", "owner_id": "1770…", "team_id": "",
"follow_up_due_at": "2026-11-21T08:53:00Z", "created_at": "2026-11-20T08:53:00Z",
"channel": { "plugin_id": "1772…", "channel_id": "1772…", "channel_code": "web" }
}grade:hot、warm 或 cold,由项目的线索评分规则决定。
email.bounced / email.complained
{
"address": "[email protected]", "kind": "hard_bounce", "source": "smtp",
"status_code": "550", "diagnostic": "5.1.1 user unknown", "broadcast_id": "1880…",
"contact_id": "1859…", "suppressed": true, "suppression_reason": "hard_bounce"
}kind:hard_bounce、soft_bounce 或 complaint。source:smtp、dsn 或 arf。suppression_reason:hard_bounce、soft_bounce_repeated、complaint 或 manual。
ai_usage.threshold
{ "kind": "quota", "metric": "ai_answers", "threshold_percent": 80, "used": 812, "limit": 1000, "period": "2026-11" }
{ "kind": "spike", "metric": "points", "multiplier": 3, "used": 1240, "limit": 210.5, "period": "2026-11-19" }
{ "kind": "breaker", "metric": "ai_breaker", "used": 61, "limit": 60, "period": "2026-11-20T03:15" }quota——AI 配额达到阈值(默认 50% / 80% / 100%,可在 AI 用量页面配置)。metric为ai_answers(每月额度)、ai_tokens(合作伙伴设定的 token 上限)、ai_answers_daily(每日上限;仅在 100% 时触发)或ai_answers_trial。spike——当天(UTC)消耗的 AI 点数至少达到前 7 天平均值的multiplier倍;limit即该平均值。breaker——一分钟内 AI 调用异常增多,AI 被暂停几分钟;used为该分钟内的调用次数,limit为每分钟上限。
每个阈值在每个 period 内最多触发一次。
webhook.test
{ "endpoint_id": "whe_…", "message": "This is a test event from STINGIM." }自动化流程中的「HTTPS webhook」步骤
自动化流程也可以通过自定义工具(AI 与知识 → 自定义工具)把调用你的 HTTPS 端点作为一个步骤。这类请求使用另一种签名:X-StingIM-Timestamp、X-StingIM-Delivery-ID(同时作为 Idempotency-Key 发送)以及 X-StingIM-Signature: sha256=<hex(HMAC-SHA256(tool secret, timestamp + "." + delivery_id + "." + raw body))>。请求体为 { "context": { tenant_id, project_id, process_id, thread_id, node_id, contact_id, conversation_id }, "input": { … } }。请在 15 秒内返回 2xx;其他 4xx 响应(408 和 429 除外)不会重试,其余情况最多重试 5 次。
发现错误?请发邮件至 [email protected]
