Forums

Problem with register_next_step_handler, telegram bot, pyTelegramBotAPI-3.7.7

Firstly,I have to say i dont know English very well, I will text my problem as clerly as can. My bot have to register new user, and redirect him to the test, after that user have to answer 9 questions and in for certain answers he gets points and in the end I have to show him test result and add all data from user in database(Sqlite3).

My problem is when user answer the question using keyboard(ReplyKeyboardMarkup) , user have to push the button several times to answer or sometimes bot redirect user to the next question method. It make total chaos.Idk how to fix this problem.Help please

 import telebot #Version pyTelegramBotAPI-3.7.7
 from flask import Flask, request
 import sqlite3

 #Configuration
 bot = telebot.TeleBot(config.TOKEN, threaded=False)
 bot.remove_webhook()
 bot.set_webhook(url=config.url)
 bot.enable_save_next_step_handlers(delay=2)
#First question
#All 9 question methods have same logic

#User have to answer using keyboards
def first_question(message):
chat_id=message.chat.id
text = message.text

#Find user using chat_id
user = user_dict[chat_id]

# #Answer options for 2 question
markup1 = types.ReplyKeyboardMarkup(one_time_keyboard=True,row_width=1)
markup1.add("Есептеу жұмыстарын орындау","Түрлі жабдықтардың нобайын жасау, жаңа техникалар ойлап шығару","Жарақаттанған адамдарға көмек көрсету","Заттардың бетіне, кітаптарға, қабырғаға салынған суреттерді тамашалау","Түрлі қатерден адамдарды қорғау және құтқару")

if text == "Шығынды есептеу":
    #Each answer has special points
    user.totalsum = user.totalsum + 1
    msg = bot.send_message(chat_id,"Сізге не ұнайды?\nМаған тек берілген варианттар арқылы жауап бересіз",reply_markup=markup1)
    bot.register_next_step_handler(msg, second_question)
elif text == "Машиналарды, құралдарды құрастыру":
    #Each answer has special points
    user.totalsum = user.totalsum + 2
    msg = bot.send_message(chat_id,"Сізге не ұнайды?\nМаған тек берілген варианттар арқылы жауап бересіз",reply_markup=markup1)
    bot.register_next_step_handler(msg, second_question)
elif text == "Табиғи құбылыстарға,өсімдіктердің өсуіне,т.б.бақылау жасау":
    #Each answer has special points
    user.totalsum = user.totalsum + 3
    msg = bot.send_message(chat_id,"Сізге не ұнайды?\nМаған тек берілген варианттар арқылы жауап бересіз",reply_markup=markup1)
    bot.register_next_step_handler(msg, second_question)
elif text == "Бақылаған немесе ойыңдағы оқиғаларды көркем тілмен суреттеу":
    #Each answer has special points
    user.totalsum = user.totalsum + 4
    msg = bot.send_message(chat_id,"Сізге не ұнайды?\nМаған тек берілген варианттар арқылы жауап бересіз",reply_markup=markup1)
    bot.register_next_step_handler(msg, second_question)
elif text == "Қиындыққа тап болғандарға көмек көрсететін органдарға жол сілтеу":
    #Each answer has special points
    user.totalsum = user.totalsum + 5
    msg = bot.send_message(chat_id,"Сізге не ұнайды?\nМаған тек берілген варианттар арқылы жауап бересіз",reply_markup=markup1)
    bot.register_next_step_handler(msg, second_question)
else:
    #If user dont answer using options ask again
     msg = bot.send_message(chat_id, "Жауабыңыз дұрыс емес,берілген варианттар арқылы жауап беріңіз1",)
     bot.register_next_step_handler(msg, first_question)
     return

After when user answer 1 question bot will redirect him to second question and etc . I copy-paste only part of code i dont wanna copy here all 600 lines of code if I didnt show you important and necessary part of code, I posted on github all code. My github repository:enter link description here

perhaps you could add some prints / some logging to figure out why the webapp is requiring several button pushes to respond etc? also are there any errors in your webapp error log?

See this. You could switch back to a single web workers, but note that that would mean that your webapp might respond slower.

There are no other solutions to this problem? for example, if I switch back to a single web worker, 10 people will write a message to the bot at the same time, that is, each user has to wait for the bot to finish processing one user? Can I use a different framework or do all bots have this logic?Please help, this is just my first production project.

Based on the limitations of pyTelegramBotAPI (that is that it assumes you're running in a single process), the best option would be to use a different telegram api that does not make that assumption. Alternatively, a more messy option, would be to use an always on task to hold the connection to telegram and then have the web process send messages to the always on task and have that send the messages to telegram.

Thank you for your responses. What framework can you suggest? To avoid such a problem again, I thought about the AIOGRAM framework (asynchronous framework for Python 3.7) is it suitable for this task? Aiogram repository:enter link description here

Maybe separate your bot code from the web app that users interact with, and as Glenn suggested put your bot into Always-on task. See Async work in Web apps | PythonAnywhere help