Skip to content

refactoring-do/whatsapp-webhook

Repository files navigation

WhatsApp Cloud API Webhook

@refactoring-do/whatsapp-webhook

WhatsApp Cloud API Webhook for message subscriptions.

NPM Package Tests Linting and Formatting

Getting Started

Follow these steps to set up the WhatsApp Cloud API Webhook for message subscriptions.

  1. Install dependency

    npm install @refactoring/whatsapp-webhook
  2. 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);
    })();
  3. Run the code

    node webhook.js
  4. Output

    The server is now listening on the provided port and is ready to verify the WhatsApp Cloud API token and receive messages.

Debugging

Follow these steps to debug the WhatsApp Cloud API Webhook:

  1. Install Cloudflare's CLI (cloudflared):

    brew install cloudflared
  2. Run an instance with a Cloudflare tunnel:

    cloudflared tunnel --url localhost:3000   
  3. Send a message to the bot and wait for the webhook subscription.

Credits

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