Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/olexnzarov/telegraf-session-mongodb
MongoDB session middleware for Telegraf
https://github.com/olexnzarov/telegraf-session-mongodb
Last synced: about 2 months ago
JSON representation
MongoDB session middleware for Telegraf
- Host: GitHub
- URL: https://github.com/olexnzarov/telegraf-session-mongodb
- Owner: olexnzarov
- Created: 2018-11-01T08:44:58.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-05T21:41:07.000Z (over 1 year ago)
- Last Synced: 2024-08-02T11:10:03.673Z (about 2 months ago)
- Language: TypeScript
- Homepage:
- Size: 27.3 KB
- Stars: 26
- Watchers: 1
- Forks: 14
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MongoDB session middleware for Telegraf
MongoDB powered simple session middleware for [Telegraf 4.0](https://github.com/telegraf/telegraf) with TypeScript support.
## Installation
```js
$ npm install telegraf-session-mongodb
``````js
$ yarn add telegraf-session-mongodb
```## Example
[Full JavaScript example can be found here.](https://github.com/alexnzarov/telegraf-session-mongodb-examples/tree/master/javascript-example)
```js
const { Telegraf } = require('telegraf');
const { MongoClient } = require('mongodb');
const { session } = require('telegraf-session-mongodb');const bot = new Telegraf(process.env.BOT_TOKEN);
MongoClient.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(client => {
const db = client.db();
bot.use(session(db, { collectionName: 'sessions' }));
});
```## Example (TypeScript)
[Full TypeScript example can be found here.](https://github.com/alexnzarov/telegraf-session-mongodb-examples/tree/master/typescript-example)
```ts
import { Context, Telegraf } from 'telegraf';
import { MongoClient } from 'mongodb';
import { session } from 'telegraf-session-mongodb';export interface SessionContext extends Context {
session: any;
};const bot = new Telegraf(process.env.BOT_TOKEN);
MongoClient.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(client => {
const db = client.db();
bot.use(session(db, { sessionName: 'session', collectionName: 'sessions' }));
});
```## API
### Options
* `collectionName`: name for MongoDB collection (default: `sessions`)
* `sessionName`: context property name (default: `session`)
* `sessionKeyFn`: function that generates the session key from the context ([default implementation](https://github.com/alexnzarov/telegraf-session-mongodb/blob/master/src/keys.ts#L10-L16), [legacy deprecated function](https://github.com/alexnzarov/telegraf-session-mongodb/blob/master/src/keys.ts#L21-L31))