[英]I want to write a timer for telegram-bot with python. The bot gets "time" from user's msg (str). How can I convert "time" from msg to (int) type?
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)". “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) local_time = int(msg.from_user.id, msg.text)
TypeError: 'str' object cannot be interpreted as an integer类型错误:“str”对象不能解释为整数
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) int 函数需要文本作为第一个参数,第二个(可选)是基数(如果您使用 Python 来解释具有不同基数的字符串 - 即二进制,则需要它)
local_time = int(msg.text)
The msg.text is the user input (it must be a number) which it is casted to int. msg.text 是用户输入(它必须是一个数字),它被转换为 int。
If you process the input via a command handler you need to consider that the text message includes the command ie /start 12
.如果您通过命令处理程序处理输入,则需要考虑文本消息包含命令,即/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)
Stop using time.sleep()
in async functions.停止在异步函数中使用time.sleep()
。 Use await asyncio.sleep()
instead!使用await asyncio.sleep()
代替!
Learn basics of python
.学习python
基础知识。
@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! :)")
PS: Someone, please add aiogram
tag PS:有人请加aiogram
标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.