A
A
Alexander Khomutov2021-10-16 17:53:57
Python
Alexander Khomutov, 2021-10-16 17:53:57

Why is my telegram bot looping itself?

Good afternoon, help please.
What am I doing wrong? Google doesn't help me. I don't know if it's possible to add links to the bot (RandomKhomutovBot
)
But when I run it in the cart, it starts the first loop endlessly without waiting for the user's response)
616ae6f530f26514227618.jpeg

code text here

import telebot
from random import randint
from time import sleep

bot = telebot.TeleBot('2040348328:AAGELseRCTUreCZKC3MhqM3FyE9ZTKh4pAw')

@bot.message_handler(content_types=['text'])
def get_text_messages(message):
bot.send_message(message.from_user.id, "Привет, я умею генерировать случайные числа, в пределах заданного диапазона")
sleep(2)
bot.send_message(message.from_user.id, "Введи наименьшее число диапазона")
while True:
try:
num_1 = int(message.text)
except Exception:
bot.send_message(message.from_user.id, 'Цифрами, пожалуйста')
continue
bot.send_message(message.from_user.id, "Введи наибольшее число диапазона")
num_2 = int(message.text)
if num_1 > num_2:
num_1, num_2 = num_2, num_1
bot.send_message(message.from_user.id, randint(num_1, num_2))
sleep(1)
bot.send_message(message.from_user.id, "Ещё раз?(да/нет)")
if message.text.lower == 'да':
bot.send_message(message.from_user.id, "Введи наименьшее число диапазона")
continue

bot.polling(none_stop=True, interval=0)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-10-16
@khomut

Why is it looping? Because you added an infinite loop.

without waiting for the user's response

He won't wait. If user input is needed - use bot.register_next_step_handler()
https://github.com/eternnoir/pyTelegramBotAPI/blob...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question