UNPKG

@josue.0/whatsapp-cloud-api

Version:

Node.js library for creating bots and sending/receiving messages using the Whatsapp Cloud API

126 lines 6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startExpressServer = void 0; const express_1 = __importDefault(require("express")); const pubsub_js_1 = __importDefault(require("pubsub-js")); const pubSub_1 = require("./utils/pubSub"); const startExpressServer = (options) => new Promise((resolve) => { const app = (options === null || options === void 0 ? void 0 : options.app) || (0, express_1.default)(); app.use(express_1.default.json()); if (options === null || options === void 0 ? void 0 : options.useMiddleware) { options.useMiddleware(app); } const webhookPath = (options === null || options === void 0 ? void 0 : options.webhookPath) || '/webhook/whatsapp'; if (options === null || options === void 0 ? void 0 : options.webhookVerifyToken) { app.get(webhookPath, (req, res) => { if (!req.query) { res.sendStatus(403); return; } const mode = req.query['hub.mode']; const verifyToken = req.query['hub.verify_token']; const challenge = req.query['hub.challenge']; if (!mode || !verifyToken || !challenge) { res.sendStatus(403); return; } if (mode === 'subscribe' && verifyToken === options.webhookVerifyToken) { console.log('✔️ Webhook verified'); res.setHeader('content-type', 'text/plain'); res.send(challenge); return; } res.sendStatus(403); }); } app.post(webhookPath, async (req, res) => { var _a; console.log(JSON.stringify(req.body)); if (!req.body.object || !req.body.entry) { res.sendStatus(400); return; } ((_a = req.body.entry) !== null && _a !== void 0 ? _a : []).forEach((entry) => { var _a; ((_a = entry.changes) !== null && _a !== void 0 ? _a : []).forEach((change) => { var _a, _b, _c, _d; const { value } = change; if (value != null) { const toPhoneNumberId = (_b = (_a = value.metadata) === null || _a === void 0 ? void 0 : _a.phone_number_id) !== null && _b !== void 0 ? _b : ''; ((_c = value.messages) !== null && _c !== void 0 ? _c : []).forEach((message) => { var _a, _b, _c, _d, _e; const { from, id, timestamp, type, ...rest } = message; let event; let data; switch (type) { case 'text': event = pubSub_1.PubSubEvents.text; data = { text: (_a = rest.text) === null || _a === void 0 ? void 0 : _a.body }; break; case 'image': case 'document': case 'audio': case 'video': case 'sticker': case 'location': case 'button': case 'contacts': event = pubSub_1.PubSubEvents[type]; data = rest[type]; break; case 'interactive': event = rest.interactive.type; data = { ...(rest.interactive.list_reply || rest.interactive.button_reply), }; break; default: break; } if (rest.context) { data = { ...data, context: rest.context, }; } const name = (_e = (_d = (_c = (_b = value.contacts) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.profile) === null || _d === void 0 ? void 0 : _d.name) !== null && _e !== void 0 ? _e : undefined; if (event && data) { const payload = { from, name, id, timestamp, type: event, data, to_phone_number_id: toPhoneNumberId, }; ['message', event].forEach((e) => pubsub_js_1.default.publish(e, payload)); } }); ((_d = value.statuses) !== null && _d !== void 0 ? _d : []).forEach((_status) => { const payload = { ..._status, to_phone_number_id: toPhoneNumberId, }; ['status'].forEach((e) => pubsub_js_1.default.publish(e, payload)); }); } }); }); res.sendStatus(200); }); if (options === null || options === void 0 ? void 0 : options.app) { resolve({ app }); return; } const port = (options === null || options === void 0 ? void 0 : options.port) || 3000; const server = app.listen(port, () => { console.log(`🚀 Server running on port ${port}...`); resolve({ server, app }); }); }); exports.startExpressServer = startExpressServer; //# sourceMappingURL=startExpressServer.js.map