Skip to content

Commit

Permalink
chore: add scenesDerives usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Dec 27, 2024
1 parent 1fbebec commit 36abec2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
63 changes: 61 additions & 2 deletions docs/plugins/official/scenes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

</div>

WIP. The API can be changed, but we already use it in production environment.
The API can be changed a little, but we already use it in production environment.

# Usage

Expand Down Expand Up @@ -60,7 +60,7 @@ const testScene = new Scene("test")
});
```

### Storage usage
## Storage usage

```ts
import { redisStorage } from "@gramio/storage-redis";
Expand All @@ -77,3 +77,62 @@ const bot = new Bot(process.env.TOKEN as string)
});
});
```

[Read more about storages](/storages)

## Scenes derives

Sometimes you wants to control scenes before plugin execute scene steps but after scene fetching from storage.

By default `scenes()` function derives what needed to next middlewares if user not in scene.
With `scenesDerives()` you can get it earlier and manage scene data.

<!-- TODO: without current scene -->

```ts twoslash
import { Scene } from "@gramio/scenes";

const testScene = new Scene("test").state<{
simple: string;
example: number[];
}>();
// ---cut---
import { scenes, scenesDerives, type AnyScene } from "@gramio/scenes";
import { Bot } from "gramio";
import { redisStorage } from "@gramio/storage-redis";

const storage = redisStorage();
const scenesList: AnyScene[] = [testScene];

const bot = new Bot(process.env.TOKEN as string)
.extend(
scenesDerives(scenesList, {
withCurrentScene: true,
storage,
})
)
.on("message", (context, next) => {
if (context.text === "/start" && context.scene.current) {
if (context.scene.current.is(testScene)) {
console.log(context.scene.current.state);
// ^?
return context.scene.current.step.previous();
} else return context.scene.current.reenter();
}

return next();
})
.extend(
scenes(scenesList, {
storage,
})
)
.command("start", async (context) => {
return context.scene.enter(testScene, {
test: true,
});
});
```

> [!IMPORTANT]
> The same **storage** and **list of scenes** should be shared across `scenes()` and `scenesDerives()` options.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@types/node": "^22.9.1",
"@unocss/preset-icons": "^0.64.1",
"elysia": "^1.1.25",
"gramio": "^0.1.2",
"gramio": "^0.2.1",
"grammy": "^1.32.0",
"undici": "^6.21.0",
"unocss": "^0.64.1",
Expand All @@ -36,7 +36,7 @@
},
"trustedDependencies": ["@biomejs/biome"],
"dependencies": {
"@gramio/scenes": "^0.0.5",
"@gramio/scenes": "^0.2.1",
"ioredis": "^5.4.2",
"jobify": "^0.1.1"
}
Expand Down

0 comments on commit 36abec2

Please sign in to comment.