plazbot
Version:
Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.
49 lines (48 loc) • 1.68 kB
JavaScript
;
// ============================================================================
// Plazbot Workers - defineWebhook()
// Crea un endpoint HTTP que recibe requests de apps externas
// ============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineWebhook = defineWebhook;
/**
* Define un webhook que genera una URL publica para recibir HTTP requests.
*
* URL generada: `https://workers.plazbot.com/<workspaceId>/<name>`
*
* Ideal para recibir eventos de Stripe, Shopify, Calendly, etc.
*
* @example
* ```typescript
* import { defineWebhook } from 'plazbot/workers';
*
* export default defineWebhook({
* name: 'stripe-payments',
* reference: 'Recibe webhooks de pagos de Stripe',
* method: 'POST',
* async handler(request, plz) {
* const event = request.body as { type: string; data: { object: any } };
* if (event.type === 'checkout.session.completed') {
* const email = event.data.object.customer_email;
* const contacts = await plz.contacts.search({ email });
* if (contacts.length > 0) {
* await plz.contacts.addTag(contacts[0].id, 'cliente-pago');
* }
* }
* return { status: 200, body: { received: true } };
* },
* });
* ```
*/
function defineWebhook(config) {
if (!config.name)
throw new Error('defineWebhook: "name" es requerido');
if (!config.method)
throw new Error('defineWebhook: "method" es requerido');
if (!config.handler)
throw new Error('defineWebhook: "handler" es requerido');
return {
__type: 'webhook',
config,
};
}