telegraf / session

Official storage adapters for Telegraf v4.12+ sessions
MIT License
34 stars 6 forks source link

Please update the README.md #3

Closed InPRTx closed 1 year ago

InPRTx commented 1 year ago

https://github.com/telegraf/session/blob/872f978e8ed9fccece390d5231312ae6ebf18318/README.md?plain=1#L35 https://github.com/telegraf/session/blob/872f978e8ed9fccece390d5231312ae6ebf18318/README.md?plain=1#L61 https://github.com/telegraf/session/blob/872f978e8ed9fccece390d5231312ae6ebf18318/README.md?plain=1#L87 https://github.com/telegraf/session/blob/872f978e8ed9fccece390d5231312ae6ebf18318/README.md?plain=1#L116 https://github.com/telegraf/session/blob/872f978e8ed9fccece390d5231312ae6ebf18318/README.md?plain=1#L144 There are more unimported sessions, which made me confused at first what should be imported. But actually it should be done.

import {session} from "telegraf";

The lack of basic query statements makes me wonder why the output of console.log(ctx.session) is undefined But it's actually used like this.

import {Context, session, Telegraf} from "telegraf";
import {Update} from "telegraf/typings/core/types/typegram";
import {Mongo} from "@telegraf/session/mongodb";
import {SessionStore} from "telegraf/typings/session";

const BOT_TOKEN = process.env.BOT_TOKEN || ''

const store: SessionStore<object> = Mongo({
    url: "mongodb://127.0.0.1:27017",
    database: "telegraf-bot",
});

export const bot: Telegraf<Context<Update>> = new Telegraf(BOT_TOKEN)
bot.use(session({store}))

// bot.use(useNewReplies())

bot.on('message', async ctx => {
    const session = await store.set('test1', {'123': 123})
    console.log(session)
    const session2 = await store.get('test1')
    console.log(session2)
})
MKRhere commented 1 year ago

1

There are unimported sessions, which made me confused at first what should be imported.

This was intentionally left out to only show what is in scope of this package. Telegraf is also not imported, since they're both imported from the "telegraf" package:

import { Telegraf, session } from "telegraf";

I'll think about how to document this better.

2

But it's actually used like this.

No! Session works like this:

bot.use(session({ defaultSession: () => ({ count: 0 }) }));

bot.on("message", async ctx => {
    ctx.session.count = ctx.session.count + 1;
});

bot.command("count", async ctx => {
    await ctx.reply("Message count: " + ctx.session.count);
});

Whenever you read or write to ctx.session in a middleware, it will be updated to store. The key is the current update's ${ctx.chat.id}:${ctx.user.id}.

3

Also, these explicit types are unnecessary, you should let TypeScript infer the correct types:

- const store: SessionStore<object> = Mongo({
+ const store = Mongo({
- export const bot: Telegraf<Context<Update>> = new Telegraf(BOT_TOKEN);
+ export const bot = new Telegraf(BOT_TOKEN);

4

You should also avoid importing from telegraf internals. You can only import from "telegraf" or "telegraf/*" (one level, like telegraf/types, telegraf/format). Anything more will break in the future.

import {SessionStore} from "telegraf/typings/session";
InPRTx commented 1 year ago

I'm not very clear, but there is still a problem, my version is as follows

dependencies:
@telegraf/session 2.0.0-beta.6
@types/node 18.15.3
glob 9.3.0
kysely 0.23.5
mysql2 3.2.0

devDependencies:
@aws-sdk/client-s3 3.292.0             ejs 3.1.9                              multer 1.4.5-lts.1
@aws-sdk/client-sts 3.292.0            express 4.18.2                         node-modules-utils 0.8.2
@aws-sdk/s3-request-presigner 3.292.0  express-xml-bodyparser 0.3.0           nodemailer 6.9.1
@kubernetes/client-node 0.18.1         fs-extra 9.1.0                         nodemon 2.0.21
@lafjs/cloud 0.0.2                     jsonwebtoken 8.5.1                     telegraf 4.12.2
alipay-sdk 3.3.0                       lodash 4.17.21                         ts-node 10.9.1
axios 1.3.4                            log4js 6.9.1                           typescript 4.9.5
database-proxy 0.8.2                   minio 7.0.32                           validator 13.9.0
dayjs 1.11.7                           mongodb 4.14.0                         ws 8.13.0
dotenv 8.6.0                           mongodb-uri 0.9.7
  1. Here is working, but it warns me with TS2339 error

    TS2339: Property 'session' does not exist on type 'NarrowedContext  , MessageUpdate >'.

    image image

  2. But switch to store , it won't work and give me the following error image image image

MKRhere commented 1 year ago

Because we add something to Context (ctx.session), we want to let TypeScript know about it.

interface MyContext extends Context {
    session: Session; // define your Session type here
}

// tell Telegraf about your context type
const bot = new Telegraf<MyContext>(token);
bot.use(session({ store }));

bot.on("message", async ctx => {
    ctx.session = ...; // assign something to session
    console.log(ctx.session); // retrieve session
});

You should not try to do store.get or store.set yourself. Telegraf calls them internally.