def main(): init_modules() try: updater.bot.get_me().username except: LOGE("Failed to connect to Telegram, check your internet connection and/or your bot token") exit() LOGI("HomeBot started, version " + str(__version__)) LOGI("Bot username: @" + updater.bot.get_me().username) updater.start_polling()
def unload(self) -> None: LOGI("Unloading module {}".format(self.name)) self.status = "Starting up" for command in self.commands: try: dispatcher.remove_handler(command.handler) except: LOGE("Error disabling module {}, command {}".format(self.name, command)) self.status = "Error" raise else: LOGI("Module {} unloaded".format(self.name)) self.status = "Disabled"
def load(self) -> None: LOGI("Loading module {}".format(self.name)) self.status = "Starting up" for command in self.commands: try: dispatcher.add_handler(command.handler) except: LOGE("Error enabling module {}, command {}".format(self.name, command)) self.status = "Error" raise else: LOGI("Module {} loaded".format(self.name)) self.status = "Running"
def speedtest(update, context): message_id = update.message.reply_text("Running speedtest...").message_id LOGI("Started") speedtest = Speedtest() speedtest.get_best_server() speedtest.download() speedtest.upload() speedtest.results.share() results_dict = speedtest.results.dict() download = str(results_dict["download"] // 10 ** 6) upload = str(results_dict["upload"] // 10 ** 6) context.bot.edit_message_text(chat_id=update.message.chat_id, message_id=message_id, text="Download: {} mbps\n" "Upload: {} mbps".format(download, upload)) LOGI("Finished, download: {} mbps, upload: {} mbps".format(download, upload))
def unload(update, context): if str(update.message.from_user.id) not in get_config( "BOT_ADMIN_USER_IDS").split(): update.message.reply_text( "Error: You are not authorized to unload modules") LOGI("Access denied to user " + str(update.message.from_user.id)) return try: module_name = update.message.text.split(' ', 1)[1] except IndexError: update.message.reply_text("Error: Module name not provided") return if module_name == "core": update.message.reply_text( "Error: You can't unload module used for loading/unloading modules" ) return for module in get_modules_list(): if module_name == module.name: module.unload() update.message.reply_text("Module {} unloaded".format(module_name)) return update.message.reply_text("Error: Module not found")
def init_modules(): global modules for module in [name for _, name, _ in iter_modules([modules_dir])]: module_class = Module(module) module_class.load() modules += [module_class] LOGI("Modules loaded")
def ci(update, context): if str(update.message.from_user.id) not in get_config( "CI_APPROVED_USER_IDS").split(): update.message.reply_text( "Error: You are not authorized to use CI function of this bot.\nAsk to who host this bot to add you to the authorized people list" ) LOGI("Access denied to user " + str(update.message.from_user.id)) return LOGI("Access granted to user " + str(update.message.from_user.id)) if get_config("CI_CHANNEL_ID") == "": update.message.reply_text("Error: CI channel or user ID not defined") LOGE("CI channel or user ID not defined") return project = update.message.text.split()[1] if not os.path.isfile(bot_path / "modules" / "ci" / "projects" / (project + ".py")): update.message.reply_text("Error: Project script not found") return project_module = import_module('homebot.modules.ci.projects.' + project, package="*") LOGI("CI workflow started, project: " + project) project_module.ci_build(update, context) LOGI("CI workflow finished, project: " + project)
def upload(file: Path, destination_path_ci: Path): """ Upload an artifact using settings from config.env Returns True if the upload went fine, else a string containing an explanation of the error """ method = get_config("CI_ARTIFACTS_UPLOAD_METHOD") destination_path_base = Path(get_config("CI_UPLOAD_BASE_DIR")) host = get_config("CI_UPLOAD_HOST") port = get_config("CI_UPLOAD_PORT") username = get_config("CI_UPLOAD_USERNAME") password = get_config("CI_UPLOAD_PASSWORD") ALLOWED_METHODS = ["localcopy", "ftp", "sftp"] file_path = Path(file) file_base = file_path.name if destination_path_base is None: destination_path = destination_path_ci else: destination_path = destination_path_base / destination_path_ci if method not in ALLOWED_METHODS: return "Upload method not valid" if not file_path.is_file(): return "File doesn't exists" LOGI("Started uploading of " + file.name) if method == "localcopy": os.makedirs(destination_path, exist_ok=True) shutil.copy(file_path, destination_path) elif method == "ftp": if port is None or port == "": server = host else: server = host + ":" + port ftp = FTP(server) ftp.login(username, password) ftp_chdir(ftp, destination_path) with open(file_path, 'rb') as f: ftp.storbinary('STOR %s' % file_base, f) f.close() ftp.close() elif method == "sftp": if port is None or port == "": server = host else: server = host + ":" + port transport = paramiko.Transport(server) transport.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(transport) sftp_chdir(sftp, destination_path) sftp.put(file_path, file_base) sftp.close() transport.close() LOGI("Finished uploading of " + file.name) return True
def unload_module(module): LOGI("Unloading" + module + "is WIP")
def load_module(module): import_module('homebot.modules.' + module, package="*") LOGI("Loading module " + module + " finished")
def init_modules(): global modules_list modules_list = [name for _, name, _ in iter_modules([modules_dir])] for module in modules_list: load_module(module) LOGI("Modules loaded")
def __init__(self, name: str) -> None: self.name = name self.module = import_module('homebot.modules.' + self.name, package="*") self.commands = [Command(command[0], command[1]) for command in self.module.commands] LOGI("Commands in module {}: {}".format(self.name, ", ".join([command.name for command in self.commands]))) self.status = "Disabled"