telegraf-session-azure-table-storage
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Build Status NPM Version js-standard-style

Azure table storage session middleware for Telegraf

Azure table storage powered session middleware for Telegraf. Forked from telegraf-session-redis

Installation

$ npm install telegraf-session-azure-table-storage

Example

const Telegraf = require('telegraf')
const AzureTableStorageSession = require('telegraf-session-azure-table-storage')

const bot = new Telegraf(process.env.BOT_TOKEN)

const session = new AzureTableStorageSession({
  store: {
    host: process.env.AZURE_STORAGE_ACCOUNT,
    port: process.env.AZURE_STORAGE_ACCESS_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()

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 azureSession = new AzureTableStorageSession()

// Retrieve session state by session key
azureSession.getSession(key)
  .then((session) => {
    console.log('Session state', session)
  })

// Save session state
azureSession.saveSession(key, session)

API

Options

  • store:
    • accountName: Azure storage account
    • accountKey: Azure storage account access 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-azure-table-storage

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

8.56 kB

Total Files

5

Last publish

Collaborators

  • pul