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.
You can install the WhatsApp Bot Builder package using npm:
npm install whatsapp-bot-builder
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
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?",
});
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.sendWhatsappTemplate(
"+91-0000000000",
template_name,
language_code,
template_components
);
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);
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);
});
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);
});
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);
});
This section provides the usage of Template management
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"],
});
// 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 WhatsApp templates
const fetchedTemplates = await whatsappManager.fetchTemplates({
category: 'MARKETING',
status: 'APPROVED'
});
console.log('Fetched Templates:', fetchedTemplates);
// 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);
// Deleting an existing WhatsApp template
const deletionResult = await whatsappManager.deleteTemplate('12345678', 'Template Name');
console.log('Deletion Result:', deletionResult);
console.log('Edited Template:', editedTemplate);