3.0.1 • Published 2 years ago

grammy-pseudo-update v3.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

grammy-pseudo-update

grammy-pseudo-update is a plugin for grammY that injects a manually generated Update object into grammy middleware stack, as if it were coming from Telegram.

This could be useful e.g. with grammy-scenes when you want to move a scene with an externally generated event.

Install

yarn add grammy-pseudo-update

Use

With composer middleware

// Monkey patch grammy.
import "grammy-pseudo-update"

import { Bot } from "grammy"

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

bot.pseudo(async (ctx, next) => {
  // Access payload with ctx.pseudo or ctx.update.pseudo.payload
  await ctx.reply(`External event occured: ${ctx.pseudo}`)
})

some_external_event_listener((chat_id, payload) => {
  bot.handlePseudoUpdate({ chat_id, payload })
})

bot.start()

With ad-hoc middleware

// This will monkey-patch grammy.
import { pseudoUpdate } from "grammy-pseudo-update"

import { Bot } from "grammy"

const bot = new Bot(process.env.BOT_TOKEN)
bot.use(session(...))
// Add ad-hoc middleware executor after you've prepared the context.
bot.use(pseudoUpdate)
bot.command("start", ...)

some_external_event_listener((chat_id, payload) => {
  bot.handlePseudoUpdate({ chat_id }, (ctx) => {
    // This will be executed by `pseudoUpdate` executor above.
    await ctx.reply(`External event occured: ${payload}`)
  })
})

bot.start()

Typescript

Augment the payload interface:

declare module "grammy-pseudo-update" {
  interface PseudoUpdatePayload {
    foo?: FooData
  }
}

Note that it is marked as interface (not type) so that multiple plugins could use the same payload type/data object with their dedicated keys.

3.0.1

2 years ago

3.0.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago