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

@telegraf/session

v2.0.0-beta.7

Published

Session store adapters for Telegraf

Downloads

9,588

Readme

@telegraf/session

This package provides official storage adapters for Telegraf v4.12+ sessions [see motivation].

[!WARNING] You're not meant to import the default path! Read one of the following sections before using this module.

An in-memory session module is bundled with Telegraf. The following modules are available here:

Redis

Install the official Redis driver alongside this module.

npm i @telegraf/session redis

Usage is pretty straightforward:

import { Redis } from "@telegraf/session/redis";

const store = Redis({
	url: "redis://127.0.0.1:6379",
});

const bot = new Telegraf(token, opts);
bot.use(session({ store }));

// the rest of your bot

To reuse an existing Redis client, use Redis({ client }) instead.

MongoDB

Install the official MongoDB driver alongside this module.

npm i @telegraf/session mongodb

Usage is pretty straightforward:

import { Mongo } from "@telegraf/session/mongodb";

const store = Mongo({
	url: "mongodb://127.0.0.1:27017",
	database: "telegraf-bot",
});

const bot = new Telegraf(token, opts);
bot.use(session({ store }));

// the rest of your bot

To reuse an existing MongoDB client, use Mongo({ client }) instead.

SQLite

Install the Better-SQLite3 driver and types alongside this module.

npm i @telegraf/session kysely better-sqlite3
npm i --save-dev @types/better-sqlite3

Usage is pretty straightforward:

import { SQLite } from "@telegraf/session/sqlite";

const store = SQLite({
	filename: "./telegraf-sessions.sqlite",
});

const bot = new Telegraf(token, opts);
bot.use(session({ store }));

// the rest of your bot

To reuse an existing Better-SQLite3 database instance, use SQLite({ database }) instead.

PostgreSQL

Install the 'pg' PostgreSQL driver and types alongside this module.

npm i @telegraf/session kysely pg
npm i --save-dev @types/pg

Usage is pretty straightforward:

import { Postgres } from "@telegraf/session/pg";

const store = Postgres({
	host: "127.0.0.1",
	database: "telegraf-test",
	user: "database-user",
	password: "hunter2",
});

const bot = new Telegraf(token, opts);
bot.use(session({ store }));

// the rest of your bot

To reuse an existing pg pool, use Postgres({ pool }) instead.

MySQL / MariaDB

Install the 'mysql2' MySQL driver alongside this module.

npm i @telegraf/session kysely mysql2

Usage is pretty straightforward:

import { MySQL } from "@telegraf/session/mysql";

const store = MySQL({
	host: "127.0.0.1",
	database: "telegraf-test",
	user: "database-user",
	password: "hunter2",
});

const bot = new Telegraf(token, opts);
bot.use(session({ store }));

// the rest of your bot

To reuse an existing MySQL2 pool, use MySQL({ pool }) instead.

Background

Since telegraf#1372, it has been known that all asynchronous session middleware have been prone to race-conditions. This was addressed in telegraf#1713, but third-party session middleware continue to be affected. Since Telegraf 1.12.0, it's recommended that third-party plugins only provide the store parameter for session, instead of implementing session themselves. This way, they can take advantage of the safety provided by Telegraf's builtin session. Of course, if your plugin has an exceptional usecase, it may need to implement its own middleware.

To begin to solve this problem, we officially maintain the 5 most common storage backends. This package is considered beta, and may have minor breaking changes and bugfixes before a semver stable release. Feedback is welcome via issues and in the group: TelegrafJSChat