DEV Community

Néon Craftx
Néon Craftx

Posted on

🚀 Simplify Telegram Inline Keyboards with telegram-inline-keyboard-builder

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')
]));

Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode

Cleaner CodeChainable APISmart 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

Enter fullscreen mode Exit fullscreen mode

💡 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.inlineKeyboard structure 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();

Enter fullscreen mode Exit fullscreen mode

📚 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?

Enter fullscreen mode Exit fullscreen mode

Top comments (0)