Building Telegram bots is fun, but creating inline keyboards can be repetitive, verbose, and messy.
telegram-inline-keyboard-builder is a lightweight, library-agnostic package that allows you to build inline buttons easily for Telegraf, node-telegram-bot-api, Aiogram, or Pyrogram.
๐ Why this package?
Creating inline buttons usually requires writing repetitive, nested code.
The "Old" Way (Verbose):
ctx.reply('Choose an option', Markup.inlineKeyboard([
Markup.button.callback('Yes', 'YES'),
Markup.button.callback('No', 'NO')
]));
The "Builder" Way (Fluent & Clean):
import { InlineKeyboardTelegraf } from 'telegram-inline-keyboard-builder';
const keyboard = new InlineKeyboardTelegraf({ buttonsPerRow: 2 })
.addCallbackButton('โ
OK', 'OK_ACTION')
.addUrlButton('๐ Visit site', '[https://example.com](https://example.com)')
.newRow()
.addCallbackButton('โ Cancel', 'CANCEL_ACTION')
.build();
ctx.reply('Welcome ๐\nChoose an action:', keyboard);
โ Cleaner Code โ Chainable API โ Smart Row Management & Auto-wrap
๐ ๏ธ Key Features
-
Fluent API: Chainable calls like
.addCallbackButton().addUrlButton().newRow()for maximum readability. - Library Agnostic: The core logic is independent; adapters handle the specific integration for different Telegram libraries.
- Ready-to-use Adapters: Out-of-the-box support for Telegraf and Node Telegram Bot API.
- Flexible Configuration: Control the number of buttons per row or let the builder handle auto-wrapping based on character count.
๐ฆ Installation
Install the package via npm:
npm install telegram-inline-keyboard-builder
๐ก How it works
The package uses a core builder (InlineKeyboardBuilder) that handles the layout logic, row management, and button types.
Adapters then convert this core output into library-specific formats:
-
Markup.inlineKeyboardstructure for Telegraf. -
{ reply_markup: { inline_keyboard: [...] } }for node-telegram-bot-api.
Quick Example (Telegraf)
import { InlineKeyboardTelegraf } from 'telegram-inline-keyboard-builder';
// Create a 2-column keyboard
const myKeyboard = new InlineKeyboardTelegraf({ buttonsPerRow: 2 })
.addCallbackButton('Option 1', 'ID_1')
.addCallbackButton('Option 2', 'ID_2')
.build();
bot.launch();
๐ Examples & Documentation
Check the GitHub Repository for:
- Detailed usage examples.
- Node.js and Python adapters.
- Custom buttons, Pay buttons, and Login URLs.
telegram-inline-keyboard-builder is perfect if you want less boilerplate and more focus on your bot logic.
Happy coding! ๐
---
### Would you like me to create a `CONTRIBUTING.md` file as well to encourage others to build adapters for more libraries?
Top comments (0)