telegraf-session-ggs
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

Build Status NPM Version js-standard-style

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 name
    • password: Password
    • projectName: '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
})

Package Sidebar

Install

npm i telegraf-session-ggs

Weekly Downloads

0

Version

1.1.2

License

MIT

Unpacked Size

6.85 kB

Total Files

5

Last publish

Collaborators

  • garnik