Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Well, think about how the bot will understand what someone else wrote? How to identify a user: by nickname or by id. Then figure out how to store data separately, maybe start a database or stupidly in RAM - in dictionaries. There are many solutions.
Well, depending on which library you use, but in general, in general, you need to remember the steps for each user and then look at which step the user
The general solution is to create a data structure about the user with the entered numbers and state, each time a message is received, check the state of the user, it is better to store states as Enum
If you are using pyTelegramBotApi, then you can use bot.register_next_step_handler , official example: https://github.com /eternnoir/pyTelegramBotAPI/blob...
from enum import Enum
class UserStates(Enum):
PARAM_1 = 0
PARAM_2 = 1
class UserInfo:
state = UserStates.PARAM_1
param_1 = 0
param_2 = 0
users = {}
# обработчик текстового сообщения от пользователя
# userId - любой спосо идентификации пользователя (TG User ID / Chat ID / etc)
if (users.has_key(userId)):
if (users[user_id].state == UserStates.PARAM_1):
# нам отправили первое число
users[user_id].param_1 = # int (сообщение)
# зпросить второе число
users[user_id].state = UserStates.PARAM_2
elif (users[user_id].state == UserStates.PARAM_2):
# отправили 2е число
users[user_id].param_2 = # int (сообщение)
#результат
result = users[user_id].param_1 + users[user_id].param_2
# готовы снова принять первый параметр для нового вычисления
users[user_id].state = UserStates.PARAM_1
else:
users[user_id] = UserInfo()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question