telegraf-session-ggs
v1.1.2
Published
GGS powered session middleware for Telegraf
Downloads
23
Maintainers
Readme
GGS session middleware for Telegraf
GGS powered session middleware for Telegraf.
Installation
$ npm install telegraf-session-ggs
Example
JavaScript
const Telegraf = require('telegraf')
const GGSSession = require('telegraf-session-ggs')
const bot = new Telegraf(process.env.BOT_TOKEN)
const session = new GGSSession({
store: {
username: process.env.USER_NAME || 'Name',
password: process.env.PASSWORD || 12345,
projectName: process.env.PROJECT_NAME || 'PROJECT_NAME',
secreteKey: process.env.SECRETE_KEY || 'SECRETE_KEY'
}
})
bot.use(session)
bot.on('text', (ctx) => {
ctx.session.counter = ctx.session.counter || 0
ctx.session.counter++
console.log('Session', ctx.session)
})
bot.launch()
TypeScript
import Telegraf from 'telegraf'
import GGSSession from 'telegraf-session-ggs'
// Your code
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 ggsSession = new GGSSession()
// Retrieve session state by session key
ggsSession.getSession(key)
.then((session) => {
console.log('Session state', session)
})
// Save session state
ggsSession.saveSession(key, session)
API
Options
store
:username
: User namepassword
: PasswordprojectName
: 'Your project name'secreteKey
: 'Your secrete key'
property
: context property name (default:session
)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
})