import express from 'express';
import { WhatsrbCloudClient, ActionRegistry, EventRegistry, AgentRun, WebhookSignature } from '@whatsrb/cloud';
const client = new WhatsrbCloudClient({ apiKey: process.env.WHATSRB_API_KEY });
// Action registry
const actions = new ActionRegistry();
actions
.register('create_task', (payload) => createTask(payload))
.register('create_ticket', (payload) => createTicket(payload));
// Event registry
const events = new EventRegistry();
events.on('agent_run.completed', (data) => {
const run = new AgentRun(data);
run.dispatch(actions);
});
// Webhook endpoint
const app = express();
app.post('/webhooks/whatsrb', express.raw({ type: 'application/json' }), (req, res) => {
const isValid = WebhookSignature.verify({
payload: req.body.toString(),
secret: process.env.WHATSRB_WEBHOOK_SECRET!,
signature: req.headers['x-whatsrb-signature'] as string,
timestamp: req.headers['x-webhook-timestamp'] as string,
});
if (!isValid) return res.status(401).end();
events.dispatch(JSON.parse(req.body.toString()));
res.status(200).end();
});