Comments (3)
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:
telegraf-session-local/lib/session.js
Lines 1 to 6 in 14c0a9f
telegraf-session-local/lib/session.js
Lines 24 to 27 in 14c0a9f
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.
Thank you, your response was extremely helpful.
from telegraf-session-local.
@Iniro now you can access database (and sessions in it) in two ways:
- 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'))
- 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)
- created session value is missing on the "poll_answer" ctx HOT 1
- Problem with types HOT 8
- can't find module telegraf-session HOT 2
- Typo in the full example HOT 5
- How to use this session local with Scene HOT 2
- Session with AWS Lambdas does not persist state when it is set after an async operation. HOT 7
- issue with nodemon HOT 2
- Is it possible to change a user session? HOT 2
- session doesn't save to file with latest telegraf version > 4.8 HOT 2
- Is it possible to set an expiration time to a session? HOT 3
- telegraf-session-local kills my server when i am trying to connect session middleware HOT 4
- How many concurrent users this library can handle? HOT 3
- Session not cleared after async task
- Cannot find module './lodash-id.js HOT 2
- Save a session on bot start? HOT 3
- Session doesn't updated in 2nd middleware in row HOT 2
- Typescript typings
- Error with Session version 1.0.3 with telegraf-js v 3.38 HOT 1
- Property 'session' does not exist on type 'TelegrafContext' HOT 2
- Not all data is saved in a local file HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from telegraf-session-local.