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
Hi. I've function to register bot users before they can use it. Here is i'm using middleware to check auth of user for all incoming requests, if user is not registered then enters to wizard scene, where i have couple of steps to collect user info. To make better user experience i want to allow users to call telegram commands when they are already in wizard scene.
Telegraf.js Version: 4.14.0
Node.js Version: 18.18.2
Operating System: macOS Ventura 13.4
Used modules: @telegraf/session/redis, i18n-telegraf
Minimal Example Code Reproducing the Issue
consthandleMyChatMemberUpdate=(ctx)=>{if(ctx.update.my_chat_member){console.log('my_chat_member chat_id',ctx.update.my_chat_member.chat.id,' status: ',ctx.update.my_chat_member.new_chat_member.status)console.log('ctx.session',ctx.session)if(ctx.update.my_chat_member.new_chat_member.status=='kicked'){ctx.scene.reset();ctx.scene.state={};ctx.session.order={};}else{}}}constcontactDataWizard=newScenes.WizardScene('CONTACT_DATA_WIZARD_SCENE_ID',async(ctx)=>{// console.log('contactDataWizard:0 update:', ctx.update)console.log('scene','CONTACT_DATA_WIZARD_SCENE_ID','0','enter')if(ctx.update.my_chat_member)returnhandleMyChatMemberUpdate(ctx);if(ctx.update.message)awaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id);ctx.wizard.state.contactData={};awaitSHOW_LANG_MENU(ctx);console.log('scene','CONTACT_DATA_WIZARD_SCENE_ID','0','next')returnctx.wizard.next();},//start scene, show lang menu then switch to next sceneasync(ctx)=>{// console.log('contactDataWizard:1 message:', ctx.update.message, ' callback:',ctx.update.callback_query )console.log('scene','CONTACT_DATA_WIZARD_SCENE_ID','1','enter')if(ctx.update.my_chat_member)returnhandleMyChatMemberUpdate(ctx);if(ctx.message&&ctx.message.text?.startsWith('/')){//check if user called commandawaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id);console.log('scene','CONTACT_DATA_WIZARD_SCENE_ID','1','leave')returnawaitctx.scene.leave();}//if user clicked callback buttonif(ctx.update.callback_query){awaitctx.answerCbQuery();if(ctx.update.callback_query.data==='kz'){ctx.wizard.state.contactData.language='kz';ctx.i18n.locale("kz");}elseif(ctx.update.callback_query.data==='ru'){ctx.i18n.locale("ru");ctx.wizard.state.contactData.language='ru';}}else{//unhandled casesif(ctx.update.message)awaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id);returnSHOW_LANG_MENU(ctx);}awaitctx.reply(ctx.i18n.t("welcome"),authKeyboard(ctx.i18n.t('share_number'))).then(reply=>SAVE_MSG_ID(reply.chat.id,reply.message_id))console.log('scene','CONTACT_DATA_WIZARD_SCENE_ID','1','next')returnctx.wizard.next();},//expecting callback queryasync(ctx)=>{if(ctx.update.my_chat_member)returnhandleMyChatMemberUpdate(ctx);if(ctx.message&&ctx.message.text?.startsWith('/')){//check if user called commandawaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id)returnctx.scene.leave();}awaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id);if(!ctx.message.contact){//handle if user not shared contact datareturnctx.reply(ctx.i18n.t("welcome"),authKeyboard(ctx.i18n.t('share_number'))).then(reply=>SAVE_MSG_ID(reply.chat.id,reply.message_id))}ctx.wizard.state.contactData.phone_number=helper.normalizePhoneNumber(ctx.message.contact.phone_number);ctx.wizard.state.contactData.first_name=ctx.message.contact.first_name;ctx.wizard.state.contactData.last_name=ctx.message.contact.last_name;ctx.wizard.state.contactData.user_id=ctx.message.contact.user_id;awaitctx.reply(ctx.i18n.t('enter_full_name'),removeKeyboard).then(reply=>SAVE_MSG_ID(reply.chat.id,reply.message_id));returnctx.wizard.next();},//expecting contactasync(ctx)=>{if(ctx.update.my_chat_member)returnhandleMyChatMemberUpdate(ctx);if(ctx.message&&ctx.message.text?.startsWith('/')){//check if user called commandawaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id)returnctx.scene.leave();}awaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id);if(ctx.message&&!helper.isValidName(ctx.message.text))//when user not send correct namereturnctx.reply(ctx.i18n.t('enter_correct_full_name')).then(reply=>SAVE_MSG_ID(reply.chat.id,reply.message_id));ctx.wizard.state.contactData.fullname=ctx.message.text;awaitctx.replyWithMarkdown(ctx.i18n.t('accept_rules_text'),Markup.inlineKeyboard(KEYBOARD(ctx,KEYBOARD_TYPE.ACCEPT_RULES)).resize().oneTime()).then(reply=>SAVE_MSG_ID(reply.chat.id,reply.message_id))returnctx.wizard.next();},//expecting user name in textasync(ctx)=>{if(ctx.update.my_chat_member)returnhandleMyChatMemberUpdate(ctx);if(ctx.update.callback_query&&ctx.update.callback_query.data==='accept'){ctx.wizard.state.contactData.chat_id=ctx.chat.id;awaitSAVE_AUTH(ctx.wizard.state.contactData);awaitCLEAR_MSGS(ctx);ctx.reply(ctx.i18n.t('welcome_to_service')).then(reply=>SAVE_MSG_ID(reply.chat.id,reply.message_id))awaitSHOW_MAIN_MENU(ctx)returnctx.scene.leave();}else{ctx.replyWithMarkdown(ctx.i18n.t('accept_rules_text'),Markup.inlineKeyboard(KEYBOARD(ctx,KEYBOARD_TYPE.ACCEPT_RULES)).resize().oneTime()).then(reply=>SAVE_MSG_ID(reply.chat.id,reply.message_id))return;}});bot.use(async(ctx,next)=>{console.log('ctx use',ctx)if(ctx.update.callback_query)//skip all stuff if callback queryreturnnext();elseif(ctx.update.my_chat_member){if(ctx.update.my_chat_member.new_chat_member.status=='kicked')returnhandleMyChatMemberUpdate(ctx);}elseif(ctx.update.message.photo){//skipp all stuff if photo received, but keep msg idawaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id);returnnext();}awaitCLEAR_MSGS(ctx);//clear all messages from historyif(ctx.update.message&&ctx.update.message.message_id)awaitSAVE_MSG_ID(ctx.chat.id,ctx.update.message.message_id);//save incoming message idconstuser=awaitCHECK_AUTH(ctx.chat.id)if(user){awaitctx.i18n.locale(user.language)awaitctx.telegram.setMyCommands([{command:"start",description:ctx.i18n.t('main_menu')}],{scope:{type:'chat',chat_id: ctx.chat.id}})console.log('middleware:auth passed',ctx.chat.id);returnnext();//go next if user authorized}else{//otherwise, it is unauthorized user, enter scenectx.scene.enter('CONTACT_DATA_WIZARD_SCENE_ID')}})
Expected Behavior
Unauthorized user start bot, then goes to contactDataWizard, first step shows language menu then switches to next step, second step handles that user called a command, then handler should leave scene then call next() of middleware to give a chance to handle commands.
Current Behavior
If unauthorized user in step 2 (that expects callback_query), but user called /start again, my code handles this case and seems like leaves the scene, but nothing happening, but if i call /start again, user enters contactDataWizard again but not switch to step 2, but executes step 1 again. But step 1 calls return ctx.wizard.next(); anyways.
screen.mov
The text was updated successfully, but these errors were encountered:
Context
Hi. I've function to register bot users before they can use it. Here is i'm using middleware to check auth of user for all incoming requests, if user is not registered then enters to wizard scene, where i have couple of steps to collect user info. To make better user experience i want to allow users to call telegram commands when they are already in wizard scene.
@telegraf/session/redis
,i18n-telegraf
Minimal Example Code Reproducing the Issue
Expected Behavior
Unauthorized user start bot, then goes to
contactDataWizard
, first step shows language menu then switches to next step, second step handles that user called a command, then handler should leave scene then callnext()
of middleware to give a chance to handle commands.Current Behavior
If unauthorized user in step 2 (that expects callback_query), but user called
/start
again, my code handles this case and seems like leaves the scene, but nothing happening, but if i call/start
again, user enterscontactDataWizard
again but not switch to step 2, but executes step 1 again. But step 1 callsreturn ctx.wizard.next();
anyways.screen.mov
The text was updated successfully, but these errors were encountered: