Giter Club home page Giter Club logo

node-telegram-keyboard-wrapper's Introduction

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.

node-telegram-keyboard-wrapper's People

Contributors

alexandercerutti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-telegram-keyboard-wrapper's Issues

After closing reply keyboard can't to open it again

toJSON: [Function: responseToJSON], caseless: Caseless { dict: [Object] }, body: { ok: false, error_code: 400, description: 'Bad Request: field "keyboard" of the ReplyKeyboardMarkup should be an Array' } } }
That's the last lines from long console outputting

(new kb.InlineKeyboard()).push() - TypeError [ERR_INVALID_ARG_TYPE]

Hello, I'm getting this error the very first time I try to push() something in my code.
If I try catch, and then try to push() again, it works.
I have no idea why.

(new kb.InlineKeyboard()).push(0, { text: 'foo', callback_data: 'bar' });

Sat, 07 Dec 2019 23:30:25 GMT node-telegram-bot-api deprecated Automatic enabling of cancellation of promises is deprecated.
In the future, you will have to enable it yourself.
See https://github.com/yagop/node-telegram-bot-api/issues/319. at internal\modules\cjs\loader.js:688:30

Warning: Push method does not return `this` anymore. Unexpected behaviour may happen. has been deprecated.
Debugger attached.
events.js:167
      throw er; // Unhandled 'error' event
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type undefined
    at validChunk (_stream_writable.js:258:10)
    at Socket.Writable.write (_stream_writable.js:292:21)
    at nodeDeprecate (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\node_modules\deprecate\index.js:22:22)
    at deprecate (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\node_modules\deprecate\index.js:11:3)
    at InlineKeyboard.push (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\node_modules\node-telegram-keyboard-wrapper\src\reply_markup.js:95:9)
    at Object.<anonymous> (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\broda-bot\broda-bot.js:10:27)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\index.js:3:11)
    at Module._compile (internal/modules/cjs/loader.js:685:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
Emitted 'error' event at:
    at validChunk (_stream_writable.js:261:12)
    at Socket.Writable.write (_stream_writable.js:292:21)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
Waiting for the debugger to disconnect...
NodeError: The "chunk" argument must be one of type string or Buffer. Received type undefined
    at validChunk (_stream_writable.js:258:10)
    at Socket.Writable.write (_stream_writable.js:292:21)
    at nodeDeprecate (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\node_modules\deprecate\index.js:22:22)
    at deprecate (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\node_modules\deprecate\index.js:11:3)
    at InlineKeyboard.push (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\node_modules\node-telegram-keyboard-wrapper\src\reply_markup.js:95:9)
    at Object.<anonymous> (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\broda-bot\broda-bot.js:10:27)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (c:\Users\fabri\Desktop\projects\personal\gado-bot-telegram\index.js:3:11)
    at Module._compile (internal/modules/cjs/loader.js:685:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
Emitted 'error' event at:
    at validChunk (_stream_writable.js:261:12)
    at Socket.Writable.write (_stream_writable.js:292:21)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

Handling multiple chats?

Thanks so much for this package! It's saved me :)

If I had two chats going on at the same time and I wanted to close a reply keyboard for just one of them, how could I do that?

Thanks

Inline Keyboard Paginate

Paginate Keyboard

Hello
if the inline keyboard has a function to paginate keyboard buttons is good
if you can please add this option
this option is a useful and good option for too many keyboard buttons
I have 60 buttons and I need to paginate this keyboard if I add this option to my bot I share code to you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.