All Projects → RealPeha → telegram-keyboard

RealPeha / telegram-keyboard

Licence: other
Simple and powerful reply and inline keyboard builder for Telegram Bots

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to telegram-keyboard

pastebin-bot
Advanced functional Pastebin Telegram Bot made using better-pastebin and TelegrafJS. This bot will help you to create pastes (Texts) on Pastebin which is a text cloud.
Stars: ✭ 16 (-77.14%)
Mutual labels:  telegram-bot, telegraf
cfworker-middware-telegraf
Make telegraf (a telegram bot framework) useable in Cloudflare Workers
Stars: ✭ 23 (-67.14%)
Mutual labels:  telegram-bot, telegraf
formatbot1
Make instant view easily and fast, from any article on the internet in the best messenger ever Telegram
Stars: ✭ 127 (+81.43%)
Mutual labels:  telegram-bot, telegraf
podsearch bot
Telegram bot that searches Podcast in iTunes store.
Stars: ✭ 28 (-60%)
Mutual labels:  telegram-bot, telegraf
Telegraf-Test
Telegraf Test - Simple Test ToolKit of Telegram Bots
Stars: ✭ 22 (-68.57%)
Mutual labels:  telegram-bot, telegraf
BooksAndBot
Telegram inline bot. Search for books and share them in a conversation
Stars: ✭ 26 (-62.86%)
Mutual labels:  telegram-bot, telegraf
Defend-The-Castle
Telegram Bot Game - Defend The Castle
Stars: ✭ 21 (-70%)
Mutual labels:  telegram-bot, telegraf
Nayumi
A cute bot of Telegram.
Stars: ✭ 12 (-82.86%)
Mutual labels:  telegram-bot, telegraf
Telegraf
Modern Telegram Bot Framework for Node.js
Stars: ✭ 5,178 (+7297.14%)
Mutual labels:  telegram-bot, telegraf
quizquickanswer-telegram-game-bot
🎮 Funny quiz game for telegram, play with friends on your group!
Stars: ✭ 15 (-78.57%)
Mutual labels:  telegram-bot, telegraf
telegraf-calendar-telegram
Inline calendar for Telegram bots using Telegraf framework
Stars: ✭ 43 (-38.57%)
Mutual labels:  telegram-bot, telegraf
keyswitch-kicad-library
Footprints for popular keyboard switches
Stars: ✭ 163 (+132.86%)
Mutual labels:  keyboard
dometyl-keyboard
A parametric generator for designing split, concave, ergonomic keyboards written in ocaml.
Stars: ✭ 84 (+20%)
Mutual labels:  keyboard
qbittorrent-bot
Telegram bot to mange your qBittorrent torrents
Stars: ✭ 91 (+30%)
Mutual labels:  telegram-bot
genshin task-resin-expedition alert
完全摸了,之后若有需要,请使用下面链接的这个仓库~
Stars: ✭ 91 (+30%)
Mutual labels:  telegram-bot
tgcli
Telegram Terminal Application
Stars: ✭ 39 (-44.29%)
Mutual labels:  telegram-bot
greed
A customizable, multilanguage Telegram shop bot with Telegram Payments support
Stars: ✭ 268 (+282.86%)
Mutual labels:  telegram-bot
kiririn
A serverless Telegram bot.
Stars: ✭ 27 (-61.43%)
Mutual labels:  telegram-bot
Plants-Identification
🌻 Deep learning project of Taiwan plants classification and detection with chatbot implementation
Stars: ✭ 18 (-74.29%)
Mutual labels:  telegram-bot
telegraf-influxdb-grafana
TIG Stack
Stars: ✭ 30 (-57.14%)
Mutual labels:  telegraf

Telegram Keyboard Builder

Simple and powerful reply and inline 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)

bot.on('text', async (ctx) => {
  const keyboard = Keyboard.make([
    ['Button 1', 'Button 2'], // First row
    ['Button 3', 'Button 4'], // Second row
  ])

  await ctx.reply('Simple built-in keyboard', keyboard.reply())
  await ctx.reply('Simple inline keyboard', keyboard.inline())
})

Reply (build-in) keyboard

Example

const { Keyboard } = require('telegram-keyboard')

const keyboard = Keyboard.make(['Button 1', 'Button 2']).reply()

// or using shortcut
const keyboard = Keyboard.reply(['Button 1', 'Button 2'])

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

// or using shortcut
const keyboard = Keyboard.inline(['Button 1', 'Button 2'])

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,
}).reply()

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
}).reply()

console.log(keyboard)

Result

{
  "reply_markup": {
    "resize_keyboard": true,
    "keyboard": [ // different every time
      ["1", "2"],
      ["3"],
      ["4"],
      ["5"]
    ]
  }
}

Pattern

Example

In this example pattern: [3, 1, 1] means that the first row will have 3 buttons, the second - 1, the third - 1 and all the other buttons in the last row

const { Keyboard } = require('telegram-keyboard')

const keyboard = Keyboard.make([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {
    pattern: [3, 1, 1]
}).reply()

console.log(keyboard)

Result

{
  "reply_markup": {
    "resize_keyboard": true,
    "keyboard": [
      ["1", "2", "3"],
      ["4"],
      ["5"],
      ["6", "7", "8", "9", "10"]
    ]
  }
}

Custom filter function

Default filter function is button => !button.hide but you can pass your filtering function

Example

const { Keyboard } = require('telegram-keyboard')

const keyboard = Keyboard.make([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {
    columns: 2,
    filter: btn => btn % 2
}).reply()

console.log(keyboard)

Result

{
  "reply_markup": {
    "resize_keyboard": true,
    "keyboard": [
      ["1", "3"],
      ["5", "7"],
      ["9"]
    ]
  }
}

Filter after build

Example

const { Keyboard } = require('telegram-keyboard')

const keyboard = Keyboard.make([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {
    columns: 2,
    filter: btn => btn % 2,
    filterAfterBuild: true
}).reply()

console.log(keyboard)

Result

{
  "reply_markup": {
    "resize_keyboard": true,
    "keyboard": [
      ["1"],
      ["3"],
      ["5"],
      ["7"],
      ["9"]
    ]
  }
}

Flat buttons

Example

const { Keyboard } = require('telegram-keyboard')

const keyboard = Keyboard.make([[1, 2], [3, 4, 5, 6], [7, 8, 9, 10]], {
    flat: true,
    columns: 3,
}).reply()

console.log(keyboard)

Result

{
  "reply_markup": {
    "resize_keyboard": true,
    "keyboard": [
      ["1", "2", "3"],
      ["4", "5", "6"],
      ["7", "8", "9"],
      [ "10"]
    ]
  }
}

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).reply()

console.log(keyboard)

Result

{
  "reply_markup": {
    "resize_keyboard": true,
    "keyboard": [
      ["1", "2"],
      ["3", "4"],
      ["5", "6", "7", "8"]
    ]
  }
}

Construct keyboard

Example

Full example in examples/pagination.js

Instead of writing a separate function to create the keyboard like this:

const createKeyboard = page => {
  return Keyboard
    .make(/* ... */)
}

You can pass function to Keyboard.make method. The function must return either an array of buttons or another keyboard. After that with the keyboard.construct(someArgs) method you can recreate it

const { Keyboard } = require('telegram-keyboard')

const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const itemsPerPage = 4

const keyboard = Keyboard.make((page) => {
    const pageItems = items.slice(page * itemsPerPage, page * itemsPerPage + itemsPerPage)
    
    return Keyboard.combine(
        Keyboard.make(pageItems, { columns: 3 }),
        Keyboard.make([
            Key.callback('<----', 'left', page === 0),
            Key.callback('---->', 'right', page === 2),
        ])
    )
})

// remake keyboard with some other arguments
console.log(keyboard.construct(0).reply())
console.log(keyboard.construct(1).reply())

More examples you may find in examples

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].