Skip to content

التحقق من Webhook

نظرة عامة

عندما يرد المستخدم على رسالة، يرسل BotBell الرد إلى reply_url عبر HTTP POST. تحقق من التوقيع لضمان صحة الطلب.

ترويسة التوقيعX-Webhook-Signature: sha256=...
ترويسة الطابع الزمنيX-Webhook-Timestamp: 1234567890

كيف يعمل

  1. استخرج X-Webhook-Timestamp و X-Webhook-Signature من الترويسات
  2. تحقق أن الطابع الزمني في نطاق 5 دقائق (يمنع هجمات الإعادة)
  3. احسب HMAC-SHA256 لـ {timestamp}.{body} باستخدام webhook secret
  4. قارن مع التوقيع (مقارنة بوقت ثابت)
signature = HMAC-SHA256(
  key: webhook_secret,
  message: "{timestamp}.{request_body}"
)

Python

from botbell import verify_webhook, WebhookVerificationError

try:
    verify_webhook(
        body=request.body,
        signature_header=request.headers["X-Webhook-Signature"],
        timestamp_header=request.headers["X-Webhook-Timestamp"],
        secret="your_webhook_secret",
    )
except WebhookVerificationError:
    return {"error": "Invalid signature"}, 401

# Signature valid — process the reply
data = json.loads(request.body)

JavaScript

import { verifyWebhook, WebhookVerificationError } from "@botbell/sdk";

try {
  verifyWebhook({
    body: req.body,
    signature: req.headers["x-webhook-signature"],
    timestamp: req.headers["x-webhook-timestamp"],
    secret: "your_webhook_secret",
  });
} catch (e) {
  if (e instanceof WebhookVerificationError) {
    return res.status(401).json({ error: e.message });
  }
  throw e;
}

// Signature valid — process the reply
const data = JSON.parse(req.body);