def _edit_script_menu_show_script(self, update, context): send_message(update, context, text=str(context.chat_data["current_script"]), parse_mode=ParseMode.MARKDOWN, keyboard=self._res.EDIT_SCRIPT_KEYBOARD) return self.EDIT_SCRIPT
def _add_to_whitelist_callback_handler(self, update, context): delete_callback_message(update, context) send_message(update, context, self._res.ADD_TO_WHITELIST_TEXT, parse_mode=ParseMode.MARKDOWN) return self.ADD_USER
def _handle_shell_input(self, update, context, command): """ handles an input to the shell, used to handle both single commands and scripts """ if self._check_permission(update): out = self.shell.execute_command(command) if out is not None: send_message(update, context, out) else: send_message(update, context, self._res.ACCESS_DENIED_TEXT)
def _upload_command_handler(self, update, context): """ lets the user download a file from the machine """ if self._check_permission(update): send_message(update, context, self._res.UPLOAD_TEXT) return self.UPLOAD else: send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _edit_script_menu_new_script(self, update, context): delete_callback_message(update, context) if self._check_permission(update): context.chat_data["current_script"] = Script( "script name", "cd", "script description", True) self._edit_script_menu_show_script(update, context) return self.EDIT_SCRIPT send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _remove_from_whitelist(self, update, context): user_id = int(context.chat_data["argument"]) if user_id in self._whiteList: self._whiteList.remove(user_id) save_config_json(self._bot_token, self._whiteList) send_message(update, context, self._res.REMOVE_FROM_WHITELIST_SUCCESS.format(user_id), parse_mode=ParseMode.MARKDOWN)
def _action_cancelled_handler(self, update, context): delete_callback_message(update, context) if self._check_permission(update): send_message(update, context, self._res.CANCEL_TEXT) if context.chat_data["menu"] is not None: return context.chat_data["menu"](update, context) else: send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _edit_script_text_field_handler(self, update, context): delete_callback_message(update, context) field = update.callback_query.data.replace("change_", "") context.chat_data["argument"] = field send_message(update, context, text="`{}`".format( context.chat_data["current_script"].__dict__[field]), parse_mode=ParseMode.MARKDOWN) return self.EDIT_SCRIPT_TEXTFIELD
def _remove_script(self, update, context): delete_callback_message(update, context) script_name = context.chat_data["argument"] script = Script.get_script_from_name(self._scripts, script_name) self._scripts.remove(script) send_message(update, context, text=self._res.SCRIPT_SUCCESSFULLY_REMOVED_TEXT.format( script_name)) save_scripts_json(self._scripts)
def _add_to_whitelist_handler(self, update, context): if self._check_permission(update): user_id = update.message.text self._send_confirmation( update, context, self._add_to_whitelist, argument=user_id, current_menu=self._options_command_handler, ) else: send_message(update, context, self._res.ACCESS_DENIED_TEXT)
def _download_handler(self, update, context): filename = update.message.text file = self.shell.download_file(filename) if file is not None: send_message(update, context, text=self._res.DOWNLOAD_SUCCESS, file=file) file.close() else: send_message(update, context, text=self._res.DOWNLOAD_FAILED) return ConversationHandler.END
def _options_command_handler(self, update, context): """ shows the user the options keyboard """ if self._check_permission(update): send_message(update, context, self._res.OPTIONS_TEXT, parse_mode=ParseMode.MARKDOWN, keyboard=self._res.OPTIONS_KEYBOARD) return self.OPTIONS else: send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _edit_script_save(self, update, context): delete_callback_message(update, context) if self._check_permission(update): context.chat_data["current_script"].save(self._scripts) save_scripts_json(self._scripts) send_message(update, context, text=self._res.SCRIPT_SUCCESSFULLY_SAVED_TEXT, parse_mode=ParseMode.MARKDOWN) self._edit_script_menu_show_script(update, context) else: send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _remove_from_whitelist_handler(self, update, context): delete_callback_message(update, context) if self._check_permission(update): user_id = update.callback_query.data self._send_confirmation( update, context, self._remove_from_whitelist, argument=user_id, current_menu=self._options_command_handler, ) else: send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _show_scripts_callback_handler(self, update, context): delete_callback_message(update, context) if len(self._scripts) == 0: send_message(update, context, self._res.NO_SCRIPTS_TEXT, parse_mode=ParseMode.MARKDOWN) else: for script in self._scripts: send_message(update, context, str(script), parse_mode=ParseMode.MARKDOWN) self._options_command_handler(update, context)
def _remove_script_callback_handler(self, update, context): delete_callback_message(update, context) if len(self._scripts) == 0: send_message(update, context, text=self._res.NO_SCRIPTS_TEXT, parse_mode=ParseMode.MARKDOWN) return self._options_command_handler(update, context) send_message(update, context, text=self._res.REMOVE_SCRIPT_TEXT, keyboard=get_inline_keyboard_from_script_list( self._scripts)) return self.REMOVE_SCRIPT
def _send_confirmation(self, update, context, function, argument=None, current_menu=None): context.chat_data["function"] = function context.chat_data["argument"] = argument context.chat_data["menu"] = current_menu send_message(update, context, text=self._res.CONFIRM_TEXT, parse_mode=ParseMode.MARKDOWN, keyboard=self._res.CONFIRM_KEYBOARD)
def _remove_from_whitelist_callback_handler(self, update, context): delete_callback_message(update, context) if self._whiteList is None: send_message(update, context, self._res.NO_USERS_IN_WHITELIST_TEXT, parse_mode=ParseMode.MARKDOWN) return self._options_command_handler(update, context) send_message( update, context, self._res.REMOVE_FROM_WHITELIST_TEXT, parse_mode=ParseMode.MARKDOWN, keyboard=get_inline_keyboard_from_string_list(self._whiteList), ) return self.REMOVE_USER
def _upload_handler(self, update, context): try: file_id = update.message.document["file_id"] file_name = update.message.document["file_name"] file = context.bot.getFile(file_id).download_as_bytearray() self.shell.upload_file(file, file_name) send_message(update, context, text=self._res.UPLOAD_SUCCESS, parse_mode=ParseMode.MARKDOWN) except TelegramError: send_message(update, context, text=self._res.UPLOAD_FAILED, parse_mode=ParseMode.MARKDOWN) return ConversationHandler.END
def _remove_script_handler(self, update, context): delete_callback_message(update, context) if self._check_permission(update): send_message(update, context, text=str( Script.get_script_from_name( self._scripts, update.callback_query.data)), parse_mode=ParseMode.MARKDOWN) self._send_confirmation(update, context, self._remove_script, argument=update.callback_query.data, current_menu=self._options_command_handler) else: send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _handle_script(self, update, context): """ handle the execution of a script """ if self._check_permission(update): script = Script.get_script_from_name(self._scripts, update.callback_query.data) if script.verbose: for command in script.body.split("\n"): self._handle_shell_input(update, context, command) else: for command in script.body.split("\n"): self.shell.execute_command(command) send_message(update, context, self._res.SCRIPT_DONE_TEXT.format(script.name)) else: send_message(update, context, self._res.ACCESS_DENIED_TEXT) delete_callback_message(update, context) return ConversationHandler.END
def _scripts_command_handler(self, update, context): """ shows the user all the available scripts """ if self._check_permission(update): if len(self._scripts) == 0: send_message(update, context, self._res.NO_SCRIPTS_TEXT, parse_mode=ParseMode.MARKDOWN) return ConversationHandler.END keyboard = get_inline_keyboard_from_script_list(self._scripts) send_message(update, context, self._res.SCRIPTS_TEXT, parse_mode=ParseMode.MARKDOWN, keyboard=keyboard) return self.SCRIPT_SELECT else: send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _add_to_whitelist(self, update, context): try: user_id = int(context.chat_data["argument"]) if self._whiteList is None: self._whiteList = [update.effective_user.id, user_id] save_config_json(self._bot_token, self._whiteList) elif user_id not in self._whiteList: self._whiteList.append(user_id) send_message(update, context, self._res.ADD_TO_WHITELIST_SUCCESS) save_config_json(self._bot_token, self._whiteList) else: send_message(update, context, self._res.USER_ALREADY_IN_WHITELIST_TEXT) except ValueError: send_message(update, context, text=self._res.USER_ID_VALUE_ERROR, parse_mode=ParseMode.MARKDOWN)
def _edit_script_menu_load_script_handler(self, update, context): delete_callback_message(update, context) if self._check_permission(update): if len(self._scripts) != 0: delete_callback_message(update, context) send_message(update, context, text=self._res.SCRIPTS_SHOW_ALL_TEXT, keyboard=get_inline_keyboard_from_script_list( self._scripts)) return self.LOAD_SCRIPT else: send_message(update, context, text=self._res.NO_SCRIPTS_TEXT, parse_mode=ParseMode.MARKDOWN) return self._options_command_handler(update, context) send_message(update, context, self._res.ACCESS_DENIED_TEXT) return ConversationHandler.END
def _end_conversation_handler(self, update, context): delete_callback_message(update, context) send_message(update, context, self._res.END_TEXT) return ConversationHandler.END
def _help_command_handler(self, update, context): """ sends the user the commands + scripts list """ send_message(update, context, self._res.BASE_COMMANDS_TEXT, parse_mode=ParseMode.MARKDOWN)
def _start_command_handler(self, update, context): """ greets the user """ username = update.effective_user.first_name send_message(update, context, self._res.START_TEXT.format(username))