npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-telegram-keyboard-wrapper-fix

v2.0.3

Published

A support to create keyboards in Telegram via bots

Downloads

7

Readme

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:

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

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:

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