SuperAPI AI 工具集

twitch-webhooks

编程与开发

查看项目

更新于 10-15 22:14

技能介绍

用途:帮助开发者接收并验证 Twitch EventSub 的 Webhook 事件。Twitch 不遵循 Standard Webhooks 规范,使用自有签名方案和三种消息类型协议,且 Webhook 传输没有官方 SDK,需要手动校验。

主要能力:

  1. 签名验证:Twitch 对 Twitch-Eventsub-Message-Id、Twitch-Eventsub-Message-Timestamp 与原始请求体按顺序拼接后做 HMAC-SHA256,摘要以 sha256=<hex> 放在 Twitch-Eventsub-Message-Signature 中。资料给出 Node 与 Python 的校验函数示例,需使用原始 body 并做时序安全比较。
  2. 消息类型分支:读取 Twitch-Eventsub-Message-Type。webhook_callback_verification 需返回 HTTP 200,body 为原始 challenge 字符串,Content-Type 为 text/plain,不要 JSON 包装;notification 处理 payload.event 后返回 2XX;revocation 返回 2XX 并记录 payload.subscription.status。撤销原因包括 user_removed、authorization_revoked、notification_failures_exceeded、version_removed。
  3. 常见事件:stream.online、stream.offline、channel.follow(v2,需 moderator_user_id)、channel.update(v2)、channel.subscribe、channel.subscription.gift、channel.cheer、channel.raid、channel.ban、channel.channel_points_custom_reward_redemption.add 等。

使用方式:通过 POST /helix/eventsub/subscriptions 创建订阅时设置密钥(10-100 个 ASCII 字符),存入环境变量 TWITCH_WEBHOOK_SECRET。回调必须为 HTTPS 且端口 443。资料提供 examples/express/、examples/nextjs/、examples/fastapi/ 完整处理器示例,以及 references/overview.md、setup.md、verification.md 参考文档。本地开发可用 npx hookdeck-cli listen 3000 twitch --path /webhooks/twitch 启动隧道。

限制与注意事项:必须校验原始 body,重新序列化 JSON 会改变字节导致 HMAC 失败;订阅密钥需用 app access token 创建,用户令牌会被拒绝;需在数秒内响应,否则重复失败后 Twitch 会撤销订阅;投递为至少一次,应按 Twitch-Eventsub-Message-Id 去重并拒绝超过 10 分钟的时间戳。建议配合 webhook-handler-patterns 技能处理处理顺序、幂等、错误与重试。