Package Exports
- telegram-keyboard
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (telegram-keyboard) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Telegram Keyboard Builder
Simple and powerful keyboard builder for Telegram Bots
Installation
Just use npm
npm i telegram-keyboard --save
or yarn
yarn add telegram-keyboard
Example of use in Telegraf
const Telegraf = require('telegraf')
const { Keyboard } = require('telegram-keyboard')
const bot = new Telegraf(process.env.BOT_TOKEN)
bon.on('text', async ({ reply }) => {
const keyboard = Keyboard.make([
['Button 1', 'Button 2'], // First row
['Button 3', 'Button 4'], // Second row
])
await ctx.reply('Simple built-in keyboard', keyboard.builtIn())
await ctx.reply('Simple inline keyboard', keyboard.inline())
})
Built-in keyboard
Example
const { Keyboard } = require('telegram-keyboard')
const keyboard = Keyboard.make(['Button 1', 'Button 2']).builtIn()
console.log(keyboard)
Result
{
"reply_markup": {
"resize_keyboard": true,
"keyboard": [
[
"Button 1",
"Button 2"
]
]
}
}
Inline keyboard
Example
const { Keyboard } = require('telegram-keyboard')
const keyboard = Keyboard.make(['Button 1', 'Button 2']).inline()
console.log(keyboard)
Result
{
"reply_markup": {
"resize_keyboard": true,
"inline_keyboard": [
[
{
"text": "Button 1",
"callback_data": "Button 1"
},
{
"text": "Button 2",
"callback_data": "Button 2"
}
]
]
}
}
Inline keyboard with custom callback data
Example
const { Keyboard, Key } = require('telegram-keyboard')
const keyboard = Keyboard.make([
Key.callback('Button 1', 'action1'),
Key.callback('Button 2', 'action2'),
]).inline()
console.log(keyboard)
Result
{
"reply_markup": {
"resize_keyboard": true,
"inline_keyboard": [
[
{
"text": "Button 1",
"callback_data": "action1"
},
{
"text": "Button 2",
"callback_data": "action2"
}
]
]
}
}
Fixed columns keyboard
Example
const { Keyboard } = require('telegram-keyboard')
const keyboard = Keyboard.make(['1', '2', '3', '4', '5'], { columns: 2 }).builtIn()
console.log(keyboard)
Result
{
"reply_markup": {
"resize_keyboard": true,
"keyboard": [
["1", "2"],
["3", "4"],
["5"]
]
}
}
Calculated columns keyboard
Example
const { Keyboard } = require('telegram-keyboard')
const keyboard = Keyboard.make(['1', '2', '3', '4', '5'], {
wrap: (row, index, button) => Math.random() > 0.5
}).builtIn()
console.log(keyboard)
Result
{
"reply_markup": {
"resize_keyboard": true,
"keyboard": [ // different every time
["1", "2"],
["3"],
["4"],
["5"]
]
}
}
Combine keyboards
Example
const { Keyboard } = require('telegram-keyboard')
const keyboard1 = Keyboard.make(['1', '2', '3', '4'], { columns: 2 })
const keyboard2 = Keyboard.make(['5', '6', '7', '8'])
const keyboard = Keyboard.combine(keyboard1, keyboard2).builtIn()
console.log(keyboard)
Result
{
"reply_markup": {
"resize_keyboard": true,
"keyboard": [
["1", "2"],
["3", "4"],
["5", "6", "7", "8"]
]
}
}
More examples you may find in example