RIPPLE : RUBEK MAHARJAN

In simple words, a chatbot is a computer program that you can talk to, just like chatting with a person. It understands what you type and replies back with useful answers.

In this blog, I’ll walk you through the step-by-step process of creating a Viber chatbot using Node.js.

The main sections of this blog are:

  1. Create a Viber Public Account.
  2. Setup your bot server.
  3. Generate Webhook URL.
  4. Test the bot.

Step 1: Create a Viber Public Account

The first step is, creating a Viber public account to make a bot.

  1. Go to Viber Admin Panel
  2. Sign in with your Viber-connected phone number
  3. Click Create Bot Account
  4. Fill in details: Name, Description, Category, Subcategory, Profile Image
  5. After creating the bot, you’ll receive a Token — keep this safe. You’ll use this to access the Viber API.

Step 2: Setup your bot server.

Your bot runs on your own server. Viber will send HTTP requests (webhooks) to it. You can use programming language of your choice (NodeJs, Python, PHP). I will use NodeJs.

i. Setup and install required dependencies

npm init -y
npm install express axios body-parser dotenv

ii. Create environment variables

VIBER_BOT_TOKEN = 50994b6c9c27e7ca-XXXXXXXXXXXXXX-XXXXXXXXXXXXX
VIBER_API_URL = https://chatapi.viber.com/pa
WEBHOOK_URL = https://0ff5-2400-xxxxxxxxxxxxxxxxxx.ngrok-free.app/webhook
PORT = 3000

iii. Import all the requirements for the project and load environment variables

const express = require('express')
const axios = require('axios')
const bodyParser = require('body-parser')
require('dotenv').config()

const VIBER_BOT_TOKEN = process.env.VIBER_BOT_TOKEN
const VIBER_API_URL = process.env.VIBER_API_URL
const WEBHOOK_URL = process.env.WEBHOOK_URL
const PORT = process.env.PORT || 3000

iv. Set the Webhook

async function setWebhook() {
try {
const response = await axios.post(
`${VIBER_API_URL}/set_webhook`,
{
url: WEBHOOK_URL,
event_types: ['delivered', 'seen', 'conversation_started', 'message']
},
{
headers: {
'X-Viber-Auth-Token': VIBER_BOT_TOKEN
}
}
);
console.log('Webhook set:', response.data);
} catch (error) {
console.error('Error setting webhook:', error);
}
}

setWebhook()

This tells Viber: “Here’s the URL (/webhook) where you can send messages or events.” It sends a POST request to Viber with the bot’s URL and what events you want to receive (like when someone sends a message or starts a conversation).

v. Send messages back to user

async function sendMessage(to, text) {
try {
await axios.post(
`${VIBER_API_URL}/send_message`,
{
receiver: to,
min_api_version: 1,
sender: {
name: "Your Bot",
avatar: "http://avatar.com/avatar.png"
},
tracking_data: "tracking data",
type: "text",
text: text
},
{
headers: {
'X-Viber-Auth-Token': VIBER_BOT_TOKEN
}
}
)
} catch (error) {
console.error('Error sending message:', error)
}
}

This is a helper function. When someone sends a message, your bot can reply using this function. You tell Viber: “Send this text to that user.”

vi. Handle incoming requests

app.post('/webhook', (req, res) => {
const messageData = req.body;

if (messageData.message && messageData.message.text) {
const userId = messageData.sender.id;
const messageText = messageData.message.text;

const replyText = `You said: ${messageText}`;
sendMessage(userId, replyText);
}

res.status(200).send('OK');
})

This is the main place where your bot receives messages. When someone sends something, Viber forwards it to this /webhook endpoint.

vii. Start the server

app.listen(PORT, () => {
console.log(`Viber bot is running on port ${PORT}`);
});

The final code looks like this:

const express = require('express')
const axios = require('axios')
const bodyParser = require('body-parser')

require('dotenv').config()

const app = express()
app.use(bodyParser.json())

const VIBER_BOT_TOKEN = process.env.VIBER_BOT_TOKEN
const VIBER_API_URL = process.env.VIBER_API_URL
const WEBHOOK_URL = process.env.WEBHOOK_URL
const PORT = process.env.PORT || 3000

async function setWebhook() {
try {
const response = await axios.post(
`${VIBER_API_URL}/set_webhook`,
{
url: WEBHOOK_URL,
event_types: ['delivered', 'seen', 'conversation_started', 'message']
},
{
headers: {
'X-Viber-Auth-Token': VIBER_BOT_TOKEN
}
}
);
console.log('Webhook set:', response.data);
} catch (error) {
console.error('Error setting webhook:', error);
}
}

setWebhook()

async function sendMessage(to, text) {
try {
await axios.post(
`${VIBER_API_URL}/send_message`,
{
receiver: to,
min_api_version: 1,
sender: {
name: "Your Bot",
avatar: "http://avatar.com/avatar.png"
},
tracking_data: "tracking data",
type: "text",
text: text
},
{
headers: {
'X-Viber-Auth-Token': VIBER_BOT_TOKEN
}
}
)
} catch (error) {
console.error('Error sending message:', error)
}
}

app.post('/webhook', (req, res) => {
const messageData = req.body;

if (messageData.message && messageData.message.text) {
const userId = messageData.sender.id;
const messageText = messageData.message.text;

const replyText = `You said: ${messageText}`;
sendMessage(userId, replyText);
}

res.status(200).send('OK');
})

app.listen(PORT, () => {
console.log(`Viber bot is running on port ${PORT}`);
});

Step 3: Generate Webhook URL

A webhook URL is like a special address on the internet where one app can send information to another automatically when something happens.

Think of it like this, Imagine you’re expecting a package, and you give your phone number to the delivery service. When the package is close, they send you a message.

In this case, you = your bot, the delivery service = Viber, your phone number = webhook URL.

So, a webhook URL is the “phone number” your bot gives to Viber so it knows where to send new messages or events.

We will generate a webhook URL using ngrok. Ngrok is a tool that lets you expose your local computer (localhost) to the internet using a public URL.

Start ngrok

//If your local server runs on port 3000 
ngrok http 3000

Now, you’ll get a forwarding URL from ngrok. Save this URL in your .env file as WEBHOOK_URL.

Step 4: Test the bot

Now, it’s time for the final step — testing your bot. Open Viber, send a message to your bot, and see if it responds as expected. If everything is set up correctly, you should receive a reply based on your code. This confirms that your bot is working and connected properly!

And that’s how you create a basic Viber bot! This was a simple example to help you get started, but Viber bots can be used for a wide range of use cases — from business support and personal assistants to task automation and much more.

Leave a Reply

Your email address will not be published. Required fields are marked *