Answer the question
In order to leave comments, you need to log in
How to make the telegram bot not take the previous message, but ask for a new one?
Decided to make a casino. Everything works, but there is one problem. When I enter a bet for the first time , the
bot does not ask for it next time, but takes the previous message. And in the end I get this:
Here is my code:
import random
import telebot
bot = telebot.TeleBot('мой токен')
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
balance = 100
while 1==1:
bot.send_message(message.from_user.id, "Ставка: ")
bet= (int(message.text))
if bet>balance:
bot.send_message(message.from_user.id, "Недостаточно монет")
continue
res = random.randint(1,2)
if res == 1:
balance = balance + bet
bot.send_message(message.from_user.id, "Вы выиграли!")
else:
balance = balance - bet
bot.send_message(message.from_user.id, "Вы проиграли!")
bot.send_message(message.from_user.id, "Баланс: "+str(balance))
bot.polling(none_stop=True, interval=0)
Answer the question
In order to leave comments, you need to log in
You should use register_next_step_handler
googled in 5 minutes, there are examples of use on the telebot library github. There were also a lot of questions about this function.
UPD:
Get rid of the infinite loop! you don't need him
The problem is that the handler has received a message object, after which you have a while 1==1 loop where bet=(int(message.text)) never changes.
Also, before you cast message.text to int, you should check if the message contains only numbers or not.
if (message.text).isdigit():
bet=(int(message.text))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question