This commit is contained in:
Alexey Sokolov
2022-03-02 15:52:27 +03:00
parent f70500a519
commit 8b2bcd29e5
4 changed files with 36 additions and 21 deletions

View File

@@ -15,10 +15,10 @@ class Config(metaclass=Singleton):
* `teletoken`: telegram api token. * `teletoken`: telegram api token.
* `bot_owner`: telegram id of bot owner. * `bot_owner`: telegram id of bot owner.
""" """
base_url: str base_url: str = ''
port: int port: int = 3001
teletoken: str teletoken: str = ''
bot_owner: int bot_owner: int = 0
class ConfigError(Exception): class ConfigError(Exception):
@@ -28,7 +28,8 @@ class ConfigError(Exception):
def _get_teletoken(cmd_arguments: Namespace) -> str: def _get_teletoken(cmd_arguments: Namespace) -> str:
""" """
Get Telegram bot token. Get Telegram bot token from command line arguments or
**LTLNOTIFYER_TELETOKEN** environment variable.
Can raise **ConfigError** if Telegram token not set or incorrect. Can raise **ConfigError** if Telegram token not set or incorrect.
:param cmd_arguments: command line arguments. :param cmd_arguments: command line arguments.
@@ -46,13 +47,14 @@ def _get_teletoken(cmd_arguments: Namespace) -> str:
def _get_bot_owner_id(cmd_arguments: Namespace) -> int: def _get_bot_owner_id(cmd_arguments: Namespace) -> int:
""" """
Get bot owner telegram id. Get bot owner telegram id from command line arguments or
**LTLNOTIFYER_BOT_OWNER** environment variable.
Can raise **ConfigError** if bot owner id not set or incorrect. Can raise **ConfigError** if bot owner id not set or incorrect.
:param cmd_arguments: command line arguments. :param cmd_arguments: command line arguments object.
:return: bot owner Telegram id. :return: bot owner Telegram id.
""" """
bot_owner = getenv('LTLNOTIFYER_BOT_OWNER') bot_owner = cmd_arguments.owner or getenv('LTLNOTIFYER_BOT_OWNER')
if not bot_owner: if not bot_owner:
raise ConfigError('Bot owner telegram id not set.') raise ConfigError('Bot owner telegram id not set.')
try: try:
@@ -66,32 +68,42 @@ def _get_bot_owner_id(cmd_arguments: Namespace) -> int:
def _get_base_url(cmd_arguments: Namespace) -> str: def _get_base_url(cmd_arguments: Namespace) -> str:
""" """
Get server base url. Get server base url from command line arguments or
**LTLNOTIFYER_BASE_URL** environment variable.
Can raise **Config error** if base url not set. Can raise **Config error** if base url not set.
:param cmd_arguments: command line arguments. :param cmd_arguments: command line arguments object.
:return: server base url. :return: server base url.
""" """
base_url = cmd_arguments.base_url or getenv('LTLNOTIFYER_BASE_URL') base_url = cmd_arguments.base_url or getenv('LTLNOTIFYER_BASE_URL')
if not base_url: if not base_url:
raise ConfigError('Server base url not set.') raise ConfigError('Server base url not set.')
return base_url
def _get_port(cmd_arguments: Namespace) -> int: def _get_port(cmd_arguments: Namespace) -> int:
""" """
Get server port. Get server port from command line arguments or
**LTLNOTIFYER_PORT** environment variable.
:param cmd_arguments: command line arguments. :param cmd_arguments: command line arguments object.
:return: :return: port number or 3001 if port not set or incorrect.
""" """
port = cmd_arguments.port or getenv('LTLNOTIFIER_PORT', default='') port = cmd_arguments.port or getenv('LTLNOTIFIER_PORT', default='')
port = int(port) if port.isdigit() else 3001 port = int(port) if port.isdigit() else 3001
return port
def _init_config(arguments) -> None: def _init_config(arguments: Namespace) -> None:
"""
Initialisation Config singleton.
:param arguments: command line arguments object.
"""
teletoken = _get_teletoken(arguments) teletoken = _get_teletoken(arguments)
bot_owner = _get_bot_owner_id(arguments) bot_owner = _get_bot_owner_id(arguments)
base_url, port = _get_base_url(arguments) base_url = _get_base_url(arguments)
port = _get_port(arguments)
Config(teletoken=teletoken, Config(teletoken=teletoken,
port=port, port=port,
bot_owner=bot_owner, bot_owner=bot_owner,
@@ -114,7 +126,7 @@ _parser.add_argument('--base_url', default='',
_parser.add_argument('--port', default='', _parser.add_argument('--port', default='',
help='Server port (default = 3001. \n' help='Server port (default = 3001. \n'
'Can be set in LTLNOTIFYER_PORT environment ' 'Can be set in LTLNOTIFYER_PORT environment '
'variable.') 'variable.)')
_init_config(_parser.parse_args()) _init_config(_parser.parse_args())

View File

@@ -2,11 +2,12 @@ import logging
from aiohttp import web from aiohttp import web
from config import config from config import Config
from telegram import telegram_bot, telegram_dispatcher from telegram import telegram_bot, telegram_dispatcher
import db import db
from web import create_app from web import create_app
config = Config()
app = create_app(telegram_bot, telegram_dispatcher, config) app = create_app(telegram_bot, telegram_dispatcher, config)
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)

View File

@@ -2,9 +2,10 @@ from aiogram import Bot, Dispatcher
from aiogram.dispatcher.webhook import SendMessage from aiogram.dispatcher.webhook import SendMessage
from aiogram.contrib.middlewares.logging import LoggingMiddleware from aiogram.contrib.middlewares.logging import LoggingMiddleware
from config import config from config import Config
config = Config()
telegram_bot = Bot(token=config.teletoken) telegram_bot = Bot(token=config.teletoken)
telegram_dispatcher = Dispatcher(telegram_bot) telegram_dispatcher = Dispatcher(telegram_bot)
telegram_dispatcher.middleware.setup(LoggingMiddleware()) telegram_dispatcher.middleware.setup(LoggingMiddleware())
@@ -15,4 +16,4 @@ async def test_telegram(message):
return SendMessage(message.chat.id, 'passed') return SendMessage(message.chat.id, 'passed')
__all__ = ('telegram_bot', telegram_dispatcher) __all__ = ('telegram_bot', 'telegram_dispatcher')

View File

@@ -1,9 +1,10 @@
from aiohttp import web from aiohttp import web
from config import config from config import Config
from .views import test_route from .views import test_route
config = Config()
TELEGRAM_WEBHOOK_ROUTE = '/' + config.teletoken TELEGRAM_WEBHOOK_ROUTE = '/' + config.teletoken
routes = [ routes = [