Classes

Session

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

Parameters:
NameTypeAttributesDescription
optionsSessionOptions<optional>

Session options

Returns:
Type: 
Session

Type Definitions

SessionOptions

Type:
  • object
Properties
NameTypeAttributesDescription
getSessionKeyfunction<optional>

Function for generating session key.

propertystring<optional>

Sets session property name in context

ttlnumber<optional>

Time to live

storeobject<optional>

Store