1.0.1 • Published 7 years ago

telegram-keyboard-wrapper v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

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

Rows

.addRow()

Adds a new row with specified elements.

(new InlineKeyboard()).addRow(...keys);
ParametersDescriptionTypeOptionalDefault value
keysOne Object per buttonInlineKeyboardButtonfalsen/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);
ParametersDescriptionTypeOptionalDefault value
rowIndexThe row to be removed.Integerfalsen/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);
ParametersDescriptionTypeOptionalDefault value
rowIndexThe row to be emptied.Integerfalsen/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);
ParametersDescriptionTypeOptionalDefault value
rowIndexThe row to be emptied.Integerfalsen/a
ignoreLastRowIgnore last edited rowBooleantruetrue

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);
ParametersDescriptionTypeOptionalDefault value
rowIndexThe index of the row in which push.Integerfalsen/a
elementThe element to pushInlineKeyboardButtonfalsen/a
ignoreLastRowIgnore last edited rowBooleantruetrue

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);
ParametersDescriptionTypeOptionalDefault value
rowIndexThe row from which pop the last element.Integerfalsen/a

Returns the object itself for chaining;

Reply Keyboards
Constructor
new ReplyKeyboard(...keys);
ParametersDescriptionTypeOptionalDefault value
keysMultiple strings or objects for each buttonKeyboardButtonfalsen/a
.addRow()

Adds a new row with specified elements.

(new ReplyKeyboard()).addRow(...keys);
ParametersDescriptionTypeOptionalDefault value
keysOne Object per buttonInlineKeyboardButtonfalsen/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 });
ParametersDescriptionTypeOptionalDefault value
optionsOptions of the buttonObjecttrue{ 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 });
ParametersDescriptionTypeOptionalDefault value
optionsOptions of the buttonObjecttrue{ 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 });
ParametersDescriptionTypeOptionalDefault value
optionsOptions of the buttonObjecttrue{ selective: false }

See for more: ForceReply

Inherited methods and properties

.export()

Returns a keyboard structure based on the type.

.export(options = {}, override = "");
ParametersDescriptionTypeOptionalDefault value
optionsOptions to be added to reply_markup objectObjecttrue{ }
overrideAn override content to be sent in the structure\<Any>true""
.extract()

Returns the content of reply_markup.

.extract(from);
ParametersDescriptionTypeOptionalDefault value
fromThe structure obtained from .export()Objectfalsen/a

Throws error if in from there's not the property reply_markup.

Getter: length

Returns the length of the keyboard (rows)

1.0.1

7 years ago

1.0.0

7 years ago