Simple WhatsApp ChatBot in Node.js for Beginners
WhatsApp API for developers
Posted on October 22, 2024
Are you looking to build your first bot but don’t know where to start? Then you're in the right place! I'm excited to announce the release of a new, simple WhatsApp bot built with Node.js that’s perfect for beginners.
In this tutorial, we are excited to announce the release of a simple WhatsApp bot built with Node.js—an ideal starting point for beginner developers. If you're just venturing into bot development, this bot will help you learn how to handle commands, respond to messages, and even share images, all while keeping the complexity at bay. You’ll focus on the core functionality without being overwhelmed by unnecessary features.
Why Build a WhatsApp Bot?
WhatsApp is one of the most widely used messaging platforms in the world, and many businesses use it for customer support, notifications, and marketing. A bot can automate these tasks, saving time and effort, and enhancing user experience.
However, for someone new to development, it can be challenging to figure out where to begin with building a WhatsApp bot. That's why I've created this bot with simplicity in mind, offering only core functionality like responding to text commands and sending images.
Let’s dive into the details of this project and explore how to set up your very own WhatsApp bot!
What’s Inside This WhatsApp Bot
This bot is designed to respond to a few basic commands. When a user sends a message to the connected WhatsApp channel, the bot can:
- Reply with text messages.
- Send images based on commands.
- Understand and talk about a team he doesn't know.
These functionalities give you a hands-on way to understand how to process messages and how to structure your own bot logic.
Prerequisites
Before we get started, there are a few things you’ll need:
- Node.js – Make sure you have Node.js installed on your system. If you haven't installed it yet, you can download it from Node.js official website.
- API Token from Whapi.Cloud – Whapi.Cloud provides an easy-to-use API for WhatsApp integration. You’ll need to sign up for an account and grab a token. We have described in more detail how to connect a WhatsApp account and get a token in our knowledge base here.
- A Webhook – The bot relies on a webhook to receive incoming messages. If you don't know how to set it up, we'll tell you more about it below.
Project Structure Overview
Here’s a breakdown of the key files in this project:
index.js – This file verifies the WhatsApp channel, checks the API token, and sets up the webhook programmatically through the API. There’s no need to configure the webhook manually in the interface, as it's handled here automatically.
/modules/channel.ts – Contains essential functions such as:
checkHealth()
– Verifies the channel’s functionality.
sendMessage()
– Sends text messages.
setWebHook()
– Sets the webhook for incoming messages.
getWebHooks()
– Retrieves existing webhooks.
sendLocalJPG()
– Converts images from the /images/ folder into Base64 and sends them as WhatsApp messages.
- /endpoints/hook.ts – This is where the core bot logic lives:
- It listens for incoming messages and ignores outgoing messages.
- It fetches the sender's number and message text.
- It filters out non-text messages.
- A
switch
statement processes the command and sends back the appropriate response, either a text message or an image.
Step-by-Step Setup
Follow these simple steps to get your WhatsApp bot running:
1. Clone the Project
Start by cloning the repository:
git clone https://github.com/Whapi-Cloud/whatsapp-simple-node-js-bot
cd whatsapp-bot
2. Update the Config
Next, open the config.json
file and add your API token and webhook URL:
{
"webhookUrl": "YOUR-WEBHOOK-URL",
"token": "YOUR-TOKEN",
"port": "8080"
}
Where to Find the Webhook URL?
To set up a webhook URL, you'll need to configure a server endpoint that can handle incoming HTTP requests from our API. This allows your application to receive real-time data, such as message updates, directly from WhatsApp.
You can set up a webhook on a hosting platform, in the cloud services, or locally (using Ngrok or similar programs).
The links provided will give you instructions on how to use this option. In this article's example, we will use Ngrok.
3. Install Dependencies
Install the required dependencies by running:
npm install
4. Run the Bot
To start your bot, run the following command:
npm run start
And that’s it! Your bot is now up and running. It will respond to incoming messages based on the logic defined in /endpoints/hook.ts
.
Detailed Features
Here’s a closer look at what the bot does and how it works:
Channel Health Check
Before anything else, the bot ensures that the WhatsApp channel is functioning correctly by using thecheckHealth()
function in /modules/channel.ts. This prevents problems from occurring later and helps you ensure that the session is working correctly and your channel is running.Sending Messages
The bot uses thesendMessage()
function to send replies to users. The bot script itself contains logic for processing various types of responses (skips outgoing messages; processes only incoming text messages; sends messages in response to the numbers that were written to it).Webhook Setup
Instead of manually setting up the webhook in the Whapi.Cloud interface, thesetWebHook()
function handles this automatically via the API, streamlining the setup process.Image Handling
ThesendLocalJPG()
function allows the bot to send images in response to certain commands. It converts images from the /images/ folder into Base64 format and sends them via WhatsApp.Processing Incoming Messages
The core bot logic in /endpoints/hook.ts listens for incoming messages, processes the text, and replies accordingly. The bot handles various commands using theswitch
statement, which makes it easy to expand the bot's capabilities in the future.
Ready to learn more?
If you want to dive deeper into the API or expand the bot's capabilities, check out our more comprehensive chatbot on GitHub.
Conclusion
This simple WhatsApp bot built with Node.js is a great way to start learning how to develop chatbots. With the functionality to send both text and images, and clear, easy-to-understand code, this project is perfect for beginner developers.
Start your journey with WhatsApp bot development today by trying out this bot and customizing it to your needs. Be sure to also explore Whapi.Cloud’s API for additional functionality and support for more complex features.
Try it now, and happy coding!
Posted on October 22, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.