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.
yarn add grammy-pseudo-update
// 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()
// 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()
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.