简体   繁体   English

我想用 python 为电报机器人编写一个计时器。 机器人从用户的 msg (str) 中获取“时间”。 如何将“时间”从 msg 转换为 (int) 类型?

[英]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)

First of all首先

Stop using time.sleep() in async functions.停止在异步函数中使用time.sleep() Use await asyncio.sleep() instead!使用await asyncio.sleep()代替!

Second第二

Learn basics of python .学习python基础知识。

Third第三

@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.

相关问题 我怎么知道电报机器人用户按下了按钮? - how can i know my telegram-bot user pressed a button? 如何使用我的电报机器人存储来自用户的输入,然后在需要时获取输入 - How can i store input from a user using my telegram bot and then fetch the input when i want 如何通过python电报机器人库在固定时间或间隔时间从机器人向用户发送消息? - How to send message from bot to user at a fixed time or at intervals through python telegram bot library? 如何从用户电报机器人获取信息? - How can I get information from the user telegram bot? 使用Python播放来自网址的音频(Telegram-bot) - Play Audio From Url using Python,(Telegram-bot) 为什么带有 Webhook 的 Python 上的电报机器人不能像带有长轮询的机器人那样同时处理来自许多用户的消息? - Why telegram-bot on Python with Webhooks can't process messages from many users simultaneously unlike a bot with Long Polling? Python电报机器人:提示输入另一个 - Python telegram-bot: Prompt for another input 在Telepot中运行handle(msg)(电报bot python软件包) - Run handle(msg) in telepot (telegram bot python package) Python。 如何从队列/主题 ActiveMQ 中删除任何消息 - Python. How to delete any msg from queue/topic ActiveMQ 如何在 python-telegram-bot 中接收来自用户的消息? - How can I receive messages from users in python-telegram-bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM