- Source
Classes
Methods
(inner) sessionFactory(optionsopt) → {Session}
Creates session middleware with given store and options
Custom session property
You can set custom session property using property
option
bot.use(
session({
property: 'propName'
})
)
For this example, session available in ctx.propName
Custom session key
By default, session key in storage generated with this function:
(ctx) => {
return ctx.from && ctx.chat && `${ctx.from.id}:${ctx.chat.id}`
}
For example, you can redefine this function for storing session with chat key, like this:
(ctx) => {
return ctx.chat && `${ctx.chat.id}`
}
If you don't want to add session object, you can return null
or undefined
from this function.
For example, session working only in chat updates:
(ctx) => {
if (ctx.chat.type === 'private') return null // When chat type is private `ctx.session` not available
return ctx.chat && `${ctx.chat.id}`
}
TTL ( Time to live)
This parameter can set in ttl
option in seconds, expire time of session, by default session time not limited, but if you use in memory store like Map or other, session be destroyed after bot restart
Name | Type | Attributes | Description |
---|---|---|---|
options | SessionOptions | <optional> | Session options |
- Source
- Type:
- Session
Type Definitions
SessionOptions
- object
Name | Type | Attributes | Description |
---|---|---|---|
getSessionKey | function | <optional> | Function for generating session key. |
property | string | <optional> | Sets session property name in context |
ttl | number | <optional> | Time to live |
store | object | <optional> | Store |
- Source