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
npm install telegraf-session-local
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
93 Stars
139 Commits
10 Forks
4 Watching
18 Branches
4 Contributors
Updated on 30 Mar 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
81.7%
189
Compared to previous day
Last week
20.6%
868
Compared to previous week
Last month
6.1%
3,228
Compared to previous month
Last year
15.5%
36,080
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 dangerous workflow patterns detected
Reason
no binaries found in the repo
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 2024-11-25
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