Universal inline keyboard builder for Telegram APIs
npm install telegram-inline-keyboard-builder!Logo    !Telegram
> Version 2 removes adapters and focuses on a single universal output:
> valid inline_keyboard JSON as expected by Telegram API.
---
- Produces pure Telegram inline keyboard JSON
- Auto-wrap & row control - Works with any Telegram framework
- Zero abstraction leak
---
``bash `
npm install telegram-inline-keyboard-builder`importation
js`
import { InlineKeyboardBuilder } from "telegram-inline-keyboard-builder";
Telegram inline keyboards follow one universal schema.
This builder:
* generates the keyboard directly in Telegram format
* lets you pass the result to any Telegram library
`js`
{ reply_markup: { inline_keyboard: [...] } }
- No adapters.
- No wrappers.
- No framework coupling.
`js
new InlineKeyboardBuilder({ buttonsPerRow = 2, autoWrapMaxChars = 0 })
//Chainable Methods
.addCallbackButton(text, callback_data, hide = false)
.addUrlButton(text, url, hide = false)
.addPayButton(text, options = {})
.addCustomButton(buttonObject)
.addButtons(config)
.setButtonsPerRow(n)
.setAutoWrapMaxChars(n)
.newRow()
// build
.build()
const keyboard = builder.build();
// Always returns:
{ reply_markup: { inline_keyboard: [...] } }
`
Fully compliant with Telegram Bot API.
`js
import { Telegraf } from "telegraf";
import { InlineKeyboardBuilder } from "telegram-inline-keyboard-builder";
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.start(ctx => {
const keyboard = new InlineKeyboardBuilder({ buttonsPerRow: 2, autoWrapMaxChars: 24 })
.addCallbackButton("β
OK", "OK_ACTION")
.addUrlButton("π Website", "https://example.com")
.newRow()
.addCallbackButton("β Cancel", "CANCEL_ACTION")
.build();
ctx.reply("Welcome π\nChoose an action:", keyboard); });
bot.launch();
`Usage Example (node-telegram-bot-api)
`js
import TelegramBot from "node-telegram-bot-api";
import { InlineKeyboardBuilder } from "telegram-inline-keyboard-builder";
const bot = new TelegramBot(TOKEN, { polling: true });
bot.onText(/\/start/, msg => {
const keyboard = new InlineKeyboardBuilder()
.addCallbackButton("OK", "OK")
.addUrlButton("Site", "https://example.com")
.build();
bot.sendMessage(msg.chat.id, "Hello", keyboard); });
`
> [!WARNING]
> Payment buttons must only be used with:
- sendInvoice
- replyWithInvoice
They must be hidden in normal messages.
`js`
.addPayButton("Pay now");
Using a visible payment button outside invoices will cause Telegram API errors.
Telegram API error
Make sure the keyboard object is passed directly:
`js
const keyboard = new InlineKeyboardBuilder(1)
.addCallbackButton("Setting","show_setting")
.build()
// telegraf
ctx.reply("Text", keyboard);
// node telegram bot api
bot.sendMessage(chatId, "Text", keyboard);
// CORRECT β
// OR if you want to include it in the options
const keyboard = new InlineKeyboardBuilder(1)
.addCallbackButton("Setting","show_setting")
.build()
// telegraf
ctx.reply("Text", {
reply_markup: keyboard.reply_makup, // inline keyboard
parse_mode: "HTML",
// ...
});
// node telegram bot api
bot.sendMessage(chatId, "Text", {
reply_markup: keyboard.reply_makup, // inline keyboard
parse_mode: "HTML",
// ...
);
`Migration to V2
- V1: The inline keyboard builder used adapters for each new API, resulting in code that was unmaintainable in case of updates.
- V2: Here we simply construct an object valid for all types of APIs without adapting it.
This project is maintained in my free time.
If it helped you, consider supporting it with a crypto donation β€οΈ
It helps me maintain and improve the project.
You can send donations to the following addresses:
| Crypto | Address |
|--------|---------|
| USDT (TRC20) | 0x607c1430601989d43c9CD2eeD9E516663e0BdD1F |0x607c1430601989d43c9CD2eeD9E516663e0BdD1F
| USDC (Polygon/ETH) | |0x607c1430601989d43c9CD2eeD9E516663e0BdD1F
| Ethereum (ETH) | |bc1qmysepz6eerz2mqyx5dd0yy87c3gk6hccwla5x2
| Bitcoin (BTC) | |TE9RiTaDpx7DGZzCMw7qds51nzszKiyeR8
| Tron (TRX) | |UQA1NPW4GqgIVa9R6lebN_0v64Q-Sz_nHrmK9LCk-FfdjVOH` |
| TON |
USDT (TRC20)
!USDT TRC20 QR
USDC
!USDC QR
Ethereum (ETH)
!ETH QR
Bitcoin (BTC)
!BTC QR
Tron (TRX)
!TRX QR
TON
!TON QR
Contributions are welcome β€οΈ
Please open an issue before proposing major changes.