Socket
Socket
Sign inDemoInstall

telegraf-callback-data

Package Overview
Dependencies
0
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    telegraf-callback-data

Easy callback data management for Telegraf


Version published
Maintainers
1
Created

Readme

Source

Introduction

Lightweight and simple library that helps manage callback data when using inline buttons in messages

Installation

$ npm install telegraf-callback-data

or using yarn:

$ yarn add telegraf-callback-data

Example

const Telegraf = require('telegraf');
const Markup = require('telegraf/markup');
const { CallbackData } = require('telegraf-callback-data');

const bot = new Telegraf(process.env.BOT_TOKEN);

greetingCD = new CallbackData('greeting', ['type']);

bot.start(({ reply }) =>
  reply('How to greet?', {
    ...Markup.inlineKeyboard([
      Markup.callbackButton(
        'oldschool',
        greetingCD.new({
          type: 'oldschool',
        })
      ), // callback data is equal to `greeting:oldschool`

      Markup.callbackButton(
        'modern',
        greetingCD.new({
          type: 'modern',
        })
      ), // callback data is equal to `greeting:modern`
    ]).extra(),
  })
);

bot.action(
  greetingCD.filter({
    type: 'oldschool',
  }),
  ({ answerCbQuery }) => answerCbQuery('Hello')
);

bot.action(
  greetingCD.filter({
    type: 'modern',
  }),
  ({ answerCbQuery }) => answerCbQuery('Yo')
);

bot.launch();

There's some more complex examples: counter and menu.

API Usage Example

exampleCallbackData = new CallbackData(
  'namespace-prefix',
  ['id', 'action' /* many as you want */]
  /* separator = ':' */
);

const callbackData = exampleCallbackData.new({
  id: '1337',
  action: 'show',
});
console.log(callbackData); // namespace-prefix:1337:show

const parsedCallbackData = exampleCallbackData.parse(callbackData);
console.log(parsedCallbackData); // { id: "1337", action: "show" }

const regexpCallbackDataFilter = exampleCallbackData.filter({
  action: 'show',
});
console.log(regexpCallbackDataFilter); // /namespace-prefix:\w+:show/

Credits

Thanks to @JrooTJunior, author of AIOGram! This library highly inspired by alternative feature in aiogram framework.

Keywords

FAQs

Last updated on 28 Apr 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc