# telegraf-session-dynamodb-temp-fork

> DynamoDB session middleware for Telegraf

Latest version **1.4.0** (published 2022-07-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install telegraf-session-dynamodb-temp-fork
pnpm add telegraf-session-dynamodb-temp-fork
yarn add telegraf-session-dynamodb-temp-fork
bun add telegraf-session-dynamodb-temp-fork
```

## 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.4.0 |
| Published | 2022-07-27 |
| First published | 2022-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.10.3 |
| Dependencies | 0 |
| Unpacked size | 16.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ness Li |
| Maintainers | voborgus |
| Keywords | telegram, telegram bot, telegraf, bot framework, session, middleware, DynamoDB, Lambda, AWS |

## Links

- npm: https://www.npmjs.com/package/telegraf-session-dynamodb-temp-fork
- Repository: https://github.com/voborgus/telegraf-session-dynamodb
- Homepage: https://github.com/voborgus/telegraf-session-dynamodb#readme
- Issues: https://github.com/voborgus/telegraf-session-dynamodb/issues
- npm.io page: https://npm.io/package/telegraf-session-dynamodb-temp-fork

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.4.0 (latest) — 2022-07-27

## README

[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) [![Build Status](https://travis-ci.org/nessgor/telegraf-session-dynamodb.svg?branch=master)](https://travis-ci.org/nessgor/telegraf-session-dynamodb) 

[![NPM](https://nodei.co/npm/telegraf-session-dynamodb.png)](https://nodei.co/npm/telegraf-session-dynamodb/)

# AWS DynamoDB session middleware for Telegraf

AWS DynamoDB powered session middleware for [Telegraf](https://github.com/telegraf/telegraf).

## Prerequisites

1. You have made your AWS access and secret key available through a provided method, like storing them in the ~/.aws/credentials file or export them into environment variables
2. You need to install Node.js  with a minimum version of 6.10.3 

## Installation

```bash
foo@bar:~$ yarn add telegraf-session-dynamodb
```

## Example

```js
const Telegraf = require('telegraf')
const DynamoDBSession = require('telegraf-session-dynamodb')

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

const dynamoDBSession = new DynamoDBSession({
    dynamoDBConfig: {
        params: {
            TableName: process.env.AWS_DYNAMODB_TABLE
        },
        region: process.env.AWS_REGION
    }
})
bot.use(dynamoDBSession.middleware())

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

bot.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 dynamoDBSession = new DynamoDBSession({
    dynamoDBConfig: {
        params: {
            TableName: process.env.AWS_DYNAMODB_TABLE
        },
        region: process.env.AWS_REGION
    }
})

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

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

## API

### Options

* `dynamoDBConfig`:
  * `params`: 
    * `TableName`: AWS DynamoDB Table to store session (default: *telegraf-session-dynamodb*)
  * `region`: AWS Region (default: *ap-northeast-1*)
* `property`: context property name (default: `session`)
* `ttl`: Time To Live in minutes, -1 for never expire (default: `-1`)
* `getSessionKey`: session key resolver 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
bot.on('text', (ctx) => {
  ctx.session = null
})

```

### Local Unit Testing

```bash
foo@bar:~$ yarn

foo@bar:~$ yarn global add serverless

foo@bar:~$ docker pull lambci/lambda

foo@bar:~$ sls dynamodb install

foo@bar:~$ sls offline start -r ap-northeast-1 --noTimeout &

foo@bar:~$ yarn test:local

```
Remarks: TTL will **NOT** work for DynamoDB Local

### Remote Unit Testing

1. Create the AWS DynamoDB Table in the desired AWS Region
2. Use `SessionKey (String)` as primary key
3. Set `ttl` as the TTL attribute in Manage TTL

```bash
foo@bar:~$ aws configure

foo@bar:~$ yarn

foo@bar:~$ yarn test

```

### Acknowledgement
* [telegraf-session-redis](https://github.com/telegraf/telegraf-session-redis)

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