5.1.0 • Published 7 years ago

telegraf-session-redis v5.1.0

Weekly downloads
271
License
MIT
Repository
github
Last release
7 years ago

Build Status NPM Version js-standard-style

Redis session middleware for Telegraf

Redis powered session middleware for Telegraf.

Installation

$ npm install telegraf-session-redis

Example

const Telegraf = require('telegraf')
const RedisSession = require('telegraf-session-redis')

const bot = new Telegraf(process.env.BOT_TOKEN)

const session = new RedisSession({
  store: {
    host: process.env.TELEGRAM_SESSION_HOST || '127.0.0.1',
    port: process.env.TELEGRAM_SESSION_PORT || 6379
  }
})

bot.use(session)

bot.on('text', (ctx) => {
  ctx.session.counter = ctx.session.counter || 0
  ctx.session.counter++
  console.log('Session', ctx.session)
})

bot.launch()

When you have stored the session key beforehand, you can access a session without having access to a context object. This is useful when you perform OAUTH or something similar, when a REDIRECT_URI is called on your bot server.

const redisSession = new RedisSession()

// Retrieve session state by session key
redisSession.getSession(key)
  .then((session) => {
    console.log('Session state', session)
  })

// Save session state
redisSession.saveSession(key, session)

API

Options

  • store:
  • property: context property name (default: session)
  • ttl: session ttl in seconds (default: forever)
  • getSessionKey: session key resolver function (ctx) => any)

Default implementation of getSessionKey:

function getSessionKey (ctx) {
  if (!ctx.from || !ctx.chat) {
    return
  }
  return `${ctx.from.id}:${ctx.chat.id}`
}

Destroying a session

To destroy a session simply set it to null.

bot.on('text', (ctx) => {
  ctx.session = null
})
5.1.0

7 years ago

5.0.0

8 years ago

4.2.0

9 years ago

4.1.0

9 years ago

4.0.0

9 years ago

3.0.0

9 years ago

2.0.0

9 years ago

1.0.0

9 years ago

0.8.0

10 years ago

0.7.0

10 years ago

0.6.0

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago