Skip to content

Commit

Permalink
[TelegramBotInterface] improve 2fa error management
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume De Saint Martin authored and valouvaliavlo committed Oct 23, 2021
1 parent c8778ab commit 807f881
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
15 changes: 10 additions & 5 deletions Services/Interfaces/telegram_bot_interface/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 12 additions & 6 deletions Services/Services_bases/telegram_api_service/telegram_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@ 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]\
[services_constants.CONFIG_TELEGRAM_PHONE], 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).",
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 807f881

Please sign in to comment.