0.1.9 • Published 4 years ago

telegraf-session-datastore v0.1.9

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

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
})
0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago