Socket
Socket
Sign inDemoInstall

@grammyjs/storage-free

Package Overview
Dependencies
7
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @grammyjs/storage-free

Storage adapter for grammY's free sessions


Version published
Maintainers
2
Install size
506 kB
Created

Changelog

Source

2.4.2 (2024-02-29)

Bug Fixes

  • add workaround for release action (9646669)
  • remove public from workflow (4f56e00)

Features

Readme

Source

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();

Keywords

FAQs

Last updated on 29 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc