I wrote a bot that measures the area of a rectangle using the pygrambotapi library. If only one user uses the bot, the bot returns the correct answer, but if many users use the bot at the same time, the bot mixes up the answers and results in incorrect answers. How do I know which user wrote which message?
a = ''
b = ''
float_pattern = r'^\d{1,7}\.\d{1,2}$'
...
...
def firstside(message):
global a
if message.text.isdigit() or re.match(float_pattern, message.text):
a = float(message.text)
print('a:', a, type(a))
bot.send_message(message.chat.id, "good!")
bot.register_next_step_handler(msg, secondside)
else:
msg = bot.send_message(message.chat.id, "Only number!")
bot.register_next_step_handler(msg, firstside)
def secondside(message):
global b
if message.text.isdigit() or re.match(float_pattern, message.text):
a = float(message.text)
print('b:', b, type(b))
bot.send_message(message.chat.id, "result = " +
f'{eval(str(a*b))})
else:
msg = bot.send_message(message.chat.id, "Only number!")
bot.register_next_step_handler(msg, firstside)
source https://stackoverflow.com/questions/71653804/pytelegrambotapi-user-id-message-id
Comments
Post a Comment