# telegraf-mysql-session

> MySQL powered session middleware for Telegraf

Latest version **1.0.0** (published 2022-05-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install telegraf-mysql-session
pnpm add telegraf-mysql-session
yarn add telegraf-mysql-session
bun add telegraf-mysql-session
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-05-04 |
| First published | 2022-05-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.2.1 |
| Dependencies | 1 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| Author | Jamol Tokhtaev |
| Maintainers | jamol_tokhtaev |
| Keywords | telegram, telegram bot, telegraf, bot framework, session, middleware, mysql |

## Links

- npm: https://www.npmjs.com/package/telegraf-mysql-session
- npm.io page: https://npm.io/package/telegraf-mysql-session

## Dependencies (1)

- [mysql2](https://npm.io/package/mysql2.md) ^2.3.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2022-05-04

## README

[![Build Status](https://travis-ci.org/hnaderi/telegraf-mysql-session.svg?branch=master)](https://travis-ci.org/hnaderi/telegraf-mysql-session)
[![NPM Version](https://img.shields.io/npm/v/telegraf-mysql-session.svg?style=flat-square)](https://www.npmjs.com/package/telegraf-mysql-session)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)

# MySQL session middleware for Telegraf

MySQL powered session middleware for [Telegraf](https://github.com/telegraf/telegraf). forked from Redis session project for telegraf.
Saves session both on mysql and in memory and use memory where possible.

## Installation

```js
$ npm install telegraf-mysql-session
```
## Setup

you should create a table named sessions in your database.
```SQL
CREATE TABLE `sessions` (
  `id` varchar(100) NOT NULL,
  `session` longtext NOT NULL,
  PRIMARY KEY (`id`))
```

## Example

```js
const Telegraf = require('telegraf')
const MySQLSession = require('telegraf-mysql-session')

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

const session = new MySQLSession({
  host: 'localhost',
  user: 'user',
  password: 'pass',
  database: 'telegraf_sessions'
})

telegraf.use(session.middleware())

telegraf.on('text', (ctx) => {
  ctx.session.counter = ctx.session.counter || 0
  ctx.session.counter++
  console.log('Session', ctx.session)
})

telegraf.startPolling()
```

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.

```js
const mysqlSession = new MySQLSession()

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

// Save session state
mysqlSession.saveSession(key, session)
```

## API

### Options

* `host`:  hostname of mysql server
* `user`: username
* `password`: user password
* `database`:  Database name
* `property`: context property name (default: `session`)
* `getSessionKey`: session key function `(ctx) => any`)

Default implementation of `getSessionKey`:

```js
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`.

```js
telegraf.on('text', (ctx) => {
  ctx.session = null
})

```

---
_Source: https://npm.io/package/telegraf-mysql-session · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
