node-telegram-keyboard-wrapper-fix v2.0.3
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 -D
npm 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
- Visual Keyboards - .addRow() - .removeRow() - .emptyRow() - .popRow() - .rowLength() - .push() - .pop() - .reset() - .Getter .length()
- Inline Keyboard
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.
(new InlineKeyboard()).addRow(...keys) : this
(new ReplyKeyboard()).addRow(...keys) : this
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
keys | One Object per button | InlineKeyboardButton | false | - |
Returns the object itself for chaining;
.removeRow()
(new InlineKeyboard()).removeRow(index) : this
(new ReplyKeyboard()).removeRow(index) : 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()
(new InlineKeyboard()).emptyRow(index) : Number
(new ReplyKeyboard()).emptyRow(index) : 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()
(new InlineKeyboard()).popRow(index) : (InlineKeyboardButton | KeyboardButton | string)[]
(new ReplyKeyboard()).popRow(index) : (InlineKeyboardButton | KeyboardButton | string)[]
Returns:
Returns the popped out row (array of the above element).
Description:
Pops out the last row of the keyboard.
.rowLength()
(new InlineKeyboard()).rowLength(index) : Number
(new ReplyKeyboard()).rowLength(index) : 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()
(new InlineKeyboard()).push(index, ...elements) : Number;
(new ReplyKeyboard()).push(index, ...elements) : 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](https://core.telegram.org/bots/api#inlinekeyboardbutton) | false | - |
.pop()
(new InlineKeyboard()).pop(index) : InlineKeyboardButton | KeyboardButton | string
(new ReplyKeyboard()).pop(index) : 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()
(new InlineKeyboard()).reset()
(new ReplyKeyboard()).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
(new InlineKeyboard()).length : Number
(new ReplyKeyboard()).length : Number
Returns:
The amount rows in the keyboard.
Inline Keyboards
Inline keyboards do not extend Visual Keyboard class with any new method.
Constructor
new InlineKeyboard(oneKey?);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
oneKey | Fastest way to have one-button keyboard. | InlineKeyboardButton | true | - |
Reply Keyboards
// keep this always as valid
let replyKeyboard = new ReplyKeyboard();
Constructor
new ReplyKeyboard(oneKey?);
Parameters | Description | Type | Optional | Default value |
---|---|---|---|---|
oneKey | Useful for one-button only keyboards. | KeyboardButton | String | true | - |
.open()
replyKeyboard.open(options?);
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.close(options?);
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 valid
let forceReply = new ForceReply();
Constructor
new ForceReply();
@override .build()
forceReply.export(options?);
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.
(new InlineKeyboard()).build();
(new ReplyKeyboard()).build();
(new ForceReply()).build();
Returns:
A built structure conforming to Telegram keyboards.
.extract()
(new InlineKeyboard()).extract();
(new ReplyKeyboard()).extract();
(new ForceReply()).extract();
Returns:
Returns the content of reply_markup
.