Skip to content

Commit

Permalink
[TelegramBotInterface] Handle TelegramApi auth_code
Browse files Browse the repository at this point in the history
  • Loading branch information
valouvaliavlo committed Sep 22, 2021
1 parent c19ae97 commit 927b9c3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
16 changes: 16 additions & 0 deletions Services/Interfaces/telegram_bot_interface/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import octobot_services.constants as services_constants
import octobot_services.interfaces.bots as interfaces_bots
import tentacles.Services.Services_bases as Services_bases
import octobot_services.interfaces.util as interfaces_util


# Telegram bot interface
Expand Down Expand Up @@ -77,6 +78,7 @@ def get_bot_handlers(self):
telegram.ext.CommandHandler("restart", self.command_restart),
telegram.ext.CommandHandler("help", self.command_help),
telegram.ext.CommandHandler(["pause", "resume"], self.command_pause_resume),
telegram.ext.CommandHandler("telegram_2fa_code", self.set_telegram_api_2fa_code),
telegram.ext.MessageHandler(telegram.ext.Filters.command, self.command_unknown)
]

Expand Down Expand Up @@ -109,6 +111,7 @@ def command_help(update, _):
message += "/set\_risk: `Changes my current risk setting into your command's parameter.`" + interfaces_bots.EOL
message += "/refresh\_portfolio or /rpf : `Forces OctoBot's real trader portfolio refresh using exchange " \
"data. Should normally not be necessary.`" + interfaces_bots.EOL
message += "/telegram\_2fa\_code : `Set the telegramApi 2fa code(substract 1 to it before).`" + interfaces_bots.EOL
message += "/pause or /resume: `Pauses or resumes me.`" + interfaces_bots.EOL
message += "/restart: `Restarts me.`" + interfaces_bots.EOL
message += "/stop: `Stops me.`" + interfaces_bots.EOL
Expand All @@ -118,6 +121,19 @@ def command_help(update, _):
elif TelegramBotInterface._is_authorized_chat(update):
update.message.reply_text(interfaces_bots.UNAUTHORIZED_USER_MESSAGE)

@staticmethod
def set_telegram_api_2fa_code(update, _):
if TelegramBotInterface._is_valid_user(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")
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")

@staticmethod
def get_command_param(command_name, update):
return update.message.text.replace(command_name, "").strip()
Expand Down
28 changes: 20 additions & 8 deletions Services/Services_bases/telegram_api_service/telegram_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@ def __init__(self):
self.telegram_client: telethon.TelegramClient = None
self.user_account = None
self.connected = False
self.auth_code = ""
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):
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

def get_fields_description(self):
return {
services_constants.CONFIG_API: "App api key.",
Expand Down Expand Up @@ -80,6 +92,7 @@ def is_setup_correctly(config):
async def prepare(self):
if not self.telegram_client:
try:
self.connected = False
self.telegram_client = telethon.TelegramClient(f"{common_constants.USER_FOLDER}/telegram-api",
self.config[services_constants.CONFIG_CATEGORY_SERVICES]
[services_constants.CONFIG_TELEGRAM_API]
Expand All @@ -88,14 +101,13 @@ async def prepare(self):
[services_constants.CONFIG_TELEGRAM_API]
[services_constants.CONFIG_API_HASH],
base_logger=self.get_name())

await self.telegram_client.start(
phone=
self.config[services_constants.CONFIG_CATEGORY_SERVICES][services_constants.CONFIG_TELEGRAM_API]
[services_constants.CONFIG_TELEGRAM_PHONE]
)
self.user_account = await self.telegram_client.get_me()
self.connected = True
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])
else:
self.user_account = (await self.telegram_client.get_me())
self.connected = True
except Exception as e:
self.logger.error(f"Failed to connect to Telegram Api : {e}")

Expand Down

0 comments on commit 927b9c3

Please sign in to comment.