https://github.com/gramiojs/keyboards
Framework-agnostic Telegram bot keyboard builder with many cool features!
https://github.com/gramiojs/keyboards
telegram telegram-api telegram-bot telegram-bot-api telegram-keyboard
Last synced: 2 months ago
JSON representation
Framework-agnostic Telegram bot keyboard builder with many cool features!
- Host: GitHub
- URL: https://github.com/gramiojs/keyboards
- Owner: gramiojs
- License: mit
- Created: 2024-02-01T23:52:07.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-06T19:36:28.000Z (about 1 year ago)
- Last Synced: 2025-04-06T20:30:38.005Z (about 1 year ago)
- Topics: telegram, telegram-api, telegram-bot, telegram-bot-api, telegram-keyboard
- Language: TypeScript
- Homepage: https://gramio.netlify.app/keyboards/overview.html
- Size: 157 KB
- Stars: 5
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @gramio/keyboards
Framework-agnostic Telegram bot keyboard (and inline query result) builder with many cool features!
[](https://www.npmjs.org/package/@gramio/keyboards)
[](https://www.npmjs.org/package/@gramio/keyboards)
[](https://jsr.io/@gramio/keyboards)
[](https://jsr.io/@gramio/keyboards)
### Installation (if you don't use GramIO)
```bash
npm i @gramio/keyboards
```
# See more in [Documentation](https://gramio.dev/keyboards/overview.html)
## Usage ([with frameworks](#usage-with-frameworks))
### Simple Keyboard
```ts
import { Keyboard } from "@gramio/keyboards";
const keyboard = new Keyboard()
.text("first row")
.row()
.text("second row")
.build(); // NOTE: In GramIO, you don't have to use the ".build" method
```
## Usage with Frameworks
### Send via [GramIO](https://gramio.dev/)
```ts
import { Bot, Keyboard } from "gramio"; // import from GramIO package!!
const bot = new Bot(process.env.TOKEN as string);
const data = ["Apple", "Realme", "Tesla", "Xiaomi"];
bot.on("message", (ctx) => {
return ctx.send("test", {
reply_markup: new Keyboard()
.columns(1)
.text("simple keyboard")
.add(...data.map((x) => Keyboard.text(x)))
.filter(({ button }) => button.text !== "Tesla"),
});
});
bot.start();
```
### Send via [Grammy](https://grammy.dev/)
```ts
import { Keyboard } from "@gramio/keyboards";
import { Bot } from "grammy";
const bot = new Bot(process.env.TOKEN as string);
const data = ["Apple", "Realme", "Tesla", "Xiaomi"];
bot.on("message", (ctx) => {
return ctx.reply("test", {
reply_markup: new Keyboard()
.columns(1)
.text("simple keyboard")
.add(...data.map((x) => Keyboard.text(x)))
.filter(({ button }) => button.text !== "Tesla")
.build(),
});
});
bot.start();
```
### Send via [Telegraf](https://github.com/telegraf/telegraf)
> [!WARNING]
> The `Telegraf` does not support the latest version of Bot API
```ts
import { Keyboard } from "@gramio/keyboards";
import { Telegraf } from "telegraf";
const bot = new Telegraf(process.env.TOKEN as string);
const data = ["Apple", "Realme", "Tesla", "Xiaomi"];
bot.on("message", (ctx) => {
return ctx.reply("test", {
reply_markup: new Keyboard()
.columns(1)
.text("simple keyboard")
.add(...data.map((x) => Keyboard.text(x)))
.filter(({ button }) => button.text !== "Tesla")
.build(),
});
});
bot.launch();
```
### Send via [node-telegram-bot-api](https://www.npmjs.com/package/node-telegram-bot-api)
> [!WARNING]
> The `node-telegram-bot-api` does not support the latest version of Bot API and the types are badly written, so the types may not match
```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, "test", {
reply_markup: new Keyboard()
.columns(1)
.text("simple keyboard")
.add(...data.map((x) => Keyboard.text(x)))
.filter(({ button }) => button.text !== "Tesla")
.build(),
});
});
```
### Send via [puregram](https://puregram.cool/)
> [!WARNING]
> The `puregram` does not support the latest version of 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("test", {
reply_markup: new Keyboard()
.columns(1)
.text("simple keyboard")
.add(...data.map((x) => Keyboard.text(x)))
.filter(({ button }) => button.text !== "Tesla")
.build(),
});
});
bot.updates.startPolling();
```
#### Result
```json
{
"keyboard": [
[
{
"text": "simple keyboard"
}
],
[
{
"text": "Apple"
}
],
[
{
"text": "Realme"
}
],
[
{
"text": "Xiaomi"
}
]
],
"one_time_keyboard": false,
"is_persistent": false,
"selective": false,
"resize_keyboard": true
}
```
