Session Plugin
Session plugin for GramIO.
Currently not optimized and WIP.
Installation
bash
npm install @gramio/session
bash
yarn add @gramio/session
bash
pnpm add @gramio/session
bash
bun install @gramio/session
Usage
ts
import { Bot } from "gramio";
import { session } from "@gramio/session";
const bot = new Bot(process.env.BOT_TOKEN as string)
.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)
Redis example
ts
import { Bot } from "gramio";
import { session } from "@gramio/session";
import { redisStorage } from "@gramio/storage-redis";
const bot = new Bot(process.env.BOT_TOKEN as string)
.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 }),
})
);