1.0.5 • Published 1 year ago

@refactoring/whatsapp-webhook v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

WhatsApp Cloud API Webhook

WhatsApp Cloud API Webhook for message subscriptions.

NPM Package Tests Linting and Formatting

Getting started

  1. Install dependecy

    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 listening on the provided port and ready to verify the WhatsApp Cloud API token and receive messages.

Debuggin

  1. Install cloudflared CLI:

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

    cloudflared tunnel --url localhost:3000   
  3. Write 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. A few improvements and facilities have been added.

Made with ❤️ by Refactoring, SRL