vk-bot-sdk
Version:
NodeJS Lib for VK API
87 lines (72 loc) • 1.53 kB
JavaScript
var Button = require( './button' );
var TextButton = require( './text-button' );
class Keyboard {
/**
* @param {Object} options
*/
constructor( { oneTime = false } = {}) {
this.oneTime = oneTime;
this.buttons = [];
}
/**
* Return keyboard
*
* @param {Array} rows
* @param {Object} options
*
* @return {Keyboard}
*/
static keyboard( rows, options ) {
if ( rows.length > 10 ) {
return console.error( 'Max count of rows 10' );
}
const keyboard = new Keyboard( options );
for ( const buttons of rows ) {
keyboard.addButtons(buttons);
}
return keyboard;
}
/**
* Returns the text button
*
* @param {Object} options
*
* @return {TextButton}
*/
static textButton(options) {
return new TextButton(options);
}
/**
* Add button
*
* @param {Button[]} buttons
*/
addButtons( buttons ) {
if (!Array.isArray(buttons)) {
buttons = [buttons];
}
if (buttons.length > 4) {
return console.error( 'Max count of columns 4' );
}
this.buttons.push( buttons );
return this;
}
/**
* Returns a string to keyboard a VK
*
* @return {string}
*/
toString() {
const buttons = this.buttons.map((buttonRow) => {
if (!Array.isArray(buttonRow)) {
return buttonRow.toJSON();
}
return buttonRow.map( button => button.toJSON() );
});
return JSON.stringify({
one_time: this.oneTime,
buttons
});
}
}
module.exports = Keyboard;