Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
telegraf-session-local
Advanced tools
Readme
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
,
$ 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.
const { Telegraf } = require('telegraf')
const LocalSession = require('telegraf-session-local')
const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here
bot.use((new LocalSession({ database: 'example_db.json' })).middleware())
bot.on('text', (ctx, next) => {
ctx.session.counter = ctx.session.counter || 0
ctx.session.counter++
ctx.replyWithMarkdownV2(`Counter updated, new value: \`${ctx.session.counter}\``)
return next()
})
bot.command('/stats', (ctx) => {
ctx.replyWithMarkdownV2(`Database has \`${ctx.session.counter}\` messages from @${ctx.from.username || ctx.from.id}`)
})
bot.command('/remove', (ctx) => {
ctx.replyWithMarkdownV2(`Removing session from database: \`${JSON.stringify(ctx.session)}\``)
// Setting session to null, undefined or empty object/array will trigger removing it from database
ctx.session = null
})
bot.launch()
const { Telegraf } = require('telegraf')
const LocalSession = require('telegraf-session-local')
const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here
const localSession = new LocalSession({
// Database name/path, where sessions will be located (default: 'sessions.json')
database: 'example_db.json',
// Name of session property object in Telegraf Context (default: 'session')
property: 'session',
// Type of lowdb storage (default: 'storageFileSync')
storage: LocalSession.storageFileAsync,
// Format of storage/database (default: JSON.stringify / JSON.parse)
format: {
serialize: (obj) => JSON.stringify(obj, null, 2), // null & 2 for pretty-formatted JSON
deserialize: (str) => JSON.parse(str),
},
// We will use `messages` array in our database to store user messages using exported lowdb instance from LocalSession via Telegraf Context
state: { messages: [] }
})
// Wait for database async initialization finished (storageFileAsync or your own asynchronous storage adapter)
localSession.DB.then(DB => {
// Database now initialized, so now you can retrieve anything you want from it
console.log('Current LocalSession DB:', DB.value())
// console.log(DB.get('sessions').getById('1:1').value())
})
// Telegraf will use `telegraf-session-local` configured above middleware
bot.use(localSession.middleware())
bot.on('text', (ctx, next) => {
ctx.session.counter = ctx.session.counter || 0
ctx.session.counter++
ctx.replyWithMarkdownV2(`Counter updated, new value: \`${ctx.session.counter}\``)
// Writing message to Array `messages` into database which already has sessions Array
ctx.sessionDB.get('messages').push([ctx.message]).write()
// `property`+'DB' is a name of ctx property which contains lowdb instance, default = `sessionDB`
return next()
})
bot.command('/stats', (ctx) => {
ctx.replyWithMarkdownV2(`Session has \`${ctx.session.counter}\` messages from @${ctx.from.username || ctx.from.id}`)
})
bot.command('/remove', (ctx) => {
ctx.replyWithMarkdownV2(`Removing session from lowdb database: \`${JSON.stringify(ctx.session)}\``)
// Setting session to null, undefined or empty object/array will trigger removing it from database
ctx.session = null
})
bot.launch()
/examples
folder (PRs welcome)Also, you may read comments in /lib/session.js
Tema Smirnov and contributors / github.tema@smirnov.one /
FAQs
Telegraf local sessions middleware with multiple supported storage types (Memory/FileSync/FileAsync/...) using lowdb
The npm package telegraf-session-local receives a total of 574 weekly downloads. As such, telegraf-session-local popularity was classified as not popular.
We found that telegraf-session-local demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.