Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
python-telegram-bot>=10.0.0
python-telegram-bot>=12.0.0
requests>=2.0.0
78 changes: 39 additions & 39 deletions wolbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import re

from telegram import (InlineKeyboardButton,
InlineKeyboardMarkup)
InlineKeyboardMarkup,
Update)
from telegram.ext import (Updater,
CommandHandler,
MessageHandler,
Filters,
CallbackQueryHandler)
CallbackQueryHandler,
CallbackContext)
import requests
import version
import config
Expand All @@ -38,7 +38,7 @@ def __init__(self, mid, name, addr):
# Command Handlers
##

def cmd_help(bot, update):
def cmd_help(update: Update, context: CallbackContext):
log_call(update)
help_message = """
(*≧▽≦) WOLBOT v{v} (≧▽≦*)
Expand Down Expand Up @@ -71,14 +71,14 @@ def cmd_help(bot, update):
update.message.reply_text(help_message)


def cmd_wake(bot, update, **kwargs):
def cmd_wake(update: Update, context: CallbackContext):
log_call(update)
# Check correctness of call
if not authorize(bot, update):
if not authorize(update, context):
return

# When no args are supplied
if 'args' not in kwargs or len(kwargs['args']) < 1 and len(machines) != 1:
if len(context.args) < 1 and len(machines) != 1:
if not len(machines):
update.message.reply_text('Please add a machine with the /add command first!')
markup = InlineKeyboardMarkup(generate_machine_keyboard(machines))
Expand All @@ -89,43 +89,43 @@ def cmd_wake(bot, update, **kwargs):
if len(machines) == 1:
machine_name = machines[0].name
else:
machine_name = kwargs['args'][0]
machine_name = context.args[0]
for m in machines:
if m.name == machine_name:
send_magic_packet(bot, update, m.addr, m.name)
send_magic_packet(update, context, m.addr, m.name)
return
update.message.reply_text('Could not find ' + machine_name)


def cmd_wake_keyboard_handler(bot, update):
def cmd_wake_keyboard_handler(update: Update, context: CallbackContext):
try:
n = int(update.callback_query.data)
except ValueError:
pass
matches = [m for m in machines if m.id == n]
if len(matches) < 1:
return
send_magic_packet(bot, update, matches[0].addr, matches[0].name)
send_magic_packet(update, context, matches[0].addr, matches[0].name)


def cmd_wake_mac(bot, update, **kwargs):
def cmd_wake_mac(update: Update, context: CallbackContext):
log_call(update)
# Check correctness of call
if not authorize(bot, update):
if not authorize(update, context):
return
if 'args' not in kwargs or len(kwargs['args']) < 1:
if len(context.args) < 1:
update.message.reply_text('Please supply a mac address')
return

# Parse arguments and send WoL packets
mac_address = kwargs['args'][0]
send_magic_packet(bot, update, mac_address, mac_address)
mac_address = context.args[0]
send_magic_packet(update, context, mac_address, mac_address)


def cmd_list(bot, update):
def cmd_list(update: Update, context: CallbackContext):
log_call(update)
# Check correctness of call
if not authorize(bot, update):
if not authorize(update, context):
return

# Print all stored machines
Expand All @@ -135,18 +135,18 @@ def cmd_list(bot, update):
update.message.reply_text(msg)


def cmd_add(bot, update, **kwargs):
def cmd_add(update: Update, context: CallbackContext):
log_call(update)
# Check correctness of call
if not authorize(bot, update):
if not authorize(update, context):
return
if 'args' not in kwargs or len(kwargs['args']) < 2:
if len(context.args) < 2:
update.message.reply_text('Please supply a name and mac address')
return

# Parse arguments
machine_name = kwargs['args'][0]
addr = kwargs['args'][1]
machine_name = context.args[0]
addr = context.args[1]

# Validate and normalize arguments
if any(m.name == machine_name for m in machines):
Expand Down Expand Up @@ -174,17 +174,17 @@ def cmd_add(bot, update, **kwargs):
update.message.reply_text('Could not write changes to disk')


def cmd_remove(bot, update, **kwargs):
def cmd_remove(update: Update, context: CallbackContext):
log_call(update)
# Check correctness of call
if not authorize(bot, update):
if not authorize(update, context):
return
if 'args' not in kwargs or len(kwargs['args']) < 1:
if len(context.args) < 1:
update.message.reply_text('Please supply a name')
return

# Parse arguments and look for machine to be deleted
machine_name = kwargs['args'][0]
machine_name = context.args
if not any(m.name == machine_name for m in machines):
update.message.reply_text('Could not find ' + machine_name)
return
Expand All @@ -202,10 +202,10 @@ def cmd_remove(bot, update, **kwargs):
update.message.reply_text('Could not write changes to disk')


def cmd_ip(bot, update):
def cmd_ip(update: Update, context: CallbackContext):
log_call(update)
# Check correctness of call
if not authorize(bot, update):
if not authorize(update, context):
return

try:
Expand All @@ -226,8 +226,8 @@ def cmd_ip(bot, update):
# Other Functions
##

def error(bot, update, error):
logger.warning('Update "{u}" caused error "{e}"'.format(u=update, e=error))
def error(update: Update, context: CallbackContext):
logger.warning('Update "{u}" caused error "{e}"'.format(u=update, e=context.error))


def log_call(update):
Expand All @@ -241,7 +241,7 @@ def log_call(update):
.format(c=cmd[0], u=uid))


def send_magic_packet(bot, update, mac_address, display_name):
def send_magic_packet(update: Update, context: CallbackContext, mac_address, display_name):
try:
wol.wake(mac_address)
except ValueError as e:
Expand All @@ -268,7 +268,7 @@ def user_is_allowed(uid):
return str(uid) in config.ALLOWED_USERS


def authorize(bot, update):
def authorize(update: Update, context: CallbackContext):
if not user_is_allowed(update.message.from_user.id):
logger.warning('Unknown User {fn} {ln} [{i}] tried to call bot'.format(
fn=update.message.from_user.first_name,
Expand Down Expand Up @@ -341,18 +341,18 @@ def main():
read_savefile(config.STORAGE_PATH)

# Set up updater
updater = Updater(config.TOKEN)
updater = Updater(config.TOKEN, use_context=True)
disp = updater.dispatcher

# Add handlers
disp.add_handler(CommandHandler('help', cmd_help))
disp.add_handler(CommandHandler('list', cmd_list))
disp.add_handler(CommandHandler('ip', cmd_ip))
disp.add_handler(CommandHandler('wake', cmd_wake, pass_args=True))
disp.add_handler(CommandHandler('wake', cmd_wake))
disp.add_handler(CallbackQueryHandler(cmd_wake_keyboard_handler))
disp.add_handler(CommandHandler('wakemac', cmd_wake_mac, pass_args=True))
disp.add_handler(CommandHandler('add', cmd_add, pass_args=True))
disp.add_handler(CommandHandler('remove', cmd_remove, pass_args=True))
disp.add_handler(CommandHandler('wakemac', cmd_wake_mac))
disp.add_handler(CommandHandler('add', cmd_add))
disp.add_handler(CommandHandler('remove', cmd_remove))

disp.add_error_handler(error)

Expand Down