https://github.com/gramiojs/session
Session plugin for GramIO
https://github.com/gramiojs/session
gramio gramio-plugin in-memory redis sessions telegram telegram-bot-api
Last synced: 8 months ago
JSON representation
Session plugin for GramIO
- Host: GitHub
- URL: https://github.com/gramiojs/session
- Owner: gramiojs
- License: mit
- Created: 2024-03-10T15:31:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-11T15:53:27.000Z (over 1 year ago)
- Last Synced: 2025-08-19T06:58:03.508Z (10 months ago)
- Topics: gramio, gramio-plugin, in-memory, redis, sessions, telegram, telegram-bot-api
- Language: TypeScript
- Homepage: https://gramio.netlify.app/plugins/official/session.html
- Size: 58.6 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @gramio/session
[](https://www.npmjs.org/package/@gramio/session)
[](https://www.npmjs.org/package/@gramio/session)
[](https://jsr.io/@gramio/session)
[](https://jsr.io/@gramio/session)
Session plugin for GramIO.
**!!!Currently not optimized and WIP!!!**
## Usage
```ts
import { Bot } from "gramio";
import { session } from "@gramio/session";
const bot = new Bot(process.env.token!)
.extend(
session({
key: "sessionKey",
initial: () => ({ apple: 1 }),
})
)
.on("message", (context) => {
context.send(`🍏 apple count is ${++context.sessionKey.apple}`);
})
.onStart(console.log);
bot.start();
```
You can use this plugin with any storage ([Read more](https://gramio.dev/storages/))
### Redis example
[More info](https://github.com/gramiojs/storages/tree/master/packages/redis)
```ts
import { Bot } from "gramio";
import { session } from "@gramio/session";
import { redisStorage } from "@gramio/storage-redis";
const bot = new Bot(process.env.token!)
.extend(
session({
key: "sessionKey",
initial: () => ({ apple: 1 }),
storage: redisStorage(),
})
)
.on("message", (context) => {
context.send(`🍏 apple count is ${++context.sessionKey.apple}`);
})
.onStart(console.log);
bot.start();
```
### TypeScript
To **type** a session data, you need to specify the type as the `ReturnType` of the initial function.
```ts
interface MySessionData {
apple: number;
some?: "maybe-empty";
}
bot.extend(
session({
key: "sessionKey",
initial: (): MySessionData => ({ apple: 1 }),
})
);
```