Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

callback-data

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

callback-data

Easy callback data management for Telegram

  • 0.3.0
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
decreased by-16.42%
Maintainers
1
Weekly downloads
 
Created
Source

Introduction

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

Installation

$ npm install callback-data

or using yarn:

$ yarn add callback-data

Example

import { Telegraf, Markup, Context } from 'telegraf';
import { CallbackData } from 'callback-data';

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

const greetingData = new CallbackData<{ type: string; }>(
  'greeting', // namespace identifier
  ['type'] // data properties
);

bot.start((ctx: Context) =>
  ctx.reply('How to greet?', {
    ...Markup.inlineKeyboard([
      Markup.button.callback(
        'oldschool',
        greetingData.create({
          type: 'oldschool',
        })
      ), // callback data is equal to `greeting:oldschool`

      Markup.button.callback(
        'modern',
        greetingData.create({
          type: 'modern',
        })
      ), // callback data is equal to `greeting:modern`
    ]),
  })
);

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

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

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.create({
  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/

Keywords

FAQs

Package last updated on 03 Jun 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc