klink.cloud - Product User Manual
  • Overview
    • 👋Welcome to klink.cloud
  • Getting Started
    • 🟦Setup Channels
      • Facebook Messenger
        • Connect to Facebook Messenger
        • Primary Routing Setup (Facebook & Instagram)
      • Facebook Feed
        • Connect to Facebook Feed
        • Primary Routing Setup (Facebook & Instagram)
      • LINE OA
        • Get Channel Secret and Channel Access Token
        • Connect to LINE OA
      • Instagram
      • Viber for Business
      • Google Workspace Email Setup
      • How to Setup Telegram Bot Channel
      • How to setup Telegram Business Account
      • How to set up WhatsApp Business Channel (Meta)
      • How to set up WhatsApp Mobile Channel
  • How to use
    • 📞Call
      • Inbound Call Handling
      • Outbound Call Handling
    • 💬Chat
      • Inbound Chat Handling
      • Outbound Chat Handling
      • Facebook Feed Handling
    • 🔖Tickets
      • Create a Ticket
      • Assign / Transfer Ticket
    • 📇Contact
      • How to Import Leads / Contacts
      • Contact Custom Fields
      • Individual Contacts
      • Company Contacts
      • Import / Export CSV
  • Advanced Setup
    • Setup Workflow Automation
    • Setup Queue Group
    • 🕖Setup SLA Targets
    • Setup Queue, Workflow, and SLA
  • Dashboard & Reports
    • 📊Reporting
      • CDR
      • Recording
      • Call Reports
        • Agent Call Statistics
        • Agent Call Activity
        • Trunk Activity
        • Queue AVG Waiting & Talking Time
        • Queue Performance
        • Agent Missed Call Activity
        • Agent Call Summary
        • Satisfaction Survey
      • Agent Status Report
      • CX Log
    • 📉Analytics Dashboard
      • 📶Call Dashboard
      • 📈Chat Dashboard
      • 💎Ticket Dashboard
  • Integrations
    • 🛒Ecommerce Marketplace
      • Shopee Integrations
      • Lazada Integrations
    • 🔈CRM Integrations
      • Salesforce CRM Integration
      • How to create Viber Messaging API Application
      • How to send transactional Viber Business Message
  • Messaging API
    • Line Messaging API
  • Features Roadmap & FAQ
    • ⏱️Features Road Map
    • 📢Release Notes
      • Chat Room Folder Enhancements
      • Enable/Disable Auto-Assign Toggle in Queue Settings
      • Agent Takeover Bot Chat Room
      • Workflow Automation - New Action "Send Message"
      • Enhancement of Hold Function
      • Workflow Automation Rule
      • Non-Voice Analytics Dashboard
      • WhatsApp Mobile Integration
      • Telegram Group Chat Integration for Inbox Module
      • Business Portfolio Integration - Facebook Feed
      • Call Recording Pause and Resume Feature
      • Advanced Hold Functionality for Efficient Conversation Management
      • Viber 24-Hour Response Timer
      • Enhanced Queue Settings and Workflow Automation Controls
      • Telegram Business Integration
      • Multi-Tab Call Support
      • Business Portfolio- (Meta) Messenger Integration
      • Mail Integration Enhancement and Telegram Integration
      • Facebook Feed Enhancement
      • Outlook Email Integration Enhancement (Beta)
      • Queue Setting (Beta)
      • Permission Enhancements and Multiple Assigns
      • CX Logs Enhancement
      • SMS In-bound Beta Version, SMS Integration Setting
      • Facebook Feed Integration (Beta)
      • SLA Setting and Business Hour Functionalities
      • Wrap Up Form Features and Enhancements
      • New Features & Enhancements
      • New Features and Improvement
      • New Features and Major Bug Fixes
    • FAQ
Powered by GitBook
On this page
  1. Integrations
  2. CRM Integrations

How to create Viber Messaging API Application

Last updated 7 months ago

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:

  1. Viber Account: Ensure you have a Viber account and that you've created a Viber bot.

  2. Bot Token: Once you've created the bot, you'll be provided with a bot token.

  3. Webhook URL: You need to expose your server with a public URL (for example, using ).

  4. Node.js: Ensure you have Node.js installed in your environment.

Steps to Create a Simple Viber Messaging App

  1. 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
  1. 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}`);
});
  1. Set the Webhook URL

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.

  1. 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:

  1. Open Viber and search for your bot.

  2. 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.

You will need a public-facing URL to expose your local server, which can be done with tools like .

🔈
ngrok
ngrok