Skip to content

Verificación Webhook

Descripción general

Cuando un usuario responde a un mensaje, BotBell envía la respuesta a tu reply_url via HTTP POST. Verifica la firma para asegurar la autenticidad.

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

Cómo funciona

  1. Extrae X-Webhook-Timestamp y X-Webhook-Signature de los headers
  2. Verifica que el timestamp esté dentro de 5 minutos (previene ataques de replay)
  3. Calcula HMAC-SHA256 de {timestamp}.{body} con tu webhook secret
  4. Compara con la firma (comparación en tiempo constante)
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);