You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.
Hello! I have problem with session-redis and i18n (actually problem exist and without i18n). I wish change language when user choosed. But overwriting session's value don't working. Overwriting works inside bot.use(), but not in bot.on('callback_query').
Thanks for help!
letTelegraf=require('telegraf');letTelegrafI18n=require('telegraf-i18n');letRedisSession=require('telegraf-session-redis');constMarkup=require('telegraf/markup');letconfig=require('config');letpath=require('path');letutils=require('./utils');letbot=newTelegraf(config.get('token'));consti18n=newTelegrafI18n({defaultLanguage: 'en',useSession: true,directory: path.resolve(__dirname,'locales')});constsession=newRedisSession({store: {host: config.get('redis.host'),port: config.get('redis.port'),db: config.get('redis.db')}});bot.use(session.middleware());bot.use(i18n.middleware());bot.use((ctx,next)=>{ctx.session.__language_code=ctx.session.__language_code||'en';next();});bot.start((ctx)=>{constmessage=ctx.i18n.t('main.hello');returnctx.reply(message);});bot.command('language',(ctx)=>{returnctx.reply(ctx.i18n.t('language.choose'),Markup.inlineKeyboard([Markup.callbackButton(ctx.i18n.t('language.en'),JSON.stringify({language: 'en'})),Markup.callbackButton(ctx.i18n.t('language.ru'),JSON.stringify({language: 'ru'}))]).extra());});bot.on('callback_query',(ctx)=>{letdata=false;try{data=JSON.parse(ctx.update.callback_query.data);}catch(err){console.log(err);}letmessage='';if(data.language!==undefined){letnewLanguage=data.language;ctx.i18n.locale(newLanguage);ctx.session.__language_code=newLanguage;// problem here: not overwrited variable in redismessage=ctx.i18n.t('language.changed');}returnctx.replyWithMarkdown(message);});bot.startPolling();
The text was updated successfully, but these errors were encountered:
Have the same issue. I've debugged this module and noticed that saveSession method called only once and before other middlewares call they "next" callbacks. In your case, you should add one more middleware such as:
Hello! I have problem with session-redis and i18n (actually problem exist and without i18n). I wish change language when user choosed. But overwriting session's value don't working. Overwriting works inside bot.use(), but not in bot.on('callback_query').
Thanks for help!
The text was updated successfully, but these errors were encountered: