How to create Viber Messaging API Application
To create a simple Viber messaging application using the Viber REST Bot API, you'll need to follow these steps. This example assumes you have some experience with setting up web servers and writing code in Node.js, but the process will be similar in other languages.
Prerequisites:
Viber Account: Ensure you have a Viber account and that you've created a Viber bot.
Bot Token: Once you've created the bot, you'll be provided with a bot token.
Webhook URL: You need to expose your server with a public URL (for example, using ngrok).
Node.js: Ensure you have Node.js installed in your environment.
Steps to Create a Simple Viber Messaging App
Install Required Dependencies
First, you'll need to set up a simple server and install the required packages.
npm init -y
npm install express body-parser axios
Set up the Server
In your project directory, create a file called index.js
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
// Initialize the app and use JSON parsing middleware
const app = express();
app.use(bodyParser.json());
// Viber Bot token
const VIBER_BOT_TOKEN = 'YOUR_VIBER_BOT_TOKEN';
// Set the Webhook URL
const setWebhook = async () => {
try {
const response = await axios.post(`https://chatapi.viber.com/pa/set_webhook`, {
url: 'YOUR_PUBLIC_WEBHOOK_URL',
}, {
headers: {
'X-Viber-Auth-Token': VIBER_BOT_TOKEN,
},
});
console.log('Webhook set:', response.data);
} catch (error) {
console.error('Error setting webhook:', error);
}
};
// When the server is started, set the webhook
setWebhook();
// Route for receiving messages from Viber
app.post('/viber/webhook', (req, res) => {
const message = req.body;
// Log incoming message
console.log('Incoming message:', message);
// When receiving a message from a user, reply with a text message
if (message.event === 'message' && message.message.type === 'text') {
const reply = {
receiver: message.sender.id,
min_api_version: 1,
sender: {
name: 'YourBot',
avatar: 'https://example.com/avatar.jpg', // Optional avatar URL
},
tracking_data: 'tracking data',
type: 'text',
text: `You said: ${message.message.text}`,
};
axios.post('https://chatapi.viber.com/pa/send_message', reply, {
headers: {
'X-Viber-Auth-Token': VIBER_BOT_TOKEN,
},
}).then((response) => {
console.log('Message sent:', response.data);
}).catch((error) => {
console.error('Error sending message:', error);
});
}
// Send an OK response back to Viber
res.sendStatus(200);
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Set the Webhook URL
You will need a public-facing URL to expose your local server, which can be done with tools like ngrok.
Once you’ve installed ngrok, run the following command to expose port 3000:
ngrok http 3000
You’ll receive a URL that looks like https://xxxx.ngrok.io
. Replace YOUR_PUBLIC_WEBHOOK_URL
in the code with this URL followed by /viber/webhook
.
Test the Bot
Once your webhook is set up, the bot will be able to receive messages.
When a user sends a text message to the bot, the bot will respond with
You said: <their message>
.
Testing
To test the bot:
Open Viber and search for your bot.
Send the bot a message, and the bot should reply with the message content.
Conclusion
This is a basic setup to create a simple Viber messaging app using the Viber Bot API. You can expand on this by adding features like sending images, buttons, and rich media. The API documentation linked contains details on all available message types and events.
Last updated