-1

I wrote this code and I need to get "local time" from user's message (string type).
But I need this like integer to set timer.
There is TypeError in

"local_time = int(msg.from_user.id, msg.text)"

How can I fix it?

from aiogram import Bot, types from aiogram.dispatcher import Dispatcher from aiogram.utils import executor import time
from Config import TOKEN

bot = Bot(token=TOKEN)
dp = Dispatcher(bot)


@dp.message_handler(commands=['start'])
async def process_start_command(message: types.Message):
    await message.reply("Hi!")


@dp.message_handler(commands=['help'])
async def process_help_command(message: types.Message):
    await message.reply("/timer - set timer")


@dp.message_handler(commands=['timer'])
async def set_timer(msg: types.Message):
    await bot.send_message(msg.from_user.id, text='How many minutes?')
    time.sleep(5)
    local_time = int(msg.from_user.id, msg.text)
    local_time_b = int(local_time * 60)
    await bot.send_message(msg.from_user.id, text='Timer set')
    time.sleep(local_time_b)
    await bot.send_message(msg.from_user.id, text='The timer has worked')

print("Hello")

if __name__ == '__main__':
    executor.start_polling(dp)
local_time = int(msg.from_user.id, msg.text)

TypeError: 'str' object cannot be interpreted as an integer
moken
  • 3,227
  • 8
  • 13
  • 23
LogDog
  • 3
  • 3

2 Answers2

0

The int function requires the text as first parameter, the second (optional) is the base (which you need if you Python to interpret the string with a different base - ie binany)

local_time = int(msg.text)

The msg.text is the user input (it must be a number) which it is casted to int.

If you process the input via a command handler you need to consider that the text message includes the command ie /start 12.
One option is to remove the command and obtain the following value(s)

# remove '/start'
interval = msg.text[7:]
local_time = int(interval)
print(local_time)
Beppe C
  • 11,256
  • 2
  • 19
  • 41
  • Doesn't work. local_time = int(msg.text) ValueError: invalid literal for int() with base 10: '/timer' – LogDog Nov 01 '20 at 18:30
  • what does the user type? it must be a valid number (which the code tried to cast to int to set it for the timer) – Beppe C Nov 01 '20 at 19:34
  • I'm the user. I write an integer ("1", for example) and I get ValueError. What should I do? – LogDog Nov 01 '20 at 19:58
  • You try to cast the number catching any exception. If there is an exception you can ask please enter a valid number – Beppe C Nov 01 '20 at 20:08
  • Compiler need a number to set timer. I send this number, but compiler thinks this is string. How can I convert string ("1") to integer (1)? This is my question. – LogDog Nov 02 '20 at 16:30
  • I used. local_time = int(msg.text) ValueError: invalid literal for int() with base 10: '/timer' – LogDog Nov 03 '20 at 19:00
  • The text message is the actual command `/timer`, which is not a valid number. I added in the answer how to extract the value after the command `/timer 10` – Beppe C Nov 04 '20 at 09:03
-1

First of all

Stop using time.sleep() in async functions. Use await asyncio.sleep() instead!

Second

@dp.message_handler(commands=['timer'])
async def timer_handler(message: Message):
    # get args from message (it's `str` type!) 
    timer_string = message.get_args()
    
    # let's try to convert it to `int`
    try:
        timer = int(timer_string)
    except (ValueError, TypeError):
        return await message.answer("Please set a digit for timer. E.g.: /timer 5")
    
    # success! timer is set!
    await message.answer(f'Timer set for {timer}')
    
    # sleeping (this way, instead of blocking `time.sleep`)
    await asyncio.sleep(timer)
    
    # it's time to send message
    await message.answer("It's time! :)")
Oleg
  • 523
  • 2
  • 10