Help Center / Tutorials

How to create a chat bot using Node.js

A complete WhatsApp chatbot demo tutorial ready to use in minutes using the OmniChat API. 🤖 🤩

Get a chatbot running in minutes on your computer or server and easily adapt it to cover you own use cases.

The chatbot is able to create, edit and remove reminders, assign chats to agents when requested and reply with different sample messages as requested by users.

If you a are developer, jump directly to the code here

Features

This tutorial provides a complete chatbot implementation in Node.js that does:

  • Provides a fully features chatbot in your WhatsApp number connected to OmniChat
  • Replies automatically to inbound messages from users
  • Allows any user to create, list and delete message reminders
  • Allows any user to ask talking with a person, in which case the case will be assigned to an againt
  • Sends a sample messages to the user based on their input (image, video, audio, document, location...)
  • Configure the chatbot behavior easily using the configuration file

Chatbot supported tasks

  • 1️⃣ Create a reminder
  • 2️⃣ List reminders
  • 3️⃣ Delete reminder
  • 4️⃣ Chat with a person

Additionally, the chatbot can send you the following sample messages:

  • Text
  • Image
  • Video
  • Audio
  • PDF Document
  • Excel document
  • File
  • Buttons
  • List
  • Location
  • Contact card
  • Quote message
  • Emojis 🥳
  • Text formatting
  • Link preview
  • Reaction

How it works

  1. The program will validate you have access to OmniChat API and you have a least one WhatsApp number connected.
  2. Creates a tunnel using Ngrok to be able to receive Webhook events on your computer (or you can use a dedicated webhook URL instead if you run the bot in a cloud server).
  3. Registers the webhook endoint automatically.
  4. Start listening for inbound messages received in your WhatsApp number.
  5. You can start playing with the bot by sending messages to the connected WhatsApp number.

Chatbot behavior

The chatbot will always reply to messages based on the following conditions:

  • The chat belong to a user (group chats are always ignored)
  • The chat is not assigned to any agent inside OmniChat
  • The chat has not any of the blacklisted labels (see config.js)
  • The chat user number has not been blacklisted (see config.js)
  • The chat or contact has not been archived or blocked

Requirements

Project structure

\
 |- bot.js -> the bot source code in a single file
 |- config.js -> configuration file to customize the bot behavior and rules
 |- package.json -> node.js package manifest required to install dependencies
 |- node_modules -> where the project dependencies will be installed, fully managed by npm

You can download all project sources in this section > Download icon in the box top-right corner.

Usage

Open your favorite terminal and change directory to project folder where package.json is located:

cd ~Downloads/chatbot-demo/

From that folder, install dependencies with npm:

npm install

With your preferred code editor, edit config.js and enter your OmniChat API key (sign up here for free) and enter the API key at line 12th:

// Required. Specify the OmniChat API key to be used
// You can obtain it here: https://chat.omnidigital.ae/apikeys
apiKey: env.API_KEY || 'ENTER API KEY HERE',

If you want to run the program on your computer, you need to create a tunnel with Ngrok. - Sign up for a Ngrok free account and obtain your auth token as explained here. Then enter it in the line 29th:

// Ngrok tunnel authentication token.
// Required if webhook URL is not provided.
// sign up for free and get one: https://ngrok.com/signup
// Learn how to obtain the auth token: https://ngrok.com/docs/agent/#authtokens
ngrokToken: env.NGROK_TOKEN,

Run the bot program:

node bot

Run the bot program on a custom port:

PORT=80 node bot

Run the bot program for a specific OmniChat connected device:

DEVICE=WHATSAPP_DEVICE_ID node bot

Run the bot program in production mode:

NODE_ENV=production node bot

Run the bot with an existing webhook server without the Ngrok tunnel:

WEBHOOK_URL=https://bot.company.com:8080/webhook node bot
Note: https://bot.company.com:8080 must point to the bot program itself running in your server and it must be accessible from Internet using HTTPS for secure connection.

Source files

Download all project files clicking on the Download icon in the box top-right corner below.

Questions

Can I customize the chatbot response and behavior?

For sure! The code is available for free and you can adapt it as much as you need.

You just need to have some JavaScript/Node.js knowledge, and you can always ask ChatGPT to help you write the code you need.

Do I have to use Ngrok?

No, you don't. Ngrok is only used for development/testing purposes when running the program from your local computer. If you run the program in a cloud server, most likely you won't need Ngrok if your server can be reachable via Internet using a public domain (e.g: bot.company.com) or a public IP.

In that case, you simply need to provide your server full URL ended with /webhook like this when running the bot program:

WEBHOOK_URL=https://bot.company.com:8080/webhook node bot

Note: https://bot.company.com:8080 must point to the bot program itself running in your server and it must be network reachable using HTTPS for secure connection.

What happens if the program fails?

Please check the error in the terminal and make sure your are running the program with enough permissions to start it in port 8080 in localhost.

How to avoid certain chats being replied by the bot?

By default the bot will ignore messages sent in group chats, blocked and archived chats/contacts.

Besides that, you can blacklist or whitelist specific phone numbers and chat with labels that be handled by the bot.

See numbersBlacklist, numbersWhitelist, and skipChatWithLabels options in config.js for more information.

Can I run this bot on my server?

Absolutely! Just deploy or transfer the program source code to your server and run the command from there. The requirements are the same, no matter where you run the bot.


Was this article helpful?

Related articles


Categories

FAQ