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 bots

  • 1.1.0
  • 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 keyboards.

Installation

Node

$ npm install callback-data

or using yarn:

$ yarn add callback-data

Deno

import { createCallbackData } from "https://deno.land/x/callback_data/mod.ts";

Example

import { Bot, InlineKeyboard } from "grammy";
import { createCallbackData } from "callback-data";

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

const greetingData = createCallbackData("greeting", {
  type: String,
});

const greetingKeyboard = new InlineKeyboard()
  .text(
    "oldschool",
    greetingData.pack({
      type: "oldschool",
    }), // callback data is equal to "greeting:oldschool"
  )
  .text(
    "modern",
    greetingData.pack({
      type: "modern",
    }), // callback data is equal to "greeting:modern"
  );

bot.command("start", (ctx) =>
  ctx.reply("How to greet?", {
    reply_markup: greetingKeyboard,
  }));

bot.callbackQuery(
  greetingData.filter({
    type: "oldschool",
  }),
  (ctx) => ctx.answerCallbackQuery("Hello"),
);

bot.callbackQuery(
  greetingData.filter({
    type: "modern",
  }),
  (ctx) => ctx.answerCallbackQuery("Yo"),
);

bot.start();

There's more complex example counter-bot.

Usage

Create callback data

const callbackData = createCallbackData(
  // unique prefix for callback data
  "data",
  // callback data fields
  {
    id: Number,
    name: String,
    isAdmin: Boolean,
    // only Number, String, Boolean data types are supported
  },
);

Pack callback data

const packedData = callbackData.pack({
  id: 1337,
  name: "John",
  isAdmin: true,
});

console.log(packedData); // data:1337:John:1

Unpack callback data

const unpackedData = callbackData.unpack(packedData);

console.log(unpackedData);
// {
//   id: 1337,
//   name: "John",
//   isAdmin: true,
// }

Filter by callback data

const regex = callbackData.filter({
  id: 1337,
});

console.log(regex); // /^data:1337:\w+:\d$/

Keywords

FAQs

Package last updated on 29 Aug 2023

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