Skip to content
This repository has been archived by the owner on Apr 28, 2023. It is now read-only.

Commit

Permalink
fix: readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-iakovlev committed Jan 20, 2023
1 parent d3d9668 commit 808fba8
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# grammy-named-session

grammy-named-session
Named session for grammY. Works the same way as [built-in session](https://grammy.dev/plugins/session.html) but allows you to set custom name for data access.

![GitHub CI](https://img.shields.io/github/actions/workflow/status/vlad-yakovlev/grammy-named-session/ci.yml?branch=main&label=github-ci)
[![Codecov](https://img.shields.io/codecov/c/github/vlad-yakovlev/grammy-named-session/main)](https://codecov.io/gh/vlad-yakovlev/grammy-named-session)
Expand All @@ -15,5 +15,57 @@ npm install grammy-named-session
## How to use

```ts
// TBD
import { Bot, Context } from 'grammy';
import { namedSession } from 'grammy-named-session';

interface Item {
id: string
value: string
}

interface MyState {
items: Item[]
users: Record<number, string>
}

interface MyContext extends Context {
myState: MyState
}

const myStateMiddleware = (dirName: string) => {
return namedSession<MyContext, 'myState'>({
// type MaybePromise<T> = Promise<T> | T

// Initial data, required
// (ctx: MyContext) => MaybePromise<MyContext['myState']>
getInitial: () => ({
items: [],
users: {},
}),

// Session key, required
// (ctx: MyContext) => MaybePromise<string | undefined>
getSessionKey: (ctx) =>
ctx.chat?.id === undefined ? undefined : `my_${ctx.chat.id}`,

// Storage adapter, required
// (ctx: MyContext) => MaybePromise<StorageAdapter<MyContext['myState']>>
getStorage: () => new FileAdapter({ dirName }),

// Access name, must be the same as in the types, required
// string
name: 'myState',
})
}

(async () => {
const bot = new Bot<MyContext>('<bot-token>');

bot.use(myStateMiddleware('db/v1'));

bot.command('start', async (ctx) => {
// Access data as usual but with custom name
await ctx.mySession.users[ctx.from.id] = ctx.from.first_name;
});
})()
```

0 comments on commit 808fba8

Please sign in to comment.