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

v3.1.0

Published

A support to create keyboards in Telegram via bots

Downloads

336

Readme

Node telegram keyboard wrapper

This library aims to provide a set of classes to improve the creation of keyboards and setup for force-reply in Telegram bots.

Built upon yagop's node-telegram-bot-api, it can work with any Node.js Bot Api wrapper, as it exports Telegram Bot APIs-compliant JSON structures.

⚠ v3.0.0 of this library is a major rewrite that is not retro-compatible. It doesn't export anymore an object with reply_markup. From now on, it will export just the content for reply_markup, which will vary from keyboard to keyboard. ⚠


Architecture

The philosophy behind this library, since v3, is to make it easier, through a series of state-less classes, to create the JSON structures to represent Rows, Buttons and the whole keyboards.

To achieve this, both ReplyKeyboard and InlineKeyboard classes and Row class extend the native Array interface.

Therefore you can do every operation you want just like you were acting on Arrays.


Looking for previous version?

I hope you don't, but if you really need it, here it is.


Install

$ npm install --save node-telegram-keyboard-wrapper

Example

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.


API Reference

ForceReply

static .getMarkup

ForceReply.getMarkup(selective: boolean = false): Object;

ForceReply.getMarkup();
ForceReply.getMarkup(true);

Use this method to export the structure to be sent to reply_markup.

Arguments:

| Argument | Type | Required | Default Value | | --------- | :-----: | :------: | :-----------: | | selective | boolean | false | false |


Row (extends Array.prototype)

new Row<R extends InlineKeyboardButton | KeyboardButton>(...values: R[]): Row;

Use this class to define a row to which buttons can be appended to.

Push this class into an InlineKeyboard or a ReplyKeyboard to let them create the structure.

This class extends native arrays and, therefore, allows every operation you can perform on Arrays to be performed on this.

Arguments:

Inherited from Array's constructor.


.clone

Row.prototype.clone(): Row<R>;

const row = new Row<KeyboardButton>();
const clone = row.clone();

row !== clone; // true;

Creates a copy of the row and clones all its children / buttons.


InlineKeyboard (extends Array.prototype)

new InlineKeyboard(...values: Row<InlineKeyboardButton>[]): InlineKeyboard;

Use this class to create a container for InlineKeyboards.

This class extends the native Array interface, therefore every operation you can perform on Arrays is allowed to be performed on this.

Arguments:

Inherited from Array's constructor.


.getMarkup

InlineKeyboard.prototype.getMarkup(): Object;

const keyboard = new InlineKeyboard();
keyboard.getMarkup();

Use this method to export the structure to be sent to reply_markup.

Throws if no rows got pushed in the object.

Arguments:

none.


.clone

InlineKeyboard.prototype.clone(): InlineKeyboard;

const keyboard = new InlineKeyboard();
const clone = row.clone();

keyboard !== clone; // true;

Creates a copy of the keyboard, a copy of all of its row and a clone of all of their children / buttons.


InlineKeyboardButton

new InlineKeyboardButton<S extends string>(text: string, exclusiveKey: S, exclusiveValue: T[S]): InlineKeyboardButton;

Use this method to create a button to be pushed in a Row. As per the Telegram Bot API Documentation, each InlineKeyboardButton must have only one of the optional properties.

Arguments:

| Argument | Type | Required | Default Value | Description | | -------------- | :--------------: | :------: | :-----------: | ---------------------------------------------------------- | | text | string | true | - | The visual string to be shown on the button. | | exclusiveKey | S extends string | true | - | The required key for this button. | | exclusiveValue | T[S] | true | - | the value for exclusiveKey (it differs from key to key). |

For the valid values of exclusiveKey and exclusiveValue refer to InlineKeyboardButton;

Example:

const row = new Row<InlineKeyboardButton>();

row.push(
	new InlineKeyboardButton("My text", "url", "https://localhost:8080/"),
	new InlineKeyboardButton(
		"My text 2",
		"callback_data",
		"any data between 1 and 64 bytes"
	)
);

.clone

InlineKeyboardButton.prototype.clone(): InlineKeyboardButton;

const button = new InlineKeyboardButton();
const clone = button.clone();

button !== clone; // true;

Creates a copy of the button and objects inside it.


ReplyKeyboard (extends Array.prototype)

new ReplyKeyboard(...values: Row<KeyboardButton>[]): ReplyKeyboard;

Use this class to create a new keyboard that is going to show up under the text area in your Telegram client.

This class extends the native Array interface, therefore every operation you can perform on Arrays is allowed to be performed on this.

Arguments:

Inherited from Array's constructor.


.getMarkup

ReplyKeyboard.prototype.getMarkup(): Object;

const keyboard = new ReplyKeyboard();
keyboard.getMarkup();

Use this method to export the structure to be sent to reply_markup for opening the keyboard.

Throws if no rows got pushed in the object.

Arguments:

| Argument | Type | Required | Default Value | | ------------------------- | :-----: | :------: | :-----------: | | options | Object | false | {} | | options.resize_keyboard | boolean | false | undefined | | options.one_time_keyboard | boolean | false | undefined | | options.selective | boolean | false | undefined |

This list might get outdated. The arguments are used as they are passed. Refer to ReplyKeyboardMarkup for the complete list, eventually.


.remove

ReplyKeyboard.prototype.remove(): Object;

const keyboard = new ReplyKeyboard();
keyboard.remove();

Use this method to export the structure to be sent to reply_markup for closing definitely the keyboard.

Arguments:

| Argument | Type | Required | Default Value | | --------- | :-----: | :------: | :-----------: | | selective | boolean | false | false |


.clone

ReplyKeyboard.prototype.clone(): ReplyKeyboard;

const keyboard = new ReplyKeyboard();
const clone = keyboard.clone();

keyboard !== clone; // true;

Creates a copy of the keyboard, a copy of all of its row and a clone of all of their children / buttons.


KeyboardButton

new KeyboardButton<S extends string>(text: string, options: Options): KeyboardButton;

Use this method to create a button to be pushed in a Row.

Arguments:

| Argument | Type | Required | Default Value | Description | | ------------------------- | :-----------------: | :------: | :-----------: | -------------------------------------------- | | text | string | true | - | The visual string to be shown on the button. | | options | Object | false | {} | The options for this button. | | options.request_contact | boolean | false | undefined | - | | options.request_location | boolean | false | undefined | - | | options.request_poll | Object | false | undefined | - | | options.request_poll.type | "quiz" | "regular" | true | undefined | - |

Example:

const row = new Row<KeyboardButton>();

row.push(
	new KeyboardButton("My text 1", {
		request_location: true,
		request_contact: false,
	})
);

.clone

InlineKeyboardButton.prototype.clone(): InlineKeyboardButton;

const button = new InlineKeyboardButton();
const clone = button.clone();

button !== clone; // true;

Creates a copy of the button and objects inside it.


Testing

Tests are build upon Jasmine Unit suite. Run these commands to test changes after have cloned the repository:

$ npm install

$ npm run build # to build from Typescript
$ npm run build:spec # to build tests
$ npm test # to both build tests and run them

Any contribution, is welcome. Made with ❤️ in Italy.