telegraf-session-datastore

0.1.9 • Public • Published

Build Status NPM Version js-standard-style

Datastore session for Telegraf

Cloud Datastore powered session middleware for Telegraf.

Installation

$ npm install telegraf-session-datastore

Example

const Telegraf = require('telegraf')
const datastoreSession = require('telegraf-session-datastore')
const { Datastore } = require('@google-cloud/datastore')

const db = new Datastore({
    projectId:   process.env.PROJECT_ID,
    keyFilename: process.env.KEY_FILENAME
})

const bot = new Telegraf(process.env.BOT_TOKEN)
bot.use(datastoreSession(db))
bot.on('text', (ctx, next) => {
  ctx.session.counter = ctx.session.counter || 0
  ctx.session.counter++
  return next()
})
bot.hears('/stats', ({ reply, session, from }) => reply(`${session.counter} messages from ${from.username}`))
bot.startPolling()

API

Options

  • property: context property name (default: session)
  • getSessionKey: session key resolver function (default: (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
})

Package Sidebar

Install

npm i telegraf-session-datastore

Weekly Downloads

1

Version

0.1.9

License

MIT

Unpacked Size

7.47 kB

Total Files

4

Last publish

Collaborators

  • rodrigosalinas