Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

RethinkDB session middleware for Telegraf

License

Notifications You must be signed in to change notification settings

deptyped/telegraf-session-rethinkdb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RethinkDB session middleware for Telegraf

RethinkDB store-based session middleware for Telegraf (Telegram bot framework).

Installation

$ npm install @xqd/telegraf-session-rethinkdb --save

Example

const Telegraf = require('telegraf')
const RethinkSession = require('@xqd/telegraf-session-rethinkdb')
const r = require('rethinkdb')

const telegraf = new Telegraf(process.env.TOKEN)

const store = {
	host: process.env.RETHINKDB_HOST,
	port: process.env.RETHINK_PORT
}

r.connect(store).then(async (connection) => {
	let session = new RethinkSession(connection/*, options */)

	await session.setup()

	telegraf.use(session.middleware)

	telegraf.use((ctx, next) => {
	  ctx.session.counter = ctx.session.counter || 0
	  ctx.session.counter++
	  console.log('->', ctx.session) // -> { counter: 1, id: 'chatId:fromId' }

	  next()
	})

	telegraf.startPolling()
})

API

Options

  • property: context property name (default: "session")
  • table: table name (default: "_telegraf_session")
  • db: db name (default: "test")
  • getSessionKey: session key function (context -> string)

Default session key depends on sender/chat:

function getSessionKey(ctx) {
  return `${ctx.from.id}:${ctx.chat.id}`
}

Destroying a session

To destroy a session simply set it to null.

ctx.session = {}

Credits

Used these projects as inspiration:

About

RethinkDB session middleware for Telegraf

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published