telegraf / telegraf-flow

[Deprecated] Control flow middleware for Telegraf
MIT License
38 stars 8 forks source link

Scene's inline_query listener throws an error #2

Closed drvirtuozov closed 7 years ago

drvirtuozov commented 7 years ago

I would like to use different inline query answers in different scenes, but it throws an error:


const bot = new Telegraf();
const flow = new TelegrafFlow();
const testScene = new Scene('test_scene');

bot.use(Telegraf.memorySession());
flow.register(testScene);

flow.command('test', ctx => {
  ctx.flow.enter('test_scene');
});

testScene.command('cancel', ctx => {
  ctx.flow.leave();
  ctx.reply('Canceled');
});

testScene.on('inline_query', ctx => { // throws Error: telegraf-flow: Can't find session.
  ctx.telegram.sendMessage(ctx.from.id, 'inline query answer from test_scene');
});

testScene.enter(ctx => {
  ctx.reply('Your inline query... /cancel');
});

bot.use(flow.middleware());

bot.on('inline_query', ctx => { // works well without flow.middleware
  ctx.telegram.sendMessage(ctx.from.id, 'global inline query answer');
});
dotcypress commented 7 years ago

Actually this it's not a bug, session is disabled by default for inline queries cause it's not clear how to handle session for one user in different chats.

You can provide custom session key resolver as following:

bot.use(Telegraf.memorySession({
  getSessionKey: (ctx) => ctx.from && `${ctx.from.id}:${ctx.chat.id || 'defaultChatId or something'}`
}))