telegraf-session-datastore
v0.1.9
Published
Cloud Datastore powered session middleware for Telegraf
Downloads
27
Maintainers
Readme
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
})