Session plugin for GramIO.
!!!Currently not optimized and WIP!!!
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)
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();
To type a session data, you need to specify the type as the ReturnType
of the initial function.
interface MySessionData {
apple: number;
some?: "maybe-empty";
}
bot.extend(
session({
key: "sessionKey",
initial: (): MySessionData => ({ apple: 1 }),
})
);