2.4.2 • Published 2 months ago

@grammyjs/storage-free v2.4.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

grammY Free Sessions


Storage adapter that can be used to store your session data for free and with zero setup. Authentication works via your existing bot token.

Installation

We already did the database setup for you. All you have to do is use this package. The rest will work automatically.

Deno

// Import this module
import { freeStorage } from "https://deno.land/x/grammy_storages/free/src/mod.ts";

// Install the session middleware
bot.use(session({
  initial: ...
  storage: freeStorage<SessionData>(bot.token),
}))

Node

Install this package.

npm i @grammyjs/storage-free

Next, use this code.

// Import the pacakge
import { freeStorage } from "@grammyjs/storage-free";

// Install the session middleware
bot.use(session({
  initial: ...
  storage: freeStorage<SessionData>(bot.token),
}))

Free Storage? Seriously?

Yes. We trust you, so please be gentle with the servers.

This service is free, forever and for everyone. We enforce some hard limits to make sure it can stay that way.

  • Session keys cannot be longer than 64 characters.
  • Session data cannot be larger than 16 KiB per session key.
  • There cannot be more than 50,000 sessions per bot.

How Does It Work?

We use your bot token once to make sure you are actually running a bot. If we can authenticate your bot successfully with the bot token, we generate a second token that is only used to access your session data. We then forget about your token again, so it is never stored.

This package will handle the login procedure (and everything else) for you. There is no further setup.

You may also be interested in our open-source backend implementation.

Full Example Bot

Example of a message counter bot running on Deno:

import {
  Bot,
  Context,
  session,
  SessionFlavor,
} from "https://lib.deno.dev/x/grammy@1.x/mod.ts";
import { freeStorage } from "https://deno.land/x/grammy_storages/free/src/mod.ts";

// Define session structure
interface SessionData {
  count: number;
}
type MyContext = Context & SessionFlavor<SessionData>;

// Create the bot and register the session middleware
const bot = new Bot<MyContext>(""); // <-- put your bot token between the ""

bot.use(session({
  initial: () => ({ count: 0 }),
  storage: freeStorage<SessionData>(bot.token),
}));

// Use persistent session data in update handlers
bot.on("message", async (ctx) => {
  ctx.session.count++;
  await ctx.reply(`Message count: ${ctx.session.count}`);
});

bot.catch((err) => console.error(err));
bot.start();
2.4.2

2 months ago

2.4.1

5 months ago

2.3.2

8 months ago

2.4.0

7 months ago

2.3.1

8 months ago

2.3.0

11 months ago

2.2.0

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.2

1 year ago

2.0.0

2 years ago

1.0.0

2 years ago