Skip to content

Verificação Webhook

Visão geral

Quando um usuário responde a uma mensagem, o BotBell envia a resposta para seu reply_url via HTTP POST. Verifique a assinatura para garantir a autenticidade.

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

Como funciona

  1. Extraia X-Webhook-Timestamp e X-Webhook-Signature dos headers
  2. Verifique se o timestamp está dentro de 5 minutos (previne ataques de replay)
  3. Calcule HMAC-SHA256 de {timestamp}.{body} com seu webhook secret
  4. Compare com a assinatura (comparação em tempo 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);