Registry / messaging / telegraf-session-sqlite

telegraf-session-sqlite

JSON →
library1.0.6jsnpmunverified

SQLite session middleware for the Telegraf Telegram bot framework, version 1.0.6. Maintains session data persistently using SQLite3 database. Requires an existing sqlite3 instance and a table with a specific schema. Suitable for production bots that need state persistence across restarts with minimal dependencies. Compared to in-memory stores, this provides durability, but lacks built-in expiry or cleanup mechanisms.

npm install telegraf-session-sqlite
INSTALL
IMPORT
SIG · TELEGRAF-SESSION-S
T
telegraf-session-sqlite
messagingjavascriptv1.0.6
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

session
const session = require('telegraf-session-sqlite');
import { session } from 'telegraf-session-sqlite';
This package uses CommonJS module format; default export is the session function. Named exports are not supported.
Telegraf
const Telegraf = require('telegraf');
const { Telegraf } = require('telegraf');
Telegraf is default exported, not a named export.
sqlite3
const sqlite3 = require('sqlite3').verbose();
const sqlite3 = require('sqlite3');
The verbose() method enables long stack traces for debugging; without it errors may be less descriptive.

Creates a Telegram bot with SQLite session middleware storing counter in memory session.

const sqlite3 = require('sqlite3').verbose(); const { session } = require('telegraf-session-sqlite'); const Telegraf = require('telegraf'); const bot = new Telegraf(process.env.BOT_TOKEN); const db = new sqlite3.Database(':memory:'); db.run(`CREATE TABLE telegraf_session (id VARCHAR(255) PRIMARY KEY, session VARCHAR(255))`); bot.use(session({ db, table_name: 'telegraf_session' })); bot.on('text', (ctx) => { ctx.session.counter = (ctx.session.counter || 0) + 1; ctx.reply(`Session counter: ${ctx.session.counter}`); }); bot.launch();
Debug
Known issues
gotchaSession data is serialized to JSON before storage; objects with circular references or non-serializable values will cause errors.
fix
Ensure session objects are JSON-serializable. Avoid storing functions, Symbols, or circular structures.
affects: >=0.0.0
gotchaDatabase table must exist before using middleware; no automatic table creation is performed.
fix
Create the table manually using the provided SQL schema: CREATE TABLE telegraf_session (id VARCHAR(255) PRIMARY KEY, session VARCHAR(255));
affects: >=0.0.0
gotchaSession data is stored as a VARCHAR(255) column; large session objects may be truncated.
fix
Alter the table to use TEXT instead of VARCHAR(255) to store longer session strings.
affects: >=0.0.0
gotchaNo session expiration or cleanup is implemented; database will grow unboundedly with stale records.
fix
Implement periodic cleanup of old sessions or use an alternative session store with TTL support.
affects: >=0.0.0
gotchaThe package uses synchronous SQLite operations? Actually it uses async but database errors may not be properly handled.
fix
Wrap bot usage in try-catch or attach error handlers to the database instance.
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'telegraf-session-sqlite'
Package not installed or incorrect import path.
fix
Run 'npm install telegraf-session-sqlite' and ensure the require path is correct.
SQLITE_ERROR: no such table: telegraf_session
Table not created in the database before using middleware.
fix
Execute the CREATE TABLE statement from the README before calling session() middleware.
TypeError: session is not a function
Incorrect import or version mismatch.
fix
Use 'const session = require('telegraf-session-sqlite');' (commonjs) and verify the package is version 1.x.
Cannot read property 'counter' of undefined
Session object not initialized; middleware not applied or ctx.session is undefined.
fix
Ensure bot.use(session(options)) is called before any handlers that access ctx.session.
Upgrade
Version history
1.0.6latest on npm
Audit
Dependencies
sqlite3requiredRequired to create and interact with the SQLite database instance passed via options.
telegrafrequiredProvides the base Telegram bot framework that the middleware integrates with.
Agent activity
17 hits · last 30 days
node
16
OpenAI (training)
1
Resources
telegraf-session-sqlite — npm install telegraf-session-sqlite · libregistry