Deta.sh Base storage adapter for grammY
Storage adapter that can be used to store your session data on Deta.sh Base when using sessions.
Installation
Node
npm install @grammyjs/storage-deta --save
Deno
import { DetaAdapter } from "https://deno.land/x/grammy_storages/deta/src/mod.ts";
Introduction
Set up your Deta Base by creating a Deta project. Copy the Project Key to here.
You should now have:
- A project key for your Deta.sh project.
- A Telegram bot token.
Put those values into the following example code:
Usage
You can check examples folder.
Example of a message counter bot running on Deno:
import {
Bot,
Context,
session,
SessionFlavor,
} from "https://deno.land/x/grammy/mod.ts";
import { DetaAdapter } from "https://deno.land/x/grammy_storages/deta/src/mod.ts";
interface SessionData {
count: number;
}
type MyContext = Context & SessionFlavor<SessionData>;
const bot = new Bot<MyContext>("");
bot.use(session({
initial: () => ({ count: 0 }),
storage: new DetaAdapter<SessionData>({
baseName: "session",
projectKey: "",
}),
}));
bot.on("message", async (ctx) => {
ctx.session.count++;
await ctx.reply(`Message count: ${ctx.session.count}`);
});
bot.catch((err) => console.error(err));
bot.start();