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

v0.2.0

Published

Twilight.js telegram-keyboards is an asynchronous library that allows you to generate dynamic and static keyboards and allows you to save the finished keyboard template in json and return the keyboard back from the json templater

Downloads

19

Readme

Logo

twilightjs

node-telegram-keyboards is an asynchronous library that allows you to generate dynamic and static keyboards and allows you to save the finished keyboard template in json and return the keyboard back from the json templater

Table of Contents

Features

  • Easy to generate keyboards of any complexity
  • Easily save, create and use keyboard templates
  • Supports the Promise API

Installing

Using npm:

$ npm i node-telegram-keyboards

Examples

import {KeyboardBuilder} from "node-telegram-keyboards";
import {KeyboardButtons} from "node-telegram-keyboards";
import {InlineKeyboardButtons} from "node-telegram-keyboards";

async function replyKeyboardStatic() {
    const keyboard = await KeyboardBuilder.keyboard().static()
        .buttons([
            KeyboardButtons.text("example"),
            KeyboardButtons.contact("example"),
            KeyboardButtons.location("example"),
            KeyboardButtons.poll("example", "quiz"),
            KeyboardButtons.webApp("example", "https://example.site/")
        ])
        .layout((data, template) => template.topToBottom())
        .build();
}

async function replyKeyboardDynamic() {
    const keyboard = await KeyboardBuilder.keyboard().dynamic()
        .data([1, 2, 3, 4, 5])
        .use(async (storage, data) => {
            storage.counter = 0;
        })
        .map(async (element, storage, positionIndex) => {
            if (positionIndex % 2 == 0) {
                storage.counter = + positionIndex;
                return KeyboardButtons.text(storage.counter);
            }
            storage.counter = + positionIndex;
            return KeyboardButtons.text(element);
        })
        .layout((data, template) => template.topToBottom())
        .build();
}

async function inlineKeyboardStatic() {
    const inlineKeyboard = await KeyboardBuilder.inlineKeyboard().static()
        .buttons([
           InlineKeyboardButtons.url("example", "https://example.site/"),
           InlineKeyboardButtons.callbackData("example", "exampleData"),
           InlineKeyboardButtons.webApp("example", "https://example.site/"),
           InlineKeyboardButtons.loginUrl("example", "https://example.site/"),
           InlineKeyboardButtons.switchInlineQuery("example", "query"),
           InlineKeyboardButtons.switchInlineQueryCurrentChat("example", "query"),
           InlineKeyboardButtons.callbackGame("example", "callbackGame"),
           InlineKeyboardButtons.pay("example")
        ])
        .layout((data, template) => template.topToBottom())
        .build();
}

async function inlineKeyboardDynamic() {
    const inlineKeyboard = await KeyboardBuilder.inlineKeyboard().dynamic()
        .data([1, 2, 3, 4, 5, 6, 7, 8])
        .use(async (storage, data) => {
            storage.counter = 0;
        })
        .map(async (element, storage, positionIndex) => {
            if (positionIndex % 2 == 0) {
                storage.counter = + positionIndex;
                return InlineKeyboardButtons.callbackData(element, storage.counter);
            }
            storage.counter = + positionIndex;
            return InlineKeyboardButtons.callbackData(element, positionIndex);
        })
        .layout((data, template) => template.topToBottom())
        .build();
}

API reference

KeyboardBuilder

| Method |Description| | -------- | -------------- | | static keyboard() |Returns a static or dynamic ReplyKeyboard keyboard selector| | static inlineKeyboard() |Returns a static or dynamic InlineKeyboard keyboard selector| | static assign(type, keyboards) |Connects all keyboards into one| | static save(keyboard) |Converts ReplyKeyboard or InlineKeyboard objects to JSON object suitable for telegram| | static async saveToJSON(keyboard, filename) |Converts and saves ReplyKeyboard or InlineKeyboard objects to a JSON file that can be used as ready-made keyboard template| | static template(json) |Based on the keyboard template, builds a new ReplyKeyboard or InlineKeyboard object| | static async templateFromJSON(pathToJSON) |Reads a JSON file and creates a new ReplyKeyboard or InlineKeyboard object based on the keyboard template|

Static and Dynamic Keyboard or InlineKeyboard reference

Common methods that both keyboards have.

In static implementation:

| Method| Parameters | Description | | ------ | ---------- | ------------------ | | buttons(buttons) |buttons: array, array of KeyboardButtons orInlineKeyboardButtonss|An array of buttons to which you need to pass KeyboardButtons orInlineKeyboardButtonss objects, depending on which keyboard you are building Keyboard or InlineKeyboard|

In dynamic implementation:

| Method| Parameters | Description | | --------- | --------------------- | -------------------- | | use(callbackfn) |async/sync () => array|It takes as input a callback function that is called once and, depending on what conditions it processes, should return an array of KeyboardButtons or InlineKeyboardButtons| | data(array, start, end) |array: array, start: number, end: number |It takes an array of data as input, which it then converts to KeyboardButtons orInlineKeyboardButtons, the start and end parameters are needed to indicate from which position to take elements for buttons, by default the entire array is taken, they can be omitted | | storage(callbackfn) |async/sync (storage: object, data: array) => storage|It accepts a callback function as input, which initializes the store, which is passed to the callback function in the map method, in this store you can pass data between KeyboardButtons orInlineKeyboardButtons| | map(callbackfn) |async/sync (element: the type is equal to the type of the element retrieved from the data , storage: object, positionIndex: number) => KeyboardButtons orInlineKeyboardButtons |It accepts a callback function as input, which is called for each element from the data array and converts it to KeyboardButtons orInlineKeyboardButtons |

For all implementations:

| Method| Parameters | Description | | --------- | ---------------------- | ------------------------------------------- | | layout(callbackfn) |async/sync (data: array, template: TemplateLayout) => array|It takes a callback function as input, which should return an array with markup, the markup looks like this, you specify how many buttons will be in a row and how many lines there will be. For example: [5] - 5 buttons will be placed in a row, [1, 1, 1, 1, 1] - there will be 5 buttons in 5 lines, [2, 3] - buttons will be located on two lines, on the first 2, on the second 3 respectively. *In the markup, you must specify the exact number of buttons or specify the markup calculation algorithm in the callback function, otherwise there will be an error| | async build() |-|Returns a finished keyboard that can be sent to Telegram|

Keyboard

KeyboardButtons

InlineKeyboard

###InlineKeyboardButtonss

TemplateLayout

Tech Stack

Node.js

Authors

Support