Skip to content

Обзор

@gramio/keyboards - это встроенный пакет GramIO. Вы также можете использовать его вне этого фреймворка, так как он не зависит от него.

Смотрите также API Reference.

Установка (не требуется для пользователей GramIO)

bash
npm install @gramio/keyboards
bash
yarn add @gramio/keyboards
bash
pnpm add @gramio/keyboards
bash
bun install @gramio/keyboards

Использование

ts
import { 
Keyboard
} from "gramio";
const
keyboard
= new
Keyboard
()
.
text
("первая строка")
.
row
()
.
text
("вторая строка");
ts
import { 
Keyboard
} from "@gramio/keyboards";
const
keyboard
= new
Keyboard
()
.
text
("первая строка")
.
row
()
.
text
("вторая строка")
.
build
();

Отправка через GramIO

ts
import { Bot, Keyboard } from "gramio"; // импорт из пакета GramIO!!

const bot = new Bot(process.env.BOT_TOKEN as string);

const data = ["Apple", "Realme", "Tesla", "Xiaomi"];

bot.on("message", (ctx) => {
    return ctx.send("тест", {
        reply_markup: new Keyboard()
            .columns(1)
            .text("простая клавиатура")
            .add(...data.map((x) => Keyboard.text(x)))
            .filter(({ button }) => button.text !== "Tesla"),
    });
});

bot.start();

Отправка через Grammy

ts
import { Keyboard } from "@gramio/keyboards";
import { Bot } from "grammy";

const bot = new Bot(process.env.BOT_TOKEN as string);

const data = ["Apple", "Realme", "Tesla", "Xiaomi"];

bot.on("message", (ctx) => {
    return ctx.reply("тест", {
        reply_markup: new Keyboard()
            .columns(1)
            .text("простая клавиатура")
            .add(...data.map((x) => Keyboard.text(x)))
            .filter(({ button }) => button.text !== "Tesla")
            .build(),
    });
});

bot.start();

Отправка через Telegraf

> `Telegraf` не поддерживает последнюю версию Bot API

ts
import { Keyboard } from "@gramio/keyboards";
import { Telegraf } from "telegraf";

const bot = new Telegraf(process.env.BOT_TOKEN as string);

const data = ["Apple", "Realme", "Tesla", "Xiaomi"];

bot.on("message", (ctx) => {
    return ctx.reply("тест", {
        reply_markup: new Keyboard()
            .columns(1)
            .text("простая клавиатура")
            .add(...data.map((x) => Keyboard.text(x)))
            .filter(({ button }) => button.text !== "Tesla")
            .build(),
    });
});

bot.launch();

Отправка через node-telegram-bot-api

> `node-telegram-bot-api` не поддерживает последнюю версию Bot API, и типы написаны плохо, поэтому типы могут не совпадать

ts
import { Keyboard } from "@gramio/keyboards";
import TelegramBot from "node-telegram-bot-api";

const bot = new TelegramBot(process.env.TOKEN as string, { polling: true });

const data = ["Apple", "Realme", "Tesla", "Xiaomi"];

bot.on("message", (msg) => {
    return bot.sendMessage(msg.chat.id, "тест", {
        reply_markup: new Keyboard()
            .columns(1)
            .text("простая клавиатура")
            .add(...data.map((x) => Keyboard.text(x)))
            .filter(({ button }) => button.text !== "Tesla")
            .build(),
    });
});

Отправка через puregram

> `puregram` не поддерживает последнюю версию Bot API

ts
import { Telegram } from "puregram";
import { Keyboard } from "@gramio/keyboards";

const bot = new Telegram({
    token: process.env.TOKEN as string,
});

const data = ["Apple", "Realme", "Tesla", "Xiaomi"];

bot.on("message", (ctx) => {
    return ctx.send("тест", {
        reply_markup: new Keyboard()
            .columns(1)
            .text("простая клавиатура")
            .add(...data.map((x) => Keyboard.text(x)))
            .filter(({ button }) => button.text !== "Tesla")
            .build(),
    });
});

bot.updates.startPolling();

Результат

json
{
    "keyboard": [
        [
            {
                "text": "простая клавиатура"
            }
        ],
        [
            {
                "text": "Apple"
            }
        ],
        [
            {
                "text": "Realme"
            }
        ],
        [
            {
                "text": "Xiaomi"
            }
        ]
    ],
    "one_time_keyboard": false,
    "is_persistent": false,
    "selective": false,
    "resize_keyboard": true
}

image