M
M
Meezmo2021-06-17 20:56:22
Python
Meezmo, 2021-06-17 20:56:22

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:
60cb8bf263354187658082.png

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)


Sorry for writing so vaguely, I just don't know how to explain it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Krostelev, 2021-06-17
@Meezmo

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

B
Bl4ckm45k, 2021-06-18
@Bl4ckm45k

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 question

Ask a Question

731 491 924 answers to any question