1.1.0 • Published 1 year ago

@loskir/grammy-markup v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Markup plugin for grammY

This plugin provides telegraf-like keyboard and inline keyboard builders.

Motivation

The official plugin provides an imperative API, which I don't really like. This plugin, on the other hand, provides declarative helper functions inspired by telegraf.

See the comparison:

new InlineKeyboard([
  [IButton.text("Get random music", "random")],
  [IButton.switchInline("Send music to friends")],
])

vs

new InlineKeyboard()
  .text("Get random music", "random").row()
  .switchInline("Send music to friends")

The declarative approach is especially useful when you want to represent dynamic data in the menu (for example, obtained from the database):

const inlineKeyboard = new InlineKeyboard(items.map(
  (item) => [IButton.text(item.name, item.id)]
))

vs

const inlineKeyboard = new InlineKeyboard()
for (const item of items) {
  inlineKeyboard.text(item.name, item.id).row()
}

Also it's easy to use all sorts of array utilities with declarative builder, like chunk:

const inlineKeyboard = new InlineKeyboard(chunk(
  items.map((item) => IButton.text(item.name, item.id))
  3,
))

Installation

Yarn

yarn add @loskir/grammy-markup

NPM

npm i --save @loskir/grammy-markup

Usage

Deno

import {
    IButton,
    Button,
} from "https://github.com/Loskir/grammy-markup/raw/main/src/index.ts"

Typescript

import {
    IButton,
    Button,
} from "@loskir/grammy-markup"

Javascript

const {
    IButton,
    Button,
} = require("@loskir/grammy-markup")

Examples

IButton

const inlineKeyboard = new InlineKeyboard([
    [IButton.text('text', 'callback_data')],
    [IButton.url('text', 'https://grammy.dev')],
    [IButton.webApp('text', 'https://grammy.dev?webApp')],
    [IButton.login('text', 'https://grammy.dev?login')],
    [
        IButton.switchInline('text', 'query'),
        IButton.switchInlineCurrent('text', 'query'),
    ],
    [IButton.game('text')],
    [IButton.pay('text')],
])
// ctx.reply('inline keyboard', {reply_markup: inlineKeyboard})

Button

const keyboard = new Keyboard([
    [Button.text('text')],
    [Button.requestContact('text')],
    [Button.requestLocation('text')],
    [Button.requestPoll('text', 'quiz')],
    [Button.webApp('text', 'https://grammy.dev?webApp')],
])
// ctx.reply('keyboard', {reply_markup: keyboard})