Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/bot/commands/credentials.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from uuid import UUID

from pybotx import Bot, BotAccountWithSecret, IncomingMessage
from pydantic import AnyHttpUrl

from app.bot.handler_with_help import HandlerCollectorWithHelp
from app.bot.regular_expressions import ADD_BOT_CREDENIALS_REGEXP
Expand Down Expand Up @@ -39,9 +40,14 @@ async def add_credentials_handler(message: IncomingMessage, bot: Bot) -> None:
await bot.answer_message("**Error:** Invalid bot id")
return

if "://" not in host:
host = f"https://{host}"

# Normally bot accounts doesn't added on the fly
bot._bot_accounts_storage._bot_accounts.append( # noqa: WPS437
BotAccountWithSecret(id=bot_id, host=host, secret_key=secret_key)
BotAccountWithSecret(
id=bot_id, cts_url=AnyHttpUrl(host, scheme="https"), secret_key=secret_key
)
)

await bot.answer_message("Credentials was added")
17 changes: 13 additions & 4 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from uuid import UUID

from pybotx import BotAccountWithSecret
from pydantic import BaseSettings, validator
from pydantic import AnyHttpUrl, BaseSettings, validator


class AppSettings(BaseSettings):
Expand Down Expand Up @@ -40,12 +40,21 @@ def _build_credentials_from_string(
cls, credentials_str: str
) -> BotAccountWithSecret:
credentials_str = credentials_str.replace("|", "@")
assert credentials_str.count("@") == 2, "Have you forgot to add `bot_id`?"
if credentials_str.count("@") != 2:
raise ValueError("Have you forgot to add `bot_id`?")

host, secret_key, bot_id = [
cts_url, secret_key, bot_id = [
str_value.strip() for str_value in credentials_str.split("@")
]
return BotAccountWithSecret(host=host, secret_key=secret_key, id=UUID(bot_id))

if "://" not in cts_url:
cts_url = f"https://{cts_url}"

return BotAccountWithSecret(
id=UUID(bot_id),
cts_url=AnyHttpUrl(cts_url, scheme="https"),
secret_key=secret_key,
)


settings = AppSettings()
Loading
Loading