def _register_cancel(self, update, context): """ Handles the cancel of the registration process """ self._register_reset_tmps(utils.get_chat_id(update, context)) context.bot.send_message(chat_id=utils.get_chat_id(update, context), text=f"Ok, process cancelled. Bye!") return ConversationHandler.END
def _register_name(self, update, context): """ Handles the registration process for the name """ tmp_dev_name = update.message.text self._register_tmp_value(utils.get_chat_id(update, context), 'name', tmp_dev_name) self.logger.info(f"Registering device name \"{tmp_dev_name}\"") context.bot.send_message( chat_id=utils.get_chat_id(update, context), text="Now give me the MAC address of the device") return utils.RegisterDeviceStates.MAC
def unknown(self, update, context): """ This will reply to all unknown commands """ context.bot.send_message( chat_id=utils.get_chat_id(update, context), text="Sorry, I didn't understand that command. Use /help to have more info!" )
def sleep(self, update, context): """ This will reply to the `/sleep` command """ reply_markup = self._build_devices_menu(self.SOL_PREFIX) context.bot.send_message( chat_id=utils.get_chat_id(update, context), text="Choose the device to wake up:", reply_markup=reply_markup )
def delete(self, update, context): """ This will reply to the `/delete` command """ reply_markup = self._build_devices_menu(self.DELETE_PREFIX) context.bot.send_message( chat_id=utils.get_chat_id(update, context), text="Choose the device to delete:", reply_markup=reply_markup )
def edit(self, update, context): """ This will reply to the `/edit` command """ reply_markup = self._build_devices_menu(self.EDIT_PREFIX) context.bot.send_message( chat_id=utils.get_chat_id(update, context), text="Choose the device to edit:", reply_markup=reply_markup )
def register(update, context): """ This will reply to the `/register` command """ context.bot.send_message( chat_id=utils.get_chat_id(update, context), text="Welcome to the register wizard.\n" "Send /cancel to stop the registration process.\n\n" "Let's start with the device name") return utils.RegisterDeviceStates.NAME
def wrapped(update, context, *args, **kwargs): user_id = get_user_id(update, context) if user_id not in settings.LIST_OF_ADMINS: get_logger( update, context).warning(f"Unauthorized access denied for {user_id}.") get_bot(update, context).send_message( chat_id=utils.get_chat_id(update, context), text=settings.UNAUTHORIZED_ERROR_MESSAGE) return return func(update, context, *args, **kwargs)
def list(self, update, context): """ This will reply to the `/list` command """ if not self.devices: list_text = "No device found. Run the /register command to add one." else: list_text = "\n".join([f"* {d['name']} ({d['mac']})" for _, d in self.devices.items()]) context.bot.send_message( chat_id=utils.get_chat_id(update, context), text=list_text )
def start(self, update, context): """ This will reply to the `/start` command """ chat_id = utils.get_chat_id(update, context) self.logger.debug(f"Talking to: {chat_id}") print(f"Talking to: {chat_id}") if chat_id not in settings.LIST_OF_ADMINS: msg = settings.UNAUTHORIZED_ERROR_MESSAGE else: msg = f"Hello! I'm {self.name}. With me you can \"Wake on LAN\" your device! Use /help to have more info!" context.bot.send_message( chat_id=chat_id, text=msg )
def help(self, update, context): """ This will reply to the `/help` command """ help_text = """ I can help you register and manage WoL enabled devices. You can control me by sending these commands: /register - registers a new device /list - lists all the registered devices /edit - edits the device /delete - removes the device /wakeup - wakes up the device /sleep - puts the device back to sleep """ context.bot.send_message( chat_id=utils.get_chat_id(update, context), text=help_text )
def _edit_handler(self, update, context): """ This will handle the callback of the edit action """ dev = update.callback_query.data.lstrip(self.EDIT_PREFIX) chat_id = utils.get_chat_id(update, context) if dev == "cancel": context.bot.send_message( chat_id=chat_id, text="Operation cancelled!" ) return dev_id = self._get_device_id(dev) with utils.DBWrapper() as db: db.update_device(dev_id) # TODO: missing name, mac context.bot.send_message( chat_id=chat_id, text=f"Editing {dev}!" )
def _sol_handler(self, update, context): """ This will handle the callback of the sol (/sleep) action """ dev = update.callback_query.data.lstrip(self.SOL_PREFIX) chat_id = utils.get_chat_id(update, context) if dev == "cancel": context.bot.send_message( chat_id=chat_id, text="Operation cancelled!" ) return mac = self._get_device_mac(dev) self.wol_sol.sol(mac) context.bot.send_message( chat_id=chat_id, text=f"SoL sent to {dev}({mac})!" )
def _register_mac(self, update, context): """ Handles the registration process for the mac """ tmp_dev_mac = update.message.text chat_id = utils.get_chat_id(update, context) self._register_tmp_value(chat_id, 'mac', tmp_dev_mac) self.logger.info(f"Registering device mac \"{tmp_dev_mac}\"") status_code, err_msg = self._register_device(chat_id) if status_code == utils.StatusCodes.OK: response_message = f"Cool! the device ({self.register_tmps.get(chat_id, {}).get('name', '')} - {self.register_tmps.get(chat_id, {}).get('mac', '')}) is successfully registered!" self._register_reset_tmps(chat_id) elif status_code == utils.StatusCodes.MAC_ADDRESS_ALREADY_EXISTS: response_message = f"The MAC address for {self.register_tmps.get(chat_id, {}).get('name', '')} already exists! If you want to update that device use the /edit command. Abort!" self._register_reset_tmps(chat_id) else: response_message = f"A generic error occurred: {err_msg}. Abort!" self._register_reset_tmps(chat_id) context.bot.send_message(chat_id=chat_id, text=response_message) return ConversationHandler.END
def _delete_handler(self, update, context): """ This will handle the callback of the delete action """ dev = update.callback_query.data.lstrip(self.DELETE_PREFIX) chat_id = utils.get_chat_id(update, context) if dev == "cancel": context.bot.send_message( chat_id=chat_id, text="Operation cancelled!" ) return dev_id = self._get_device_id(dev) with utils.DBWrapper() as db: db.remove_device(dev_id) self._reload_devices() context.bot.send_message( chat_id=chat_id, text=f"Device {dev} deleted!" )
def command_func(update, context, *args, **kwargs): get_bot(update, context).send_chat_action(chat_id=utils.get_chat_id( update, context), action=action) return func(update, context, *args, **kwargs)