recker112 / telegraf-postgres-session

MIT License
2 stars 3 forks source link

Pls add typescript support) #5

Closed J3imip closed 2 years ago

recker112 commented 2 years ago

Hi! So far it didn't use typescript, but I'll try to add its support!

J3imip commented 2 years ago

Hi! So far it didn't use typescript, but I'll try to add its support!

I'm waiting for updates. Warmest thanks!

recker112 commented 2 years ago

Hi! Can you send me how you define bot() in typescript? I think I may have found something but I'm not sure.

J3imip commented 2 years ago

Hi! Can you send me how you define bot() in typescript? I think I may have found something but I'm not sure.

Yeah, sure. You can check out my index.ts in repo - GodSounds.

There you can see how I created custom Session data and added it to Context. In Context.ts I defined all necessary interfaces and used it to create instance of Telegraf Bot class, so I won't have any problems with types whenever I'll try to get my session data in handlers and etc.

recker112 commented 2 years ago

I reviewed and I think the best solution in my opinion would be to add a small example to the documentation to extend Context. Maybe like that?

import { Context, Telegraf } from 'telegraf';
import PostgresSession from 'telegraf-postgres-session';

interface SessionContext extends Context {
 session: any;
};

const bot: Telegraf<SessionContext> = new Telegraf(process.env.BOT_TOKEN as string);// Your Bot token here

bot.use((new PostgresSession({
    connectionString: process.env.DATABASE_URL,
        ssl: {
            rejectUnauthorized: false
        }
})).middleware());

bot.on('message', ctx => {
    ctx.session.counter = ctx.session.counter ? ctx.session.counter : 0
    ctx.session.counter++
    ctx.reply(ctx.session.counter)
})

bot.launch()
J3imip commented 2 years ago

I reviewed and I think the best solution in my opinion would be to add a small example to the documentation to extend Context.

Maybe like that?


import { Context, Telegraf } from 'telegraf';

import PostgresSession from 'telegraf-postgres-session';

interface SessionContext extends Context {

 session: any;

};

const bot: Telegraf<SessionContext> = new Telegraf(process.env.BOT_TOKEN as string);// Your Bot token here

bot.use((new PostgresSession({

  connectionString: process.env.DATABASE_URL,

      ssl: {

          rejectUnauthorized: false

      }

})).middleware());

bot.on('message', ctx => {

    ctx.session.counter = ctx.session.counter ? ctx.session.counter : 0

    ctx.session.counter++

    ctx.reply(ctx.session.counter)

})

bot.launch()

I hope that's will work

recker112 commented 2 years ago

Done!

J3imip commented 2 years ago

I reviewed and I think the best solution in my opinion would be to add a small example to the documentation to extend Context.

Maybe like that?


import { Context, Telegraf } from 'telegraf';

import PostgresSession from 'telegraf-postgres-session';

interface SessionContext extends Context {

 session: any;

};

const bot: Telegraf<SessionContext> = new Telegraf(process.env.BOT_TOKEN as string);// Your Bot token here

bot.use((new PostgresSession({

  connectionString: process.env.DATABASE_URL,

      ssl: {

          rejectUnauthorized: false

      }

})).middleware());

bot.on('message', ctx => {

    ctx.session.counter = ctx.session.counter ? ctx.session.counter : 0

    ctx.session.counter++

    ctx.reply(ctx.session.counter)

})

bot.launch()

But that's still didn't solve my problem hahahah I'm sorry. I still can't import it and use it in typescript because your library doesn't have the type declaration or do not using typescript. Maybe I'll fork it and rewrite it on typescript)