added start.py
This commit is contained in:
13
config.py
13
config.py
@@ -7,9 +7,11 @@ class Config:
|
|||||||
"""
|
"""
|
||||||
Notifier config dataclass.
|
Notifier config dataclass.
|
||||||
|
|
||||||
|
* `base_url`: url of domain
|
||||||
* `api_token`: telegram api token.
|
* `api_token`: telegram api token.
|
||||||
* `param bot_owner`: telegram id of bot owner.
|
* `param bot_owner`: telegram id of bot owner.
|
||||||
"""
|
"""
|
||||||
|
base_url: str
|
||||||
api_token: str
|
api_token: str
|
||||||
bot_owner: int
|
bot_owner: int
|
||||||
|
|
||||||
@@ -19,13 +21,15 @@ class ConfigError(Exception):
|
|||||||
|
|
||||||
|
|
||||||
_api_token = getenv('LTLNOTIFIER_API_TOKEN')
|
_api_token = getenv('LTLNOTIFIER_API_TOKEN')
|
||||||
|
print(_api_token[9])
|
||||||
# Check api token
|
# Check api token
|
||||||
if not _api_token:
|
if not _api_token:
|
||||||
raise ConfigError('virtual environment LTLNOTIFIER_API_TOKEN not set.')
|
raise ConfigError('virtual environment LTLNOTIFIER_API_TOKEN not set.')
|
||||||
if len(_api_token) != 45 or _api_token[9] or not _api_token[:9].isdigit():
|
if len(_api_token) != 45 or _api_token[9] != ':' or not _api_token[:9].isdigit():
|
||||||
raise ConfigError('virtual environment LTLNOTIFIER_API_TOKEN incorrect.')
|
raise ConfigError('virtual environment LTLNOTIFIER_API_TOKEN incorrect.')
|
||||||
|
|
||||||
_bot_owner = getenv('LTLNOTIFIER_BOT_OWNER')
|
_bot_owner = getenv('LTLNOTIFIER_BOT_OWNER')
|
||||||
|
print(_bot_owner)
|
||||||
# Check bot owner
|
# Check bot owner
|
||||||
if not _bot_owner:
|
if not _bot_owner:
|
||||||
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER not set.')
|
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER not set.')
|
||||||
@@ -36,6 +40,11 @@ try:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER incorrect.')
|
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER incorrect.')
|
||||||
|
|
||||||
config = Config(api_token=_api_token, bot_owner=_bot_owner)
|
_base_url = getenv('LTLNOTIFIER_BASE_URL')
|
||||||
|
# Check base url
|
||||||
|
if not _base_url:
|
||||||
|
raise ConfigError('virtual environment LTLNOTIFIER_BASE_URL not set')
|
||||||
|
|
||||||
|
config = Config(api_token=_api_token, bot_owner=_bot_owner, base_url=_base_url)
|
||||||
|
|
||||||
__all__ = ['Config', 'config']
|
__all__ = ['Config', 'config']
|
||||||
|
|||||||
45
start.py
Normal file
45
start.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from aiogram import Bot, Dispatcher
|
||||||
|
from aiogram.dispatcher.webhook import configure_app
|
||||||
|
from aiogram.contrib.middlewares.logging import LoggingMiddleware
|
||||||
|
from aiohttp import web
|
||||||
|
|
||||||
|
from config import config
|
||||||
|
|
||||||
|
|
||||||
|
WEBAPP_PORT = 3001
|
||||||
|
TELEGRAM_WEBHOOK_URL = f'{config.base_url}{config.api_token}'
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
bot = Bot(token=config.TELEGRAM_TOKEN)
|
||||||
|
dp = Dispatcher(bot)
|
||||||
|
dp.middleware.setup(LoggingMiddleware())
|
||||||
|
|
||||||
|
|
||||||
|
async def test_route(_):
|
||||||
|
return web.json_response({'test': 'passed'}, status=200)
|
||||||
|
|
||||||
|
|
||||||
|
async def on_startup(_):
|
||||||
|
await bot.set_webhook(TELEGRAM_WEBHOOK_URL)
|
||||||
|
|
||||||
|
|
||||||
|
async def on_shutdown(_):
|
||||||
|
logging.warning('Shutting down')
|
||||||
|
await bot.delete_webhook()
|
||||||
|
await dp.storage.close()
|
||||||
|
await dp.storage.wait_closed()
|
||||||
|
await bot.session.close()
|
||||||
|
logging.warning('Bye!..')
|
||||||
|
|
||||||
|
|
||||||
|
app = web.Application()
|
||||||
|
app.add_routes([web.get('/test', test_route)])
|
||||||
|
app.on_startup.append(on_startup)
|
||||||
|
app.on_shutdown.append(on_shutdown)
|
||||||
|
configure_app(dp, app, '/'+config.api_token)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
web.run_app(app, port=WEBAPP_PORT)
|
||||||
Reference in New Issue
Block a user