Перейти к основному содержимому

grammy

Современная TypeScript-библиотека для Bot API. Минималистичная, быстрая, типизированная.

Установка

npm install grammy
# или
pnpm add grammy

Подключение к Telefon

import { Bot } from 'grammy';

const bot = new Bot(process.env.BOT_TOKEN!, {
client: { apiRoot: 'https://api.telefon.chat' }
});

bot.command('start', (ctx) => ctx.reply('Привет!'));
bot.on('message:text', (ctx) => ctx.reply(ctx.message.text));

bot.start();

Inline keyboard

import { InlineKeyboard } from 'grammy';

const kb = new InlineKeyboard()
.text('✅ Да', 'yes').text('❌ Нет', 'no').row()
.url('🌐 Сайт', 'https://example.com');

bot.command('vote', (ctx) => ctx.reply('Выбери:', { reply_markup: kb }));

bot.callbackQuery('yes', async (ctx) => {
await ctx.answerCallbackQuery({ text: 'Спасибо!' });
});

Conversation (FSM)

import { Bot, session } from 'grammy';
import { conversations, createConversation } from '@grammyjs/conversations';

bot.use(session({ initial: () => ({}) }));
bot.use(conversations());

async function register(conversation, ctx) {
await ctx.reply('Как тебя зовут?');
const { message: { text: name } } = await conversation.wait();
await ctx.reply(\`Привет, \${name}!\`);
}

bot.use(createConversation(register));
bot.command('register', (ctx) => ctx.conversation.enter('register'));

Документация

grammy.dev — туториал, гайды, плагины.