LocalSession

LocalSession

Represents a wrapper around locally stored session, it's middleware & lowdb

Constructor

new LocalSession(optionsopt)

Source:
Parameters:
Name Type Attributes Description
options Object <optional>

Options for LocalSession & lowdb

Properties
Name Type Attributes Description
database String <optional>

Name or path to database file default: 'sessions.json'

property String <optional>

Name of property in Telegraf Context where session object will be located default: 'session'

state Object <optional>

Initial state of database. You can use it to pre-init database Arrays/Objects to store your own data default: {}

getSessionKey function <optional>

Function to get identifier for session from Telegraf Context (may implement it on your own)

storage Object <optional>

lowdb storage option for implementing your own storage read/write operations default: LocalSession.storageFileSync

Properties
Name Type Attributes Description
read function <optional>

lowdb storage read function, must return an object or a Promise

write function <optional>

lowdb storage write function, must return undefined or a Promise

format Object <optional>

lowdb storage format option for implementing your own database format for read/write operations

Properties
Name Type Attributes Description
serialize function <optional>

lowdb storage serialize function, must return data (usually string) default: JSON.stringify()

deserialize function <optional>

lowdb storage deserialize function, must return an object default: JSON.parse()

Returns:

Instance of LocalSession

Members

(readonly) .storagefileAsync

Description:
Source:

lowdb storage named fileAsync before lowdb@0.17.0

(readonly) .storagefileSync

Description:
Source:

lowdb storage named fileSync before lowdb@0.17.0

Methods

getSession(key) → {Object}

Description:
  • Get session by it's key in database

Source:
Parameters:
Name Type Description
key String

Key which will be used to find associated session object

Returns:

Session data object or empty object if there's no session in database with this key

Type
Object

getSessionKey(ctx) → {String}

Description:
Source:
Parameters:
Name Type Description
ctx Object

Telegraf Context

Returns:

Session key in format number:number (chat.id:from.id)

Type
String

middleware(propertyopt) → {Promise}

Description:
  • Session middleware for use in Telegraf

Source:
Parameters:
Name Type Attributes Description
property String <optional>

Name of property in Telegraf Context where session object will be located (overrides property at LocalSession constructor)

Returns:
Type
Promise

(async) saveSession(key, data) → {Promise|function}

Description:
  • Save session to database

Source:
Parameters:
Name Type Description
key String

Unique Key which will be used to store session object

data Object

Session data object (if empty, session will be removed from database)

Returns:
  • Promise or Promise-like .then() function, with session object at 1-st argument
Type
Promise | function