D
D
DmtIDS2021-10-31 16:19:41
Python
DmtIDS, 2021-10-31 16:19:41

I can't figure out where the error is in a piece of code (Telegram bot on aiogram)?

It's kind of like a timer. The user sends a message to the bot with a number, and the bot displays a message in which the numbers from the given number to 0 change every second and performs some action, in my case it is the output of another message.

Displays the message "Time: input number", and that's it.

Here is the code itself:

import asyncio
from aiogram import Bot, executor, Dispatcher

bot = Bot(token='')
dp = Dispatcher(bot=bot)
bot_dispather = Dispatcher(bot=bot)


@dp.message_handler()
async def new_timer_message(message):
    try:
        timer_seconds = int(message.text)
        if timer_seconds <= 0:
            raise ValueError()

    except (TypeError, ValueError):
        await bot.send_message(chat_id=message.chat.id, text="Error")
        return
    new_message = await bot.send_message(chat_id=message.chat.id, text=f"Time: {timer_seconds}")

    for seconds_left in range(timer_seconds - 1, -1):
        await asyncio.sleep(1)
        await new_message.edit_text(f"Time: {timer_seconds}")

    await bot.send_message(chat_id=message.chat.id, text=f"Timeout")

if __name__ == "__main__":
    executor.start_polling(dp)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-10-31
@DmtIDS

1. Make out the code correctly, using a special button.
2. You can't make range() from larger to smaller without specifying a step.
Either eitherrange(timer_seconds - 1, -1, -1)reversed(range(timer_seconds))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question