All Projects → alexandercerutti → node-telegram-keyboard-wrapper

alexandercerutti / node-telegram-keyboard-wrapper

Licence: other
A support to manage keyboards and force replies in Telegram via bots

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to node-telegram-keyboard-wrapper

formatbot1
Make instant view easily and fast, from any article on the internet in the best messenger ever Telegram
Stars: ✭ 127 (+296.88%)
Mutual labels:  telegram-bot, node-js
winston-telegram
A Telegram transport for winston
Stars: ✭ 28 (-12.5%)
Mutual labels:  telegram-bot, node-js
pastebin-bot
Advanced functional Pastebin Telegram Bot made using better-pastebin and TelegrafJS. This bot will help you to create pastes (Texts) on Pastebin which is a text cloud.
Stars: ✭ 16 (-50%)
Mutual labels:  telegram-bot
RSSnotifier
Node RSS reader telegram bot. Provides notification on queries-matching elements and supports multiple users.
Stars: ✭ 15 (-53.12%)
Mutual labels:  telegram-bot
TelegaGraph
TelegaGraph. Print messages from telegram to thermal printer
Stars: ✭ 23 (-28.12%)
Mutual labels:  telegram-bot
node-js-jwt-auth
Node.js Demo for Token Based Authentication (JWT) with MySQL database
Stars: ✭ 161 (+403.13%)
Mutual labels:  node-js
Click-Counter-Bot
A telegram bot module for how to count total clicks on button.
Stars: ✭ 23 (-28.12%)
Mutual labels:  telegram-bot
Bot-Telegram-BeMEAN
💣 Bot para o Telegram do grupo do Be MEAN
Stars: ✭ 76 (+137.5%)
Mutual labels:  telegram-bot
onetricks.net
(WIP) kayn-powered (typescript node.js) ReasonReact app presenting you a dashboard of high ELO one trick ponies in League of Legends
Stars: ✭ 13 (-59.37%)
Mutual labels:  node-js
Discord-Reposter
Bot for reposting Discord messages (work in progress)
Stars: ✭ 62 (+93.75%)
Mutual labels:  node-js
telegram-aqi-bot
Telegram AQI Bot based on nodejs
Stars: ✭ 31 (-3.12%)
Mutual labels:  telegram-bot
notion-help-bot
A Notion-backend Telegram bot, designed for Notion CN Community (https://t.me/Notionso).
Stars: ✭ 67 (+109.38%)
Mutual labels:  telegram-bot
timeoff-server
TimeOff is an application that allows companies' employees to set vacations before they begin taking their time off. Implemented in modern tech stack i.e. Node, Express, MongoDB.
Stars: ✭ 33 (+3.13%)
Mutual labels:  node-js
firefly-bot
Bot to quickly create transactions in Firefly III
Stars: ✭ 49 (+53.13%)
Mutual labels:  telegram-bot
digitalocean-helper-bot
用 telegram bot 管理 Digital Ocean 账号
Stars: ✭ 24 (-25%)
Mutual labels:  telegram-bot
LilSholex
A project containing web apps and Telegram API bots.
Stars: ✭ 32 (+0%)
Mutual labels:  telegram-bot
tinjecttelegram delphi
LMCODE
Stars: ✭ 37 (+15.63%)
Mutual labels:  telegram-bot
dictum
🤔 API to get access to the collection of the most inspiring expressions of mankind
Stars: ✭ 77 (+140.63%)
Mutual labels:  telegram-bot
spbu4u
Telegram bot Timetable for SPbU students @Spbu4UBot | Телеграм бот Расписание для студентов СПбГУ
Stars: ✭ 20 (-37.5%)
Mutual labels:  telegram-bot
crawley
Crawley the Telegram Beholder
Stars: ✭ 24 (-25%)
Mutual labels:  telegram-bot

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].