diff --git a/Services/Interfaces/telegram_bot_interface/telegram_bot.py b/Services/Interfaces/telegram_bot_interface/telegram_bot.py index b937f3896..7396eda75 100644 --- a/Services/Interfaces/telegram_bot_interface/telegram_bot.py +++ b/Services/Interfaces/telegram_bot_interface/telegram_bot.py @@ -127,12 +127,17 @@ def set_telegram_api_2fa_code(update, _): result = False param = TelegramBotInterface.get_command_param("/telegram_2fa_code", update) telegram_api = Services_bases.TelegramApiService.get_instance_if_exists() - if telegram_api is None: - self.logger.error("TelegramApiService is not running, can't set auth_code") + if not param: + message = "Please provide a value to the command" + elif telegram_api is None: + message = "TelegramApiService is not running, can't set auth_code" + TelegramBotInterface.get_logger().error(message) else: - result = interfaces_util.run_in_bot_main_loop(telegram_api.set_telegram_2fa_code(str(int(param) + 1))) - TelegramBotInterface._send_message(update, "Done. Please restart me to apply." - if result else "Error, see my logs for more details") + result, message = \ + interfaces_util.run_in_bot_main_loop(telegram_api.set_telegram_2fa_code(str(int(param) + 1))) + TelegramBotInterface._send_message(update, + message + if result else f"Error ({message}), see my logs for more details.") @staticmethod def get_command_param(command_name, update): diff --git a/Services/Services_bases/telegram_api_service/telegram_api.py b/Services/Services_bases/telegram_api_service/telegram_api.py index 5202cd704..0128e4c48 100644 --- a/Services/Services_bases/telegram_api_service/telegram_api.py +++ b/Services/Services_bases/telegram_api_service/telegram_api.py @@ -43,7 +43,8 @@ def __init__(self): self.tentacle_resources_path = tentacles_manager_api.get_tentacle_resources_path(self.__class__) bot_logging.set_logging_level(self.LOGGERS, logging.WARNING) - async def set_telegram_2fa_code(self, auth_code): + async def set_telegram_2fa_code(self, auth_code) -> (bool, str): + error = "" try: self.auth_code = auth_code await self.telegram_client.sign_in(self.config[services_constants.CONFIG_CATEGORY_SERVICES][services_constants.CONFIG_TELEGRAM_API]\ @@ -51,12 +52,13 @@ async def set_telegram_2fa_code(self, auth_code): self.user_account = (await self.telegram_client.get_me()) self.connected = True except Exception as e: - self.logger.exception(e, True, f"Error when login in TelegramApiService: {e}") - return self.connected + error = f"Error when login in TelegramApiService: {e}" + self.logger.exception(e, True, ) + return self.connected, error def get_fields_description(self): return { - services_constants.CONFIG_API: "App api key.", + services_constants.CONFIG_API: "App api id.", services_constants.CONFIG_API_HASH: "App api hash.", services_constants.CONFIG_TELEGRAM_PHONE: "Your telegram phone number (beginning with '+' country code).", } @@ -103,8 +105,9 @@ async def prepare(self): base_logger=self.get_name()) await self.telegram_client.connect() if not (await self.telegram_client.is_user_authorized()): - await self.telegram_client.send_code_request(self.config[services_constants.CONFIG_CATEGORY_SERVICES][services_constants.CONFIG_TELEGRAM_API]\ - [services_constants.CONFIG_TELEGRAM_PHONE]) + await self.telegram_client.send_code_request( + self.config[services_constants.CONFIG_CATEGORY_SERVICES][services_constants.CONFIG_TELEGRAM_API]\ + [services_constants.CONFIG_TELEGRAM_PHONE]) else: self.user_account = (await self.telegram_client.get_me()) self.connected = True @@ -166,6 +169,9 @@ async def download_media_from_message(self, message, source=""): def get_successful_startup_message(self): try: return f"Successfully connected to {self.user_account.username} account.", True + except AttributeError: + self.logger.error(f"Error when connecting to Telegram API: invalid telegram configuration.") + return "", False except Exception as e: self.logger.error(f"Error when connecting to Telegram API ({e}): invalid telegram configuration.") return "", False