telegraf-postgres-session

1.0.5 • Public • Published

PostgreSQL session middleware for Telegraf

Installation

npm i telegraf-postgres-session

Setup

Create table in your database

CREATE TABLE postgress_sessions(id varchar PRIMARY KEY, session varchar);

Example

import { Telegraf } from 'telegraf';
import PostgresSession from 'telegraf-postgres-session';

const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here

bot.use((new PostgresSession({
	connectionString: process.env.DATABASE_URL,
		ssl: {
			rejectUnauthorized: false
		}
})).middleware());

bot.on('message', ctx => {
    ctx.session.counter = ctx.session.counter ? ctx.session.counter : 0
    ctx.session.counter++
    ctx.reply(ctx.session.counter)
});

bot.launch();

Example with Typescript

import { Context, Telegraf } from 'telegraf';
import PostgresSession from 'telegraf-postgres-session';

interface SessionContext extends Context {
 session: any;
};

const bot: Telegraf<SessionContext> = new Telegraf(process.env.BOT_TOKEN as string);// Your Bot token here

bot.use((new PostgresSession({
	connectionString: process.env.DATABASE_URL,
		ssl: {
			rejectUnauthorized: false
		}
})).middleware());

bot.on('message', ctx => {
    ctx.session.counter = ctx.session.counter ? ctx.session.counter : 0
    ctx.session.counter++
    ctx.reply(ctx.session.counter)
});

bot.launch();

The database connection configuration is described in The PostgreSQL API.

Package Sidebar

Install

npm i telegraf-postgres-session

Weekly Downloads

19

Version

1.0.5

License

MIT

Unpacked Size

5.84 kB

Total Files

4

Last publish

Collaborators

  • recker112