From 78363514b51a66fe23f8fdca1056dd37c986031b Mon Sep 17 00:00:00 2001 From: Ilya Semenov Date: Mon, 27 Dec 2021 15:35:21 +0700 Subject: [PATCH] feat: integration with grammy-pseudo-update --- README.md | 39 ++++++++++++++++++++++++++++++++++++ package.json | 38 ++++++++++++++++++++++++++++------- src/contrib/pseudo-update.ts | 39 ++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 ++- tsup.config.ts | 2 +- yarn.lock | 5 +++++ 6 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 src/contrib/pseudo-update.ts diff --git a/README.md b/README.md index d71698d..30668a0 100644 --- a/README.md +++ b/README.md @@ -237,3 +237,42 @@ await ctx.scenes.continue(token, { result: 123 }) ``` If the scene has moved on, this will be silently ignored. + +### Integration with `grammy-pseudo-update` + +In the "continue" example above, the imaginary external handler is supposed to somehow keep a reference to `ctx`. + +In real world, that is not always possible. The continuation request could come from e.g. message queue processor or a HTTP server. + +To achieve that, `grammy-scenes` provides integration with [grammy-pseudo-update](https://github.com/IlyaSemenov/grammy-pseudo-update): + +```ts +import { scenesPseudoUpdate } from "grammy-scenes/pseudo-update" + +// ... + +bot.use(scenes) +bot.use(scenesPseudoUpdate) + +on_external_event(({ chat_id, token, arg }) => { + bot.handlePseudoUpdate({ + chat_id, + payload: { + scenes: { _: "continue", token, arg }, + }, + }) +}) + +bot.start() +``` + +Similarly, it's possible to "enter" a scene with: + +```ts +bot.handlePseudoUpdate({ + chat_id, + payload: { + scenes: { _: "enter", scene, arg }, + }, +}) +``` diff --git a/package.json b/package.json index dd478cb..dba5b7a 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,23 @@ "module": "dist/index.mjs", "types": "dist/index.d.ts", "source": "src/index.ts", + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.mjs" + }, + "./pseudo-update": { + "require": "./dist/contrib/pseudo-update.js", + "import": "./dist/contrib/pseudo-update.mjs" + } + }, + "typesVersions": { + "*": { + "pseudo-update": [ + "dist/contrib/pseudo-update" + ] + } + }, "files": [ "dist", "src", @@ -24,6 +41,19 @@ "lint": "eslint --fix '**/*.{js,ts}'", "prepare": "husky install" }, + "dependencies": { + "ts-essentials": "^9.1.0", + "uuid": "^8.3.2" + }, + "peerDependencies": { + "grammy": "^1.5.4", + "grammy-pseudo-update": "^1.0.0" + }, + "peerDependenciesMeta": { + "grammy-pseudo-update": { + "optional": true + } + }, "devDependencies": { "@commitlint/cli": "^15.0.0", "@commitlint/config-conventional": "^15.0.0", @@ -39,18 +69,12 @@ "eslint-plugin-prettier": "^4.0.0", "eslint-plugin-simple-import-sort": "^7.0.0", "grammy": "^1.5.4", + "grammy-pseudo-update": "^1.0.0", "husky": ">=6", "lint-staged": ">=10", "prettier": "^2.1.2", "tap": "^15.1.5", "tsup": "^5.11.9", "typescript": "^4.0.5" - }, - "peerDependencies": { - "grammy": "^1.5.4" - }, - "dependencies": { - "ts-essentials": "^9.1.0", - "uuid": "^8.3.2" } } diff --git a/src/contrib/pseudo-update.ts b/src/contrib/pseudo-update.ts new file mode 100644 index 0000000..a4f96e4 --- /dev/null +++ b/src/contrib/pseudo-update.ts @@ -0,0 +1,39 @@ +import "grammy-pseudo-update" + +import { MiddlewareFn } from "grammy" +import { ScenesFlavoredContext } from "grammy-scenes" + +declare module "grammy-pseudo-update" { + interface PseudoUpdatePayload { + scenes?: + | { + _: "enter" + scene: string + arg?: any + } + | { + _: "continue" + token: string + arg?: any + } + } +} + +export const scenesPseudoUpdate: MiddlewareFn = async ( + ctx, + next +) => { + const payload = ctx.pseudo?.scenes + if (payload) { + switch (payload._) { + case "enter": + await ctx.scenes.enter(payload.scene, payload.arg) + break + case "continue": + await ctx.scenes.continue(payload.token, payload.arg) + break + } + return + } + return next() +} diff --git a/tsconfig.json b/tsconfig.json index 7570eba..e83c556 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,8 @@ "include": ["src", "tests"], "compilerOptions": { "paths": { - "grammy-scenes": ["./src"] + "grammy-scenes": ["./src"], + "grammy-scenes/pseudo-update": ["./src/contrib/pseudo-update"] } } } diff --git a/tsup.config.ts b/tsup.config.ts index 887dfb3..6ea7a52 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -2,7 +2,7 @@ import { defineConfig } from "tsup" export default defineConfig({ clean: true, - entry: ["src/index.ts"], + entry: ["src/index.ts", "src/contrib/pseudo-update.ts"], format: ["cjs", "esm"], sourcemap: true, dts: true, diff --git a/yarn.lock b/yarn.lock index 3530d08..327771c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1904,6 +1904,11 @@ graceful-fs@^4.1.15, graceful-fs@^4.1.6, graceful-fs@^4.2.0: resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.8.tgz#e412b8d33f5e006593cbd3cee6df9f2cebbe802a" integrity sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg== +grammy-pseudo-update@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/grammy-pseudo-update/-/grammy-pseudo-update-1.0.0.tgz#4ff145a292ab7697b9fe3ba5a52b3b6866d67be0" + integrity sha512-SQcAKG+UmnikDkApCVYOLPXpXTDr3fWFp6s402pVr+x7oxgj/4nzJRnZ8KVbGxhnXdTGOTo5RvnuqqN5Qsdhyg== + grammy@^1.5.4: version "1.5.4" resolved "https://registry.yarnpkg.com/grammy/-/grammy-1.5.4.tgz#0c2915626edcb23fb20dc52a09b21ae1fe788d25"