Use this library to develop a bot for the Viber platform. The library is available on GitHub as well as a package on npm.
This library is released under the terms of the Apache 2.0 license. See License for more information.
- Node >= 5.0.0
- An Active Viber account on a platform which supports Public Accounts/ bots (iOS/Android). This account will automatically be set as the account administrator during the account creation process.
- Active Public Account/ bot - Create an account here.
- Account authentication token - unique account identifier used to validate your account in all API requests. Once your account is created your authentication token will appear in the account’s “edit info” screen (for admins only). Each request posted to Viber by the account will need to contain the token.
- Webhook - Please use a server endpoint URL that supports HTTPS. If you deploy on your own custom server, you'll need a trusted (ca.pem) certificate, not self-signed. Read our blog post on how to test your bot locally.
This library is released on npm.
Install with npm install viber-bot --save
If you are already using express or equivalent, you can do the following:
app.use("/viber/webhook",bot.middleware());Please revisit app.use() documentation. For more information see ViberBot.middleware().
Creating a basic Viber bot is simple:
- Import
viber-botlibrary to your project - Create a Public Account or bot and use the API key from https://developers.viber.com
- Configure your bot as described in the documentation below
- Add the bot as middleware to your server with
bot.middleware() - Start your web server
- Call
setWebhook(url)with your web server url
Firstly, let's import and configure our bot:
'use strict';constViberBot=require('viber-bot').Bot;constBotEvents=require('viber-bot').Events;constbot=newViberBot({authToken: YOUR_AUTH_TOKEN_HERE,name: "EchoBot",avatar: "http://viber.com/avatar.jpg"// It is recommended to be 720x720, and no more than 100kb.});// Perfect! Now here's the key part:bot.on(BotEvents.MESSAGE_RECEIVED,(message,response)=>{// Echo's back the message to the client. Your bot logic should sit here.response.send(message);});// Wasn't that easy? Let's create HTTPS server and set the webhook:consthttps=require('https');constport=process.env.PORT||8080;// Viber will push messages sent to this URL. Web server should be internet-facing.constwebhookUrl=process.env.WEBHOOK_URL;consthttpsOptions={key: ...,cert: ...,ca: ...
};// Trusted SSL certification (not self-signed).https.createServer(httpsOptions,bot.middleware()).listen(port,()=>bot.setWebhook(webhookUrl));We provide an option to use Winston logger with our library. The only requirement is that you use Winston >= 2.0.0.
'use strict';constViberBot=require('viber-bot').Bot;constwinston=require('winston');consttoYAML=require('winston-console-formatter');// makes the output more friendlyfunctioncreateLogger(){constlogger=newwinston.Logger({level: "debug"});// We recommend DEBUG for developmentlogger.add(winston.transports.Console,toYAML.config());returnlogger;}constlogger=createLogger();constbot=newViberBot({logger: logger,authToken: ...,
...
});Well funny you ask. Yes we do. But a word of warning - messages sent to your router callback will also be emitted to the BotEvents.MESSAGE_RECEIVED event.
constTextMessage=require('viber-bot').Message.Text;// A simple regular expression to answer messages in the form of 'hi' and 'hello'.bot.onTextMessage(/^hi|hello$/i,(message,response)=>response.send(newTextMessage(`Hi there ${response.userProfile.name}. I am ${bot.name}`)));Have you noticed how we created the TextMessage instance? There's a all bunch of message types you should get familiar with.
- Text Message
- Url Message
- Contact Message
- Picture Message
- Video Message
- Location Message
- Sticker Message
- File Message
- Rich Media Message
- Keyboard Message
Creating them is easy! Every message object has its own unique constructor corresponding to its API implementation. Click on each type in the list to find out more. Check out the full API documentation for more advanced uses.
require('viber-bot').Bot
An event emitter, emitting events described here.
- ViberBot
- new ViberBot()
- .getBotProfile() ⇒
promise.JSON - .getUserDetails(userProfile) ⇒
promise.JSON - .getOnlineStatus(viberUserIds) ⇒
promise.JSON - .setWebhook(url) ⇒
promise.JSON - .sendMessage(userProfile, messages, [optionalTrackingData]) ⇒
promise.ARRAY - .postToPublicChat(userProfile, messages) ⇒
promise.ARRAY - .on(handler)
- .onTextMessage(regex, handler) :
handler=TextMessageHandlerCallback - .onError(handler) :
handler=ErrorHandlerCallback - .onConversationStarted(userProfile, isSubscribed, context, onFinish) :
onFinish=ConversationStartedOnFinishCallback - .onSubscribe(handler) :
handler=SubscribeResponseHandlerCallback - .onUnsubscribe(handler) :
handler=UnsubscribeResponseHandlerCallback - .middleware()
| Param | Type | Description |
|---|---|---|
| options.logger | object | Winston logger |
| options.authToken | string | Viber Auth Token |
| options.name | string | Your BOT Name |
| options.avatar | string | Avatar URL. No more than 100kb. |
| options.registerToEvents | array | example: ["message", "delivered"] |
require('viber-bot').Events
| Param | Type |
|---|---|
| handler | EventHandlerCallback |
| message | Message Object |
| response | Response Object |
| err | Error Object |
Subscribe to events:
- MESSAGE_RECEIVED (Callback:
function (message, response) {}) - MESSAGE_SENT (Callback:
function (message, userProfile) {}) - SUBSCRIBED (Callback:
function (response) {}) - UNSUBSCRIBED (Callback:
function (response) {}) - CONVERSATION_STARTED (Callback:
function (userProfile, isSubscribed, context, onFinish) {}) - ERROR (Callback:
function (err) {})
Example
bot.on(BotEvents.MESSAGE_RECEIVED,(message,response)=> ... );bot.on(BotEvents.MESSAGE_SENT,(message,userProfile)=> ... );bot.on(BotEvents.CONVERSATION_STARTED,(userProfile,isSubscribed,context,onFinish)=> ... );bot.on(BotEvents.ERROR,err=> ... );bot.on(BotEvents.UNSUBSCRIBED,response=> ... );bot.on(BotEvents.SUBSCRIBED,response=>response.send(`Thanks for subscribing, ${response.userProfile.name}`));Returns a promise.JSONwith the following JSON.
bot.getBotProfile().then(response=>console.log(`Public Account Named: ${response.name}`));| Param | Type | Description |
|---|---|---|
| userProfile | UserProfile | UserProfile object |
The getUserDetails function will fetch the details of a specific Viber user based on his unique user ID. The user ID can be obtained from the callbacks sent to the account regarding user's actions. This request can be sent twice during a 12 hours period for each user ID.
Returns a promise.JSON.
bot.onSubscribe(response=>bot.getUserDetails(response.userProfile).then(userDetails=>console.log(userDetails)));| Param | Type | Description |
|---|---|---|
| viberUserIds | array of strings | Collection of Viber user ids |
Returns a promise.JSON.
bot.getOnlineStatus(["a1, "a2"]).then(onlineStatus=>console.log(onlineStatus));| Param | Type | Description |
|---|---|---|
| url | string | Trusted SSL Certificate |
Returns a promise.JSON.
bot.setWebhook("https://my.bot/incoming").then(()=>yourBot.doSomething()).catch(err=>console.log(err));| Param | Type | Description |
|---|---|---|
| userProfile | UserProfile | UserProfile object |
| messages | object or array | Can be Message object or array of Message objects |
| [optionalTrackingData] | JSON | Optional. JSON Object. Returned on every message sent by the client |
Note: When passing array of messages to sendMessage, messages will be sent by explicit order (the order which they were given to the sendMessage method). The library will also cancel all custom keyboards except the last one, sending only the last message keyboard.
Returns a promise.ARRAY array of message tokens.
// Single messageconstTextMessage=require('viber-bot').Message.Text;bot.sendMessage(userProfile,newTextMessage("Thanks for shopping with us"));// Multiple messagesconstUrlMessage=require('viber-bot').Message.Url;bot.sendMessage(userProfile,[newTextMessage("Here's the product you've requested:"),newUrlMessage("http://my.ecommerce.site/product1"),newTextMessage("Shipping time: 1-3 business days")]);The Viber post API allows the Public Account owner to post a message in the Public Account’s public chat.
| Param | Type | Description |
|---|---|---|
| userProfile | UserProfile | UserProfile object |
| messages | object or array | Can be Message object or array of Message objects |
Note: When passing array of messages to postToPublicChat, messages will be sent by explicit order (the order which they were given to the postToPublicChat method).
Note: This method does not support keyboard attachment.
Returns a promise.ARRAY array of message tokens.
// Single messageconstTextMessage=require('viber-bot').Message.Text;bot.postToPublicChat(userProfile,newTextMessage("Thanks for shopping with us"));// Multiple messagesconstUrlMessage=require('viber-bot').Message.Url;bot.postToPublicChat(userProfile,[newTextMessage("Here's the product you've requested:"),newUrlMessage("http://my.ecommerce.site/product1"),newTextMessage("Shipping time: 1-3 business days")]);Returns a middleware implementation to use with http/https.
consthttps=require('https');https.createServer({key: ...,cert: ...,ca: ...
},bot.middleware()).listen(8080);| Param | Type |
|---|---|
| regex | regular expression |
| handler | TextMessageHandlerCallback |
bot.onTextMessage(/^hi|hello$/i,(message,response)=>response.send(newTextMessage(`Hi there ${response.userProfile.name}. I am ${bot.name}`)));| Param | Type |
|---|---|
| handler | ErrorHandlerCallback |
bot.onError(err=>logger.error(err));| Param | Type | Description |
| --- | --- |
| userProfile | UserProfile | UserProfile object |
| isSubscribed | boolean | Indicates whether a user is already subscribed |
| context | String | Any additional parameters added to the deep link used to access the conversation passed as a string |
| onFinish | ConversationStartedOnFinishCallback | When called, a Message will be sent to the client |
Conversation started event fires when a user opens a conversation with the Public Account/ bot using the “message” button (found on the account’s info screen) or using a deep link.
This event is not considered a subscribe event and doesn't allow the account to send messages to the user; however, it will allow sending one "welcome message" to the user. See sending a welcome message below for more information.
The ConversationStartedOnFinishCallback accepts null and MessageObject only. Otherwise, an exception is thrown.
bot.onConversationStarted((userProfile,isSubscribed,context,onFinish)=>onFinish(newTextMessage(`Hi, ${userProfile.name}! Nice to meet you.`)));bot.onConversationStarted((userProfile,isSubscribed,context,onFinish)=>onFinish(newTextMessage(`Thanks`),{saidThanks: true}));| Param | Type |
|---|---|
| handler | SubscribeResponseHandlerCallback |
bot.onSubscribe(response=>console.log(`Subscribed: ${response.userProfile.name}`));| Param | Type |
|---|---|
| handler | UnsubscribeResponseHandlerCallback |
bot.onUnsubscribe(userId=>console.log(`Unsubscribed: ${userId}`));Members:
| Param | Type | Notes |
|---|---|---|
| userProfile | UserProfile | --- |
- Response
- .send(messages, [optionalTrackingData]) ⇒
promise.JSON
- .send(messages, [optionalTrackingData]) ⇒
Members:
| Param | Type | Notes |
|---|---|---|
| id | string | --- |
| name | string | --- |
| avatar | string | Optional Avatar URL |
| country | string | currently set in CONVERSATION_STARTED event only |
| language | string | currently set in CONVERSATION_STARTED event only |
constTextMessage=require('viber-bot').Message.Text;constUrlMessage=require('viber-bot').Message.Url;constContactMessage=require('viber-bot').Message.Contact;constPictureMessage=require('viber-bot').Message.Picture;constVideoMessage=require('viber-bot').Message.Video;constLocationMessage=require('viber-bot').Message.Location;constStickerMessage=require('viber-bot').Message.Sticker;constRichMediaMessage=require('viber-bot').Message.RichMedia;constKeyboardMessage=require('viber-bot').Message.Keyboard;Common Members for Message interface:
| Param | Type | Description |
|---|---|---|
| timestamp | string | Epoch time |
| token | string | Sequential message token |
| trackingData | JSON | JSON Tracking Data from Viber Client |
Common Constructor Arguments Message interface:
| Param | Type | Description |
|---|---|---|
| optionalKeyboard | JSON | Writing Custom Keyboards |
| optionalTrackingData | JSON | Data to be saved on Viber Client device, and sent back each time message is received |
| Member | Type |
|---|---|
| text | string |
constmessage=newTextMessage(text,[optionalKeyboard],[optionalTrackingData]);console.log(message.text);| Member | Type |
|---|---|
| url | string |
constmessage=newUrlMessage(url,[optionalKeyboard],[optionalTrackingData]);console.log(message.url);| Member | Type |
|---|---|
| contactName | string |
| contactPhoneNumber | string |
constmessage=newContactMessage(contactName,contactPhoneNumber,[optionalAvatar],[optionalKeyboard],[optionalTrackingData]);console.log(`${message.contactName}, ${message.contactPhoneNumber}`);| Member | Type |
|---|---|
| url | string |
| text | string |
| thumbnail | string |
constmessage=newPictureMessage(url,[optionalText],[optionalThumbnail],[optionalKeyboard],[optionalTrackingData]);console.log(`${message.url}, ${message.text}, ${message.thumbnail}`);| Member | Type |
|---|---|
| url | string |
| size | int |
| thumbnail | string |
| duration | int |
constmessage=newVideoMessage(url,size,[optionalText],[optionalThumbnail],[optionalDuration],[optionalKeyboard],[optionalTrackingData]);console.log(`${message.url}, ${message.size}, ${message.thumbnail}, ${message.duration}`);| Member | Type |
|---|---|
| latitude | float |
| longitude | float |
constmessage=newLocationMessage(latitude,longitude,[optionalKeyboard],[optionalTrackingData]);console.log(`${message.latitude}, ${message.longitude}`);| Member | Type |
|---|---|
| stickerId | int |
constmessage=newStickerMessage(stickerId,[optionalKeyboard],[optionalTrackingData]);console.log(message.stickerId);| Member | Type |
|---|---|
| url | string |
| sizeInBytes | int |
| filename | string |
constmessage=newFileMessage(url,sizeInBytes,filename,[optionalKeyboard],[optionalTrackingData]);console.log(`${message.url}, ${message.sizeInBytes}, ${message.filename}`);| Member | Type |
|---|---|
| richMedia | Object |
constSAMPLE_RICH_MEDIA={"ButtonsGroupColumns": 6,"ButtonsGroupRows": 2,"BgColor": "#FFFFFF","Buttons": [{"ActionBody": "http://www.website.com/go_here","ActionType": "open-url","BgMediaType": "picture","Image": "http://www.images.com/img.jpg","BgColor": "#000000","TextOpacity": 60,"Rows": 4,"Columns": 6},{"ActionBody": "http://www.website.com/go_here","ActionType": "open-url","BgColor": "#85bb65","Text": "Buy","TextOpacity": 60,"Rows": 1,"Columns": 6}]};constmessage=newRichMedia(SAMPLE_RICH_MEDIA,[optionalKeyboard],[optionalTrackingData]);| Member | Type |
|---|---|
| keyboard | JSON |
constSAMPLE_KEYBOARD={"Type": "keyboard","Revision": 1,"Buttons": [{"Columns": 3,"Rows": 2,"BgColor": "#e6f5ff","BgMedia": "http://www.jqueryscript.net/images/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg","BgMediaType": "picture","BgLoop": true,"ActionType": "reply","ActionBody": "Yes"}]};constmessage=newKeyboardMessage(SAMPLE_KEYBOARD,[optionalTrackingData]);We've created the Is It Up sample project to help you get started.
Join the conversation on Gitter.