WhatsApp Cloud API Webhook for message subscriptions.
Follow these steps to set up the WhatsApp Cloud API Webhook for message subscriptions.
-
Install dependency
npm install @refactoring/whatsapp-webhook
-
Write the following code:
import { Logger, Webhook, ProcessedMessage } from "@refactoring/whatsapp-webhook"; const observer = (message: any) => { console.log(JSON.stringify(message)); }; const port = +process.env['PORT'] || 3000; const verificationToken = process.env['VERIFICATION_TOKEN'] || 'qwertyuiop1234567890'; (async () => { const webhook = new Webhook({ endpoint: '/webhook', port, verificationToken, observer, }); webhook.run().then(Logger.log).catch(Logger.error); })();
-
Run the code
node webhook.js
-
Output
The server is now listening on the provided port and is ready to verify the WhatsApp Cloud API token and receive messages.
Follow these steps to debug the WhatsApp Cloud API Webhook:
-
Install Cloudflare's CLI
(cloudflared)
:brew install cloudflared
-
Run an instance with a Cloudflare tunnel:
cloudflared tunnel --url localhost:3000
-
Send a message to the bot and wait for the webhook subscription.
This package has been inspired by the official WhatsApp Cloud API for Node.js. It includes several improvements and enhancements.
Made with ❤️ by Refactoring, SRL