telegraf / telegraf-session-redis

Redis session middleware for Telegraf
MIT License
50 stars 25 forks source link

Doesn't work with "inline_query" #13

Closed Fazendaaa closed 6 years ago

Fazendaaa commented 6 years ago

Right now I'm trying to use it the session in my bot.on('inline_query')... Out of the box doesn't work, my Redis is open and running, telegraf-session-redis is connected to it and, the funny part is that on my bot.on('text') it works.

Init:

const redisStorage = new RedisSession({
    property: 'redis',
    store: {
        host: process.env.TELEGRAM_SESSION_HOST,
        port: process.env.TELEGRAM_SESSION_PORT
    }
});

bot.use(redisStorage.middleware());

The problem:

bot.on('inline_query', async ({ i18n, answerInlineQuery, inlineQuery, redis }) => {
    // redis.counter += redis.counter || 0;

    console.log(redis);
});

outputs undefined

bot.on('text', async ({ i18n, message, replyWithMarkdown, redis }) => {
    console.log(redis);
});

outputs { }

Is this a good description of the problem that I'm having? note: right now I've done the TypeScript typings, just hoping to fix that first before making a PR :)

dotcypress commented 6 years ago

@Fazendaaa default getSessionKey implementation doesn't support inline_query, but you can provide custom session key resolver, with any custom logic.

Fazendaaa commented 6 years ago

@dotcypress Would it be okay for you if I made a logic for it and write the tests when I was going to do the TypeScript typings PR?

dotcypress commented 6 years ago

@Fazendaaa the main idea to make this implementation optional(you can add more than 1 session per bot), and it must be implemented by library user. For example:

const session = new RedisSession({
  store: {
    host: process.env.TELEGRAM_SESSION_HOST || '127.0.0.1',
    port: process.env.TELEGRAM_SESSION_PORT || 6379
  },
  // Session per user. In other words, session will be shared between chats
  getSessionKey:(ctx) => ctx.from && `${ctx.from.id}`
})