Giter VIP home page Giter VIP logo

Comments (3)

TemaSM avatar TemaSM commented on June 19, 2024

Hi @Iniro,
Thanks for your question!

Short answer:
You are trying to use sessions database synchronously, but it even didn't initialized yet.
Simply set storage type to be synchronous, so your code will looks like:

const session = new LocalSession({
  database: 'example_db.json',
  storage: LocalSession.storageFileSync
})
bot.use(session.middleware());
console.log(session.getSession('170062604:170062604'));

Detailed answer:
As default telegraf-session-local uses asynchronous storage file adapter for lowdb:

const
debug = require('debug')('telegraf:session-local'),
lowdb = require('lowdb'),
storageFileSync = require('lowdb/adapters/FileSync'),
storageFileAsync = require('lowdb/adapters/FileAsync'),
storageMemory = require('lowdb/adapters/Memory')

class LocalSession {
constructor (options = {}) {
this.options = Object.assign({
storage: LocalSession.storageFileAsync,

And there's a little bug - we cannot use sessions database before it actually initialized.
Meanwhile I'm working on telegraf-session-local, you can simply set storage type to be synchronous, like:

const session = new LocalSession({
  database: 'example_db.json',
  storage: LocalSession.storageFileSync
})

It's not so easy to keep code backward compatible, but I will try to introduce in v0.0.6 something new on how to deal with asynchronous adapters.

from telegraf-session-local.

CaiooAndrade avatar CaiooAndrade commented on June 19, 2024

Thank you, your response was extremely helpful.

from telegraf-session-local.

TemaSM avatar TemaSM commented on June 19, 2024

@Iniro now you can access database (and sessions in it) in two ways:

  1. Synchronous storage (LocalSession.storageFileSync - default storage adapter since v0.0.6):
const session = new LocalSession()
bot.use(session.middleware())
console.log(session.getSession('170062604:170062604'))
  1. Asynchronous storage (LocalSession.storageFileAsync):
const session = new LocalSession({
  storage: LocalSession.storageFileAsync,
})
bot.use(session.middleware())
session.DB.then((db) => {
  console.log(db.get('sessions').getById('170062604:170062604').value())
})

from telegraf-session-local.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.