Пишу мини программу для управление пк через бота в телеграмм. Одна из функций выключение пк(в будущем будет выбор выключить или перезагрузить). Последовательность действий должна быть:
1. Выбор действия(Sleep)
2.Выбор единиц времени(Здесь любой кроме 'по умолчания' т.к. там время не выбирается)
3. Ввод времени в соответствии с 2 п.
Проблема с 3 п. при вводе любого сообщения выводит 'Выберете время'
import telebot
from telebot import types
import os
token = ' '
bot = telebot.TeleBot(token)
@bot.message_handler(commands = ['start'])
def start(message):
chat = message.chat.id
markup = types.ReplyKeyboardMarkup()
btn_sleep = types.KeyboardButton('Sleep')
btn_live = types.KeyboardButton('Live')
btn_cmd = types.KeyboardButton('Cmd')
markup.row(btn_sleep)
markup.row(btn_live)
markup.row(btn_cmd)
bot.send_message(chat, f'Привет, {message.from_user.first_name}, вот тебе пару кнопок для управления Пк', reply_markup = markup)
#Выбор действия
@bot.message_handler()
def text(message):
chat = message.chat.id
text = message.text
if text == 'Sleep':
markup = types.InlineKeyboardMarkup()
btn_def = types.InlineKeyboardButton('По умолчания', callback_data = 'defolt')
btn_hour = types.InlineKeyboardButton('Час', callback_data = 'hour')
btn_minute = types.InlineKeyboardButton('Мин', callback_data = 'minute')
btn_second = types.InlineKeyboardButton('Сек', callback_data = 'second')
markup.row(btn_def)
markup.row(btn_hour, btn_minute, btn_second)
bot.send_message(chat, 'Выберете время', reply_markup = markup)
elif text == 'Live':
bot.send_message(chat, 'Выключение пк отменено')
elif text == 'Cmd':
bot.send_message(chat, 'Cmd открыт')
else:
bot.send_message(chat, 'Не могу распознать')
#Выбор е.д. врмени
@bot.callback_query_handler(func=lambda callback: True)
def callback_off(callback):
chat = callback.message.chat.id
message = callback.message
choice = 0
if callback.data == 'defolt':
bot.send_message(chat, 'Выключение пк через минуту')
elif callback.data == 'hour':
bot.send_message(chat, 'Введите время в часах')
choice = 1
elif callback.data == 'minute':
bot.send_message(chat, 'Введите время в минутах')
choice = 2
elif callback.data == 'second':
bot.send_message(chat, 'Введите время в секундах')
choice = 3
bot.register_next_step_handler(message, choice_time_off(message, choice = choice))
#Выбор времени
def choice_time_off(message, choice):
f = open('bat.bat', 'w')
choice_time = message.text
time = 60
if choice == 1:
time = 100
elif choice == 2:
time = 100
elif choice == 3:
time = 100
print(message.text)
f.write(f'shutdown -s -f -t {time}')
f.close()
os.startfile('bat.bat')
bot.infinity_polling()