Gathering detailed insights and metrics for telegraf-session-local
Gathering detailed insights and metrics for telegraf-session-local
Gathering detailed insights and metrics for telegraf-session-local
Gathering detailed insights and metrics for telegraf-session-local
Telegraf local sessions middleware with multiple supported storage types (Memory/FileSync/FileAsync/...) using lowdb
npm install telegraf-session-local
Typescript
Module System
Min. Node Version
Node Version
NPM Version
70.9
Supply Chain
97.7
Quality
77.2
Maintenance
100
Vulnerability
99.6
License
JavaScript (100%)
Total Downloads
166,764
Last Day
187
Last Week
1,193
Last Month
4,997
Last Year
44,200
MIT License
92 Stars
139 Commits
10 Forks
3 Watchers
18 Branches
4 Contributors
Updated on May 12, 2025
Minified
Minified + Gzipped
Latest Version
2.1.1
Package Id
telegraf-session-local@2.1.1
Unpacked Size
24.33 kB
Size
7.51 kB
File Count
6
NPM Version
9.3.1
Node Version
18.13.0
Published on
Jan 29, 2023
Cumulative downloads
Total Downloads
Last Day
17.6%
187
Compared to previous day
Last Week
23.6%
1,193
Compared to previous week
Last Month
16.8%
4,997
Compared to previous month
Last Year
48.9%
44,200
Compared to previous year
2
Middleware for locally stored sessions & database
Any type of storage: Memory
, FileSync
, FileAsync
, ... (implement your own)
Any format you want: JSON
, BSON
, YAML
, XML
, ... (implement your own)
Shipped together with power of lodash
Supports basic DB-like operations (thanks to lodash-id):
getById
, insert
, upsert
, updateById
, updateWhere
, replaceById
, removeById
, removeWhere
, createId
,
1$ npm install -S telegraf-session-local
💡 TIP: We recommend
pnpm
package manager:npm i -g pnpm
and thenpnpm i -S telegraf-session-local
.
It's in-place replacement fornpm
, faster and better thannpm
/yarn
, and saves your disk space.
1const { Telegraf } = require('telegraf')
2const LocalSession = require('telegraf-session-local')
3
4const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here
5
6bot.use((new LocalSession({ database: 'example_db.json' })).middleware())
7
8bot.on('text', (ctx, next) => {
9 ctx.session.counter = ctx.session.counter || 0
10 ctx.session.counter++
11 ctx.replyWithMarkdownV2(`Counter updated, new value: \`${ctx.session.counter}\``)
12 return next()
13})
14
15bot.command('/stats', (ctx) => {
16 ctx.replyWithMarkdownV2(`Database has \`${ctx.session.counter}\` messages from @${ctx.from.username || ctx.from.id}`)
17})
18
19bot.command('/remove', (ctx) => {
20 ctx.replyWithMarkdownV2(`Removing session from database: \`${JSON.stringify(ctx.session)}\``)
21 // Setting session to null, undefined or empty object/array will trigger removing it from database
22 ctx.session = null
23})
24
25bot.launch()
1const { Telegraf } = require('telegraf') 2const LocalSession = require('telegraf-session-local') 3 4const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here 5 6const localSession = new LocalSession({ 7 // Database name/path, where sessions will be located (default: 'sessions.json') 8 database: 'example_db.json', 9 // Name of session property object in Telegraf Context (default: 'session') 10 property: 'session', 11 // Type of lowdb storage (default: 'storageFileSync') 12 storage: LocalSession.storageFileAsync, 13 // Format of storage/database (default: JSON.stringify / JSON.parse) 14 format: { 15 serialize: (obj) => JSON.stringify(obj, null, 2), // null & 2 for pretty-formatted JSON 16 deserialize: (str) => JSON.parse(str), 17 }, 18 // We will use `messages` array in our database to store user messages using exported lowdb instance from LocalSession via Telegraf Context 19 state: { messages: [] } 20}) 21 22// Wait for database async initialization finished (storageFileAsync or your own asynchronous storage adapter) 23localSession.DB.then(DB => { 24 // Database now initialized, so now you can retrieve anything you want from it 25 console.log('Current LocalSession DB:', DB.value()) 26 // console.log(DB.get('sessions').getById('1:1').value()) 27}) 28 29// Telegraf will use `telegraf-session-local` configured above middleware 30bot.use(localSession.middleware()) 31 32bot.on('text', (ctx, next) => { 33 ctx.session.counter = ctx.session.counter || 0 34 ctx.session.counter++ 35 ctx.replyWithMarkdownV2(`Counter updated, new value: \`${ctx.session.counter}\``) 36 // Writing message to Array `messages` into database which already has sessions Array 37 ctx.sessionDB.get('messages').push([ctx.message]).write() 38 // `property`+'DB' is a name of ctx property which contains lowdb instance, default = `sessionDB` 39 40 return next() 41}) 42 43bot.command('/stats', (ctx) => { 44 ctx.replyWithMarkdownV2(`Session has \`${ctx.session.counter}\` messages from @${ctx.from.username || ctx.from.id}`) 45}) 46 47bot.command('/remove', (ctx) => { 48 ctx.replyWithMarkdownV2(`Removing session from lowdb database: \`${JSON.stringify(ctx.session)}\``) 49 // Setting session to null, undefined or empty object/array will trigger removing it from database 50 ctx.session = null 51}) 52 53bot.launch()
/examples
folder (PRs welcome)Also, you may read comments in /lib/session.js
Tema Smirnov and contributors / github.tema@smirnov.one /
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 1/16 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-05-12
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More