2.0.3 • Published 5 years ago

node-telegram-keyboard-wrapper-fix v2.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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

Classes architecture:

This library is divided in the following class architecture:

npm.io

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
ParametersDescriptionTypeOptionalDefault value
keysOne Object per buttonInlineKeyboardButtonfalse-

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:

ParametersDescriptionTypeOptionalDefault value
indexThe row to be removed.Integerfalse-

.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:

ParametersDescriptionTypeOptionalDefault value
indexThe row to be emptied.Integerfalse-

.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:

ParametersDescriptionTypeOptionalDefault value
indexThe row to be emptied.Integerfalse-

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:

ParametersDescriptionTypeOptionalDefault value
indexThe index of the row in which push.Integerfalse-
... elementsThe elements to be pushedArray[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:

ParametersDescriptionTypeOptionalDefault value
indexThe row from which pop the last element.Integerfalse-

.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?);
ParametersDescriptionTypeOptionalDefault value
oneKeyFastest way to have one-button keyboard.InlineKeyboardButtontrue-

Reply Keyboards

// keep this always as valid
let replyKeyboard = new ReplyKeyboard();

Constructor

new ReplyKeyboard(oneKey?);
ParametersDescriptionTypeOptionalDefault value
oneKeyUseful for one-button only keyboards.KeyboardButton | Stringtrue-

.open()

replyKeyboard.open(options?);

Returns:

Keyboard structure to open a ReplyKeyboard.

Arguments:

ParametersDescriptionTypeOptionalDefault value
optionsOptions of the buttonObjecttrue{}
options.selectiveIf true, valid only for specific users (e.g. Mentioned), Users which replied the bot of original senderBooleantruefalse
options.one_time_keyboardHides the keyboard after the first usage.Booleantruefalse
options.resize_keyboardTells telegram client to use smaller buttonsBooleantruefalse

See more: Reply Keyboard Markup

.close()

replyKeyboard.close(options?);

Returns:

Keyboard structure to close a ReplyKeyboard.

Arguments:

ParametersDescriptionTypeOptionalDefault value
optionsOptions of the buttonObjecttrue{}
options.selectiveIf true, valid only for specific users (e.g. Mentioned), Users which replied the bot of original senderBooleantruefalse

See: Reply Keyboard Remove

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:

ParametersDescriptionTypeOptionalDefault value
optionsOptions of the buttonObjecttrue{}
options.selectiveIf true, valid only for specific users (e.g. Mentioned), Users which replied the bot of original senderBooleantruefalse

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.

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago