npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@grammyjs/storage-free

v2.4.2

Published

Storage adapter for grammY's free sessions

Downloads

1,103

Readme

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/[email protected]/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();