Node telegram keyboard wrapper
A support for telegram keyboards management, both inline and reply, and forceReply
This libray aims to provide a set of methods and classes to handle keyboards and force replies in node.js-written Telegram bots.
Built upon yagop's node-telegram-bot-api but can virtually work with any node.js telegram bot api wrapper.
Installation
From NPM:
npm install -s node-telegram-keyboard-wrapper
Downloading from Github, won't give the compiled version, so you'll have to do it by yourself.
npm build
Tests for methods used by ReplyKeyboard and InlineKeyboard are included.
# Installing dev dependencies npm install -Dnpm test
In examples folder, an example bot is available. It requires a bot token to be passed as argument.
npm run example -- 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
Then just type /replyKeyboard
(and answer or click to hide), /inlineKeyboard
(and click to trigger) or /forceReply
in your Telegram client to see the wrapper in action.
If you have any issue, suggestion of what else, feel free to open a topic in issues. 😉
API Reference
Classes architecture:
This library is divided in the following class architecture:
Visual Keyboard
This class get extended by both InlineKeyboards and ReplyKeyboards and extend ReplyMarkup class. Therefore, the methods inserted in here are valid for both.
Rows methods
.addRow()
Adds a new row with specified elements.
: this : this
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
keys | One Object per button | InlineKeyboardButton | false | - |
Returns the object itself for chaining;
.removeRow()
: this : this
Returns:
The object itself for chaining;
Description:
Removed a specific row.
Both row indexes index < 0
and index > rowQuantity
, will make the counter restart from their opposite bound.
Arguments:
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
index | The row to be removed. | Integer | false | - |
.emptyRow()
: Number : Number
Returns:
The index of the emptied row.
Description:
Empty an entire row of keys but without removing the row.
Please note that both index < 0
and index > rowQuantity
, will make the counter restart from their opposite bounds.
Arguments:
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
index | The row to be emptied. | Integer | false | - |
.popRow()
: InlineKeyboardButton | KeyboardButton | string : InlineKeyboardButton | KeyboardButton | string
Returns:
Returns the popped out row (array of the above element).
Description:
Pops out the last row of the keyboard.
.rowLength()
: Number : Number
Returns:
The length of a specific row.
Description:
Both row indexes index < 0
and index > rowQuantity
, will make the counter restart from their opposite bounds.
Arguments:
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
index | The row to be emptied. | Integer | false | - |
Returns the amount of buttons in a row.
Buttons operations
.push()
: Number; : Number;
Returns:
The new length of the current row.
Description:
Adds elements
to the specified row.
Both row indexes index < 0
and index > rowQuantity
, will make the counter restart from their opposite bounds.
Arguments:
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
index | The index of the row in which push. | Integer | false | - |
... elements | The elements to be pushed | Array<InlineKeyboardButton> | false | - |
.pop()
: InlineKeyboardButton | KeyboardButton | string : InlineKeyboardButton | KeyboardButton | string
Returns:
The popped out element.
Description:
Pops out the last element of a row.
Both row indexes index < 0
and index > rowQuantity
, will make the counter restart from their opposite bounds.
Arguments:
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
index | The row from which pop the last element. | Integer | false | - |
.reset()
Description:
Wipes out the whole content. Probabily the most useless method. I mean: if you want to create a new keyboard, you don't wipe out your old, but create a brand new one.
Getter .length
length : Numberlength : Number
Returns:
The amount rows in the keyboard.
Inline Keyboards
Inline keyboards do not extend Visual Keyboard class with any new method.
Constructor
oneKey?);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
oneKey | Fastest way to have one-button keyboard. | InlineKeyboardButton | true | - |
Reply Keyboards
// keep this always as validlet replyKeyboard = ;
Constructor
oneKey?);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
oneKey | Useful for one-button only keyboards. | KeyboardButton | String | true | - |
.open()
replyKeyboard
Returns:
Keyboard structure to open a ReplyKeyboard.
Arguments:
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
options | Options of the button | Object | true | {} |
options.selective | If true, valid only for specific users (e.g. Mentioned), Users which replied the bot of original sender | Boolean | true | false |
options.one_time_keyboard | Hides the keyboard after the first usage. | Boolean | true | false |
options.resize_keyboard | Tells telegram client to use smaller buttons | Boolean | true | false |
See more: Reply Keyboard Markup
.close()
replyKeyboard
Returns:
Keyboard structure to close a ReplyKeyboard.
Arguments:
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
options | Options of the button | Object | true | {} |
options.selective | If true, valid only for specific users (e.g. Mentioned), Users which replied the bot of original sender | Boolean | true | false |
Force Reply
// keep this always as validlet forceReply = ;
Constructor
;
@override .build()
forceReply
Returns:
Returns a keyboard structure for force reply.
Arguments:
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
options | Options of the button | Object | true | {} |
options.selective | If true, valid only for specific users (e.g. Mentioned), Users which replied the bot of original sender | Boolean | true | false |
See: ForceReply
Inherited methods and properties
These methods are inherited from ReplyMarkup class, which gets inherited by both Visual Keyboards and ForceReply.
.build()
Returns a keyboard structure based on the type.
;;;
Returns:
A built structure conforming to Telegram keyboards.
.extract()
;;;
Returns:
Returns the content of reply_markup
.