npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

whatsapp-bot-builder

v1.0.6

Published

Middleware to build Whatsapp Bot

Downloads

3

Readme

WhatsApp Bot Builder

Overview

The WhatsApp Bot Builder is an Express/Fastify middleware designed to simplify the creation of WhatsApp chatbots using WhatsApp Business Cloud API. It provides an easy-to-use builder class, whatsappBotBuilder, that allows you to set up and manage a WhatsApp chatbot quickly.

Installation

You can install the WhatsApp Bot Builder package using npm:

npm install whatsapp-bot-builder

Usage

const { whatsappBotBuilder } = require("whatsapp-bot-builder");
const whatsappBot = new whatsappBotBuilder({
  webhook_verify_token: ["Your Verification Token"], //used to authenticate whatsapp webhook
  meta_version: ["META API Version"], //Defaults to v16.0
  whatsapp_buisness_id: ["WhatsApp Business Account ID"],
  buisness_phone_number: ["Phone number ID"],
  meta_access_token: ["Meta Access Token"],
});

<!-- Implement Bot listeners -->

const app = express();
app.use(express.json());
app.use("/webhook/events", whatsappBot.init()); //Use it as an express Middleware, it will handle Whatsapp Webhook Events

Sending a WhatsApp Text Message

You can use the sendWhatsappMessage function to send a text message via WhatsApp. Here's an example of how to use it:

// Send a WhatsApp text message
whatsappBot.sendWhatsappMessage("+91-0000000000", {
  preview_url: false,
  body: "How can I help you today?",
});

Sending a WhatsApp Template Message

You can use the sendWhatsappTemplate function to send a template message via WhatsApp. Here's an example of how to use it:

// Send a WhatsApp Template message
whatsappBot.sendWhatsappDocument(
  "+91-0000000000",
  template_name,
  language_code,
  template_components
);

Sending a WhatsApp Document

You can use the sendWhatsappDocument function to send a document via WhatsApp. Here's an example of how to use it:

// Send a WhatsApp document
whatsappBot.sendWhatsappDocument("+91-0000000000", {
  id: "1012124500309246", // ID generated using uploadMedia
  filename: "sample",
}); // if file is webp then it's sent as sticker and hence make isSticker true

Upload Media

You can use the uploadMedia function to upload media to Whatsapp Media . Here's an example of how to use it:

// Send a WhatsApp Template message
let response = await whatsappBot.uploadMedia(filePath); // if file is webp then make sure choosing isWebpAnimated based on it's animation, default it static
console.log(response);

Fetch Media Url Data

You can use the fetchUserMediaUrl function to get media url from Whatsapp Media ID. Here's an example of how to use it:

// Send a WhatsApp Template message
let response = await whatsappBot.fetchUserMediaUrl(media_id);
console.log(response);

Listening Text Message

You can use the text function listen for particular message from a user. Here's an example of how to use it:

whatsappBot.text("Hello", ({ sender, data }) => {
  console.log(sender);
  console.log(data);
});

Listening Button Response

You can use the button function listen for particular message from a user. Here's an example of how to use it:

whatsappBot.button(button_payload, ({ sender, data }) => {
  console.log(sender);
  console.log(data);
});

Listening for a Document/Image

Only one listener for document/image is permissible, you can use image or document function for the same, Example for the same

whatsappBot.image(({ sender, data }) => {
  console.log(sender);
  console.log(data);
});

Template Management

This section provides the usage of Template management

Usage

const { ManageWhatsapp } = require("whatsapp-bot-builder");
const whatsappManager = new ManageWhatsapp({
  webhook_verify_token: ["Your Verification Token"], //used to authenticate whatsapp webhook
  meta_version: ["META API Version"], //Defaults to v16.0
  whatsapp_buisness_id: ["WhatsApp Business Account ID"],
  buisness_phone_number: ["Phone number ID"],
  meta_access_token: ["Meta Access Token"],
});

Create Template

// Creating a new WhatsApp template
const newTemplate = await whatsappManager.createTemplate({
  name: "new_template",
  category: "MARKETING",
  language: "en_US",
  components: [
    {
      type: "BODY",
      text: "This is a sample",
    },
  ],
});
console.log("Created Template:", newTemplate);

Fetching Templates

// Fetching WhatsApp templates
const fetchedTemplates = await whatsappManager.fetchTemplates({
  category: "MARKETING",
  status: "APPROVED",
});
console.log("Fetched Templates:", fetchedTemplates);

Edit Template

// Editing an existing WhatsApp template
const editedTemplate = await whatsappManager.editTemplate("12345678", {
  category: "Updated Category",
  components: [{ type: "HEADER", text: "Updated Header" }],
});
console.log("Edited Template:", editedTemplate);

Delete Template

// Deleting an existing WhatsApp template
const deletionResult = await whatsappManager.deleteTemplate(
  "12345678",
  "Template Name"
);
console.log("Deletion Result:", deletionResult);
console.log("Edited Template:", editedTemplate);