Skip to content

Commit

Permalink
feat: integration with grammy-pseudo-update
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSemenov committed Dec 27, 2021
1 parent 4051bd6 commit 7836351
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 9 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
})
```
38 changes: 31 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
}
}
39 changes: 39 additions & 0 deletions src/contrib/pseudo-update.ts
Original file line number Diff line number Diff line change
@@ -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<ScenesFlavoredContext> = 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()
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"include": ["src", "tests"],
"compilerOptions": {
"paths": {
"grammy-scenes": ["./src"]
"grammy-scenes": ["./src"],
"grammy-scenes/pseudo-update": ["./src/contrib/pseudo-update"]
}
}
}
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 7836351

Please sign in to comment.