telegram-keyboard-wrapper v1.0.1
Node.JS Telegram Keyboard Wrapper
A support for telegram keyboards creation, both inline and reply.
This is a support wrapper created to simplify the creation of keyboards inside telegram bot written in Node.JS which used wrapper doesn't have already a support for managing keyboards in such dynamic way.
Tests for all methods are included.
npm install --only=dev
npm test
To install:
npm install telegram-keyboard-wrapper --save
Moreover, a test folder subfolder, which contains a test bot which uses yagop's node-telegram-bot-api to show how the library works, is available.
To use it, you have just to pass a bot token as a command line argument to node, like follows.
cd node-telegram-keyboard-wrapper
node test/bot 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
Then just type /replyKeyboard
(and answer or click to hide) /inlineKeyboard
(and click to trigger) 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
Inline Keyboards
All the methods are chainable.
Constructor
new InlineKeyboard(oneKey);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
oneKey | Fastest way to have one-button keyboard. | InlineKeyboardButton | true | n/a |
Rows
.addRow()
Adds a new row with specified elements.
(new InlineKeyboard()).addRow(...keys);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
keys | One Object per button | InlineKeyboardButton | false | n/a |
Returns the object itself for chaining;
.removeRow()
Removed an entire row of keys.
Please note that both rowIndex < 0
and rowIndex > rowQuantity
, will make the counter restart from their opposite bounds.
(new InlineKeyboard()).removeRow(rowIndex);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
rowIndex | The row to be removed. | Integer | false | n/a |
Returns the object itself for chaining;
.emptyRow()
Empty an entire row of keys but without removing the row.
Please note that both rowIndex < 0
and rowIndex > rowQuantity
, will make the counter restart from their opposite bounds.
(new InlineKeyboard()).emptyRow(rowIndex);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
rowIndex | The row to be emptied. | Integer | false | n/a |
Returns the object itself for chaining with lastRow
property to keep trace of which was the emptied row.
.popRow()
Pops the last row of the keyboard.
(new InlineKeyboard()).popRow(rowIndex);
Returns the object itself for chaining;
.rowLength()
Empty an entire row of keys but without removing the row.
Please note that both rowIndex < 0
and rowIndex > rowQuantity
, will make the counter restart from their opposite bounds.
(new InlineKeyboard()).rowLength(rowIndex, ignoreLastRow = true);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
rowIndex | The row to be emptied. | Integer | false | n/a |
ignoreLastRow | Ignore last edited row | Boolean | true | true |
Returns the amount of buttons in a row.
Buttons
.push()
Adds element
to the specified row.
Please note that both rowIndex < 0
and rowIndex > rowQuantity
, will make the counter restart from their opposite bounds.
(new InlineKeyboard()).push(rowIndex, element, ignoreLastRow = true);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
rowIndex | The index of the row in which push. | Integer | false | n/a |
element | The element to push | InlineKeyboardButton | false | n/a |
ignoreLastRow | Ignore last edited row | Boolean | true | true |
Returns the object itself for chaining;
.pop()
Pops out the last element of a row.
Please note that both rowIndex < 0
and rowIndex > rowQuantity
, will make the counter restart from their opposite bounds.
(new InlineKeyboard()).pop(rowIndex);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
rowIndex | The row from which pop the last element. | Integer | false | n/a |
Returns the object itself for chaining;
Reply Keyboards
Constructor
new ReplyKeyboard(...keys);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
keys | Multiple strings or objects for each button | KeyboardButton | false | n/a |
.addRow()
Adds a new row with specified elements.
(new ReplyKeyboard()).addRow(...keys);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
keys | One Object per button | InlineKeyboardButton | false | n/a |
Returns the object itself (chaining) with length
property for keyboard rows length.
.open()
Returns a keyboard structure to open a ReplyKeyboard.
(new ReplyKeyboard()).open(options = { selective: false, one_time_keyboard: false, resize_keyboard: false });
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
options | Options of the button | Object | true | { selective: false, one_time_keyboard: false, resize_keyboard: false } |
See for more: Reply Keyboard Markup
.close()
Returns a keyboard structure to close a ReplyKeyboard.
(new ReplyKeyboard()).close(options = { selective: false });
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
options | Options of the button | Object | true | { selective: false } |
See for more: Reply Keyboard Remove
Getter: getKeys
Returns an array containing the keys which compose the keyboard.
Force Reply
Constructor
new ForceReply();
@override .export()
Returns a keyboard structure for force reply.
(new ForceReply()).export(options = { selective: false });
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
options | Options of the button | Object | true | { selective: false } |
See for more: ForceReply
Inherited methods and properties
.export()
Returns a keyboard structure based on the type.
.export(options = {}, override = "");
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
options | Options to be added to reply_markup object | Object | true | { } |
override | An override content to be sent in the structure | \<Any> | true | "" |
.extract()
Returns the content of reply_markup
.
.extract(from);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
from | The structure obtained from .export() | Object | false | n/a |
Throws error if in from
there's not the property reply_markup
.
Getter: length
Returns the length of the keyboard (rows)