1.0.1 • Published 4 years ago
telegraf-session-azure-table-storage v1.0.1
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-storageExample
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 accountaccountKey: 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
})