In this guide:
- Intro
- Text messages
- Custom Keyboard
- Photo messages
- Video messages
- File messages
- Contact messages
- Location messages
- URL messages
- Sticker messages
- Sending multiple messages
- Handling errors
Viber Template Message builder allows you to generate more complex messages for Viber without writing JSON files manually.
To use it, just require viberTemplate
function from Claudia Bot Builder:
const viberTemplate = require('claudia-bot-builder').viberTemplate;
viberTemplate
exports an object that contains multiple classes that allows you to generate different types of structured messages for Viber:
- Text messages (we need a template for this just in case you want to add custom keyboard to it)
- Photo
- Video
- File
- Contact
- Location
- Url
- Sticker
More info about each type of structured messages can be found in the official documentation.
If you simply want to answer with the text you can just return text. But in case you want to answer with text with a custom keyboard you should use Text method.
Text
(class) - Class that allows you to build text messages with custom keyboard.
Arguments:
- text, string (required) - a simple text to send as a reply.
Method | Required | Arguments | Returns | Description |
---|---|---|---|---|
addReplyKeyboard | No | isDefaultHeight (boolean, optional, is keyboard default height, default is true), backgroundColor (string, optional, keyboard background color) |
this for chaining |
Adds a custom keyboard |
addKeyboardButton | No | text (string, required, button text), buttonValue (string, required, url or text value that will be returned on press), columnSize (number, optional, a width of the button), rowSize (number, optional, a height of the button), buttonObj (object, optional, additional button settings, should be a valid values from Viber Keyboard docs) |
this for chaining |
Viber button on a keyboard, more info is available here |
get | Yes | No arguments | Formatted JSON to pass as a reply | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Viber |
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
new viberTemplate.Text(`What's your favorite House in Game Of Thrones`)
.addReplyKeyboard()
.addKeyboardButton('Stark', 'STARK', 6, 1)
.addKeyboardButton('Lannister', 'LANNISTER', 6, 1)
.addKeyboardButton('Targaryen', 'TARGARYEN', 6, 1)
.addKeyboardButton('None of the above', 'OTHER', 6, 1)
.get();
});
Each of the methods below has 2 methods for adding custom keyboards and buttons.
Following methods are allowed:
- addReplyKeyboard
- addKeyboardButton - only when keyboard is added, otherwise it'll throw an error
If you add multiple keyboards only the last one will be active
Photo attachment allows you to send images.
Photo
(class) - Class that allows you to build a photo messages with optional custom keyboards.
Arguments:
- photo, string (required) - an url or an ID of already uploaded image.
- caption, string (optional) - a caption for an image, default is an empty string ('')
Method | Required | Arguments | Returns | Description |
---|---|---|---|---|
addReplyKeyboard | No | isDefaultHeight (boolean, optional, is keyboard default height, default is true), backgroundColor (string, optional, keyboard background color) |
this for chaining |
Adds a custom keyboard |
addKeyboardButton | No | text (string, required, button text), buttonValue (string, required, url or text value that will be returned on press), columnSize (number, optional, a width of the button), rowSize (number, optional, a height of the button), buttonObj (object, optional, additional button settings, should be a valid values from Viber Keyboard docs) |
this for chaining |
Viber button on a keyboard, more info is available here |
get | Yes | No arguments | Formatted JSON to pass as a reply | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Viber |
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
return new viberTemplate.Photo('https://claudiajs.com/assets/claudiajs.png').get();
});
Video message allows you to send videos.
Video
(class) - Class that allows you to build a video messages with optional custom keyboards.
Arguments:
- media, string (required) - an url of a video.
- size, number (required) - a size of the video in bytes.
- duration, integer (optional) - an optional duration of a video file.
Method | Required | Arguments | Returns | Description |
---|---|---|---|---|
addReplyKeyboard | No | isDefaultHeight (boolean, optional, is keyboard default height, default is true), backgroundColor (string, optional, keyboard background color) |
this for chaining |
Adds a custom keyboard |
addKeyboardButton | No | text (string, required, button text), buttonValue (string, required, url or text value that will be returned on press), columnSize (number, optional, a width of the button), rowSize (number, optional, a height of the button), buttonObj (object, optional, additional button settings, should be a valid values from Viber Keyboard docs) |
this for chaining |
Viber button on a keyboard, more info is available here |
get | Yes | No arguments | Formatted JSON to pass as a reply | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Viber |
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
return new viberTemplate.Video('http://example.com/videos/video.mp4', 1024).get();
});
File messages allows you to send files with optional custom keyboard. Multiple file formats can be sent, but there's a list of forbidden formats in Viber documentation.
File
(class) - Class that allows you to build a file messages with optional custom keyboards.
Arguments:
- media, string (required) - an url of a file.
- size, number (required) - file size in bytes.
- filename, string (required) - a name of the file
Method | Required | Arguments | Returns | Description |
---|---|---|---|---|
addReplyKeyboard | No | isDefaultHeight (boolean, optional, is keyboard default height, default is true), backgroundColor (string, optional, keyboard background color) |
this for chaining |
Adds a custom keyboard |
addKeyboardButton | No | text (string, required, button text), buttonValue (string, required, url or text value that will be returned on press), columnSize (number, optional, a width of the button), rowSize (number, optional, a height of the button), buttonObj (object, optional, additional button settings, should be a valid values from Viber Keyboard docs) |
this for chaining |
Viber button on a keyboard, more info is available here |
get | Yes | No arguments | Formatted JSON to pass as a reply | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Viber |
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
return new viberTemplate.File('http://example.com/files/file.pdf', 1024, 'Random file').get();
});
Contact messages allows you to send a contact info.
Contact
(class) - Class that allows you to build a contact messages with optional custom keyboards.
Arguments:
- name, string (required) - contact name.
- phoneNumber, string (required) - contact phone number.
Method | Required | Arguments | Returns | Description |
---|---|---|---|---|
addReplyKeyboard | No | isDefaultHeight (boolean, optional, is keyboard default height, default is true), backgroundColor (string, optional, keyboard background color) |
this for chaining |
Adds a custom keyboard |
addKeyboardButton | No | text (string, required, button text), buttonValue (string, required, url or text value that will be returned on press), columnSize (number, optional, a width of the button), rowSize (number, optional, a height of the button), buttonObj (object, optional, additional button settings, should be a valid values from Viber Keyboard docs) |
this for chaining |
Viber button on a keyboard, more info is available here |
get | Yes | No arguments | Formatted JSON to pass as a reply | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Viber |
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
return new viberTemplate.Contact('Alex', '+972511123123').get();
});
Location template allows you to send a location.
Location
(class) - Class that allows you to build a location messages with optional custom keyboards.
Arguments:
- latitude, number (required) - location latitude.
- longitude, number (required) - location longitude.
Method | Required | Arguments | Returns | Description |
---|---|---|---|---|
addReplyKeyboard | No | isDefaultHeight (boolean, optional, is keyboard default height, default is true), backgroundColor (string, optional, keyboard background color) |
this for chaining |
Adds a custom keyboard |
addKeyboardButton | No | text (string, required, button text), buttonValue (string, required, url or text value that will be returned on press), columnSize (number, optional, a width of the button), rowSize (number, optional, a height of the button), buttonObj (object, optional, additional button settings, should be a valid values from Viber Keyboard docs) |
this for chaining |
Viber button on a keyboard, more info is available here |
get | Yes | No arguments | Formatted JSON to pass as a reply | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Viber |
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
return new viberTemplate.Location(44.831115, 20.421277).get();
});
URL template allows you to send a link.
Url
(class) - Class that allows you to build a link messages with optional custom keyboards.
Arguments:
- url, string (required) - valid URL.
Method | Required | Arguments | Returns | Description |
---|---|---|---|---|
addReplyKeyboard | No | isDefaultHeight (boolean, optional, is keyboard default height, default is true), backgroundColor (string, optional, keyboard background color) |
this for chaining |
Adds a custom keyboard |
addKeyboardButton | No | text (string, required, button text), buttonValue (string, required, url or text value that will be returned on press), columnSize (number, optional, a width of the button), rowSize (number, optional, a height of the button), buttonObj (object, optional, additional button settings, should be a valid values from Viber Keyboard docs) |
this for chaining |
Viber button on a keyboard, more info is available here |
get | Yes | No arguments | Formatted JSON to pass as a reply | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Viber |
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
return new viberTemplate.Url('https://claudiajs.com').get();
});
Sticker template allows you to send a Viber sticker.
Sticker
(class) - Class that allows you to build a sticker messages with optional custom keyboards.
Arguments:
- stickerId, number (required) - valid sticker ID, check Viber documentation for more info.
Method | Required | Arguments | Returns | Description |
---|---|---|---|---|
addReplyKeyboard | No | isDefaultHeight (boolean, optional, is keyboard default height, default is true), backgroundColor (string, optional, keyboard background color) |
this for chaining |
Adds a custom keyboard |
addKeyboardButton | No | text (string, required, button text), buttonValue (string, required, url or text value that will be returned on press), columnSize (number, optional, a width of the button), rowSize (number, optional, a height of the button), buttonObj (object, optional, additional button settings, should be a valid values from Viber Keyboard docs) |
this for chaining |
Viber button on a keyboard, more info is available here |
get | Yes | No arguments | Formatted JSON to pass as a reply | Get method is required and it returns a formatted JSON that is ready to be passed as a response to Viber |
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
return new viberTemplate.Sticker(40126).get();
});
It's easy to send multiple messages, just pass an array with at least one of the methods mentioned above. You can combine them how ever you want.
const botBuilder = require('claudia-bot-builder');
const viberTemplate = botBuilder.viberTemplate;
module.exports = botBuilder(message => {
if (message.type === 'viber')
return [
'Hello!',
new viberTemplate.Text(`What's your favorite House in Game Of Thrones`)
.addReplyKeyboard()
.addKeyboardButton('Stark', 'STARK', 6, 1)
.addKeyboardButton('Lannister', 'LANNISTER', 6, 1)
.addKeyboardButton('Targaryen', 'TARGARYEN', 6, 1)
.addKeyboardButton('None of the above', 'OTHER', 6, 1)
.get()
];
});
Viber Template Message Builder checks if messages you are generating are following Viber Messenger guidelines and limits, in case they are not an error will be thrown.
Example:
Calling new viberTemplate.Text()
without text
will throw Text is required for the Viber Text template
error.
All errors that Claudia bot builder's viberTemplate
library is throwing can be found in the source code.
Errors will be logged in Cloud Watch log for your bot.