UNPKG

whatsapp-business

Version:

Node.js connector for the WhatsApp Business APIs with TypeScript support, integration tests and more.

70 lines (69 loc) 3.03 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebhookClient = void 0; var express_1 = __importStar(require("express")); var helpers_1 = require("./helpers"); /** * Use this client if you want to easily initialize webhook connections. * Before anything, make sure your server has an https connection. * For more info, check the docs: https://developers.facebook.com/docs/whatsapp/cloud-api/guides/set-up-webhooks */ var WebhookClient = /** @class */ (function () { function WebhookClient(_a) { var path = _a.path, port = _a.port, expressApp = _a.expressApp, token = _a.token; this.path = path || "/webhook"; this.port = port || 8080; this.router = (0, express_1.Router)(); this.token = token; if (expressApp) this.expressApp = expressApp; } /** * Initializes the webhook listener server with the provided events. */ WebhookClient.prototype.initWebhook = function (events) { //If the express app param is not passed, then create a general application if (!this.expressApp) { this.expressApp = { app: (0, express_1.default)(), shouldStartListening: true, }; this.expressApp.app.use((0, express_1.json)()); this.expressApp.app.use((0, express_1.urlencoded)({ extended: false })); } //Webhook subscription this.router.get("/", (0, helpers_1.getWebhookController)(this.token)); //Listen to the webhook events this.router.post("/", (0, helpers_1.postWebhookController)(events || {})); //Route requests this.expressApp.app.use(this.path, this.router); //Start listening if (this.expressApp.shouldStartListening) this.expressApp.app.listen(this.port, events === null || events === void 0 ? void 0 : events.onStartListening); }; return WebhookClient; }()); exports.WebhookClient = WebhookClient;