Add /shutdown and /reboot

This commit is contained in:
Ceda EI 2018-10-03 21:49:00 +05:30
parent 08e60121ae
commit 701419c6a9
1 changed files with 31 additions and 1 deletions

32
bot.py
View File

@ -32,7 +32,8 @@ def start(bot, update):
if not check_user(bot, update): if not check_user(bot, update):
return False return False
chat_id = update.message.chat_id chat_id = update.message.chat_id
custom_keyboard = [['/state', '/screenshot'], ['/lock', '/video']] custom_keyboard = [['/state', '/lock'],
['/screenshot', '/shutdown', '/reboot']]
reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
bot.send_message(chat_id=chat_id, text="What to do?", bot.send_message(chat_id=chat_id, text="What to do?",
reply_markup=reply_markup) reply_markup=reply_markup)
@ -87,6 +88,30 @@ def state(bot, update):
bot.send_message(chat_id=chat_id, text="Unlocked.") bot.send_message(chat_id=chat_id, text="Unlocked.")
def shutdown(bot, update):
if not check_user(bot, update):
return False
command = ["poweroff"]
chat_id = update.message.chat_id
bot.send_message(chat_id=chat_id, text="Attempting to shutdown.")
exitcode = subprocess.call(command)
chat_id = update.message.chat_id
if exitcode != 0:
bot.send_message(chat_id=chat_id, text="Shutdown failed.")
def reboot(bot, update):
if not check_user(bot, update):
return False
command = ["reboot"]
chat_id = update.message.chat_id
bot.send_message(chat_id=chat_id, text="Attempting to reboot.")
exitcode = subprocess.call(command)
chat_id = update.message.chat_id
if exitcode != 0:
bot.send_message(chat_id=chat_id, text="Reboot failed.")
updater = Updater(token=config.api_key) updater = Updater(token=config.api_key)
dispatcher = updater.dispatcher dispatcher = updater.dispatcher
@ -102,4 +127,9 @@ dispatcher.add_handler(lock_handler)
state_handler = CommandHandler('state', state) state_handler = CommandHandler('state', state)
dispatcher.add_handler(state_handler) dispatcher.add_handler(state_handler)
shutdown_handler = CommandHandler('shutdown', shutdown)
dispatcher.add_handler(shutdown_handler)
reboot_handler = CommandHandler('reboot', reboot)
dispatcher.add_handler(reboot_handler)
updater.start_polling() updater.start_polling()