Skip to content

Vérification Webhook

Aperçu

Quand un utilisateur répond à un message, BotBell envoie la réponse à votre reply_url via HTTP POST. Vérifiez la signature pour garantir l'authenticité.

Header de signatureX-Webhook-Signature: sha256=...
Header de timestampX-Webhook-Timestamp: 1234567890

Fonctionnement

  1. Extraire X-Webhook-Timestamp et X-Webhook-Signature des headers
  2. Vérifier que le timestamp est dans les 5 minutes (prévient les attaques par rejeu)
  3. Calculer le HMAC-SHA256 de {timestamp}.{body} avec votre webhook secret
  4. Comparer avec la signature (comparaison en temps constant)
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);