npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@puregram/callback-data

v1.2.3

Published

basic callback data validation and serialization for puregram

Downloads

85

Readme

@puregram/callback-data

basic callback data validation and serialization for puregram package

introduction

i'm tired of people using json objects as their callback data payload. this is NOT okay, you should NOT do that. at least because it's not safe when it comes to amount of bytes (JSON.stringify your ass), at most because at the end you have zero types, zero validation, zero anything

@puregram/callback-data provides those mentioned things. yeah, you can write callback data payload that will be validated and will have correct types! incredible.

example

let's create a keyboard that will automatically ban the user!

const { Telegram, InlineKeyboard } = require('puregram')

const { CallbackDataBuilder } = require('@puregram/callback-data')

const telegram = Telegram.fromToken(process.env.TOKEN)

// we create a 'ban' callback data...
const BanPayload = CallbackDataBuilder.create('ban')
  // ... with a number field 'user_id'
  .number('user_id')

const createBanKeyboard = (userId: number) => (
  InlineKeyboard.keyboard([
    InlineKeyboard.textButton({
      text: 'Ban',
      // here we pack our callback data into a small parseable string
      // with all our needed payload
      payload: BanPayload.pack({ user_id: userId })
    })
  ])
)

// let's create our handler
telegram.updates.on('message', (context) => {
  return context.send('User sent a message!', {
    chat_id: process.env.ADMIN_ID,
    reply_markup: createBanKeyboard(context.senderId)
  })
})

// let's handle our 'ban' callback queries!
telegram.updates.use(
  BanPayload.handle((context) => {
    // for convenience
    const payload = context.unpackedPayload
    //    payload: { user_id: number }

    // you can now do whatever you want with `payload.user_id`!
  })
)

telegram.updates.startPolling()

installation

$ yarn add @puregram/callback-data
$ npm i -S @puregram/callback-data

optional or defaulted values

it's possible that you will need a value that may not be present at all times (basically an optional value, right?)

@puregram/callback-data has tools for handling optional values and values that have a default value

optional
const BanPayload = CallbackDataBuilder.create('...')
  .number('user_id')
  .string('reason', { optional: true })

telegram.updates.use(
  BanPayload.handle((context) => {
    const payload = context.unpackedPayload
    //    payload: { user_id: number, reason?: string | undefined }
  })
)

you can also use filtering and literally filters to handle specific cases like when you have the reason for ban and when you dont

telegram.updates.use(
  BanPayload.filter({ reason: filters.exists() }).handle((context) => {
    const payload = context.unpackedPayload
    //    payload: { user_id: number, reason: string }
    // reason will always be present in this case!
  })
)

telegram.updates.use(
  BanPayload.filter({ reason: filters.exists(false) }).handle((context) => {
    const payload = context.unpackedPayload
    //    payload: { user_id: number, reason: undefined }
    // reason will always be undefined here no matter what
  })
)
default

yeah so

const CounterPayload = CallbackDataBuilder.create('counter')
  .number('clicks', { default: 0 })

const counterKeyboard = InlineKeyboard.keyboard([
  InlineKeyboard.textButton({
    text: 'click!',
    payload: CounterPayload.pack({}) // no need to provide `clicks` value!
  })
])

ez stuff $$$

filtering

sometimes you will need to process updates in more detail. that's why CallbackDataBuilder has its own filter function (that is also type-safe! as far as i know). it can accept those values:

  • raw values (string, boolean, number);
  • a function that takes the value and returns non-false value;
  • an array of those above;
  • or a filter.
raw value
telegram.updates.use(
  BanPayload.filter({ user_id: 1337 }).handle((context) => {
    // will be called only if `user_id` is exactly `1337`
  })
)
function
const ADMIN_IDS = [1, 2, 3]

telegram.updates.use(
  BanPayload.filter({ user_id: (userId) => !ADMIN_IDS.includes(userId) }).handle((context) => {
    // this will not be called if `user_id` is either `1`, `2` or `3`
    // will be called otherwise though
  })
)
an array
telegram.updates.use(
  BanPayload.filter({ user_id: [42, (userId) => userId % 2 !== 0 ] }).handle((context) => {
    // will be called EITHER if `user_id` is `42` OR if `user_id` is even
    // because who wants people with odd user IDs be banned? ¯\_(ツ)_/¯
  })
)

literally filters

@puregram/callback-data also has filters for filters. they're called... filters.

currently there aren't much of those: only filters.exists(exists?: boolean) and that's it. it will probably be expanded in the future...

const { filters } = require('@puregram/callback-data')

const FooPayload = CallbackDataBuilder.create('foo')
  .string('bar')
  .boolean('baz', { optional: true })
  .number('quix', { default: 42 })

telegram.updates.use(
  FooPayload.filter({ baz: filters.exists() }).handle((context) => {
    // this will be called only if `baz` iz provided (not `undefined`)
  })
)

not sure what to describe here, filters act like filters: they filter out values


typescript usage

@puregram/callback-data's updates are based on puregram's CallbackQueryContext so CallbackQueryContext will have a new unpackedPayload: Record<never, never> property by default, but you definitely won't use that in your ordinary 'callback_query' updates, so don't worry about that. everything you need is already packed into handles logic under the hood

import { CallbackDataBuilder } from '@puregram/callback-data'

const BanPayload = CallbackDataBuilder.create('ban')
  .number('user_id')

// ...

telegram.updates.use(
  BanPayload.handle((context) => {
    const payload = context.unpackedPayload
    //    payload: { user_id: number }
  })
)

but in case you really need to extend your own contexts with that juicy types you can always import CallbackLayer:

import type { Context } from 'puregram'
import type { CallbackLayer } from '@puregram/callback-data'

type MyContext<C extends Context> = C & CallbackLayer<typeof BanPayload>

that's it!