diff --git a/README.md b/README.md index ca39ae9..d9f9a75 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,7 @@ killall geckodriver killall firefox killall python ``` + +## Help article + +[Пишем асинхронного Телеграм-бота](https://habr.com/ru/company/kts/blog/598575/) diff --git a/app/config/.env.template b/app/config/.env.template index 2b73f4a..e0b8e7f 100644 --- a/app/config/.env.template +++ b/app/config/.env.template @@ -8,4 +8,7 @@ WEBHOOK_PATH= WEBAPP_HOST= # or ip WEBAPP_PORT= +# set to 1 to start with webhook. Else bot will start on polling method +START_WITH_WEBHOOK= + GECKO_DRIVER_VERSION=0.31.0 \ No newline at end of file diff --git a/app/geckodriver b/app/geckodriver new file mode 100755 index 0000000..1bd1945 Binary files /dev/null and b/app/geckodriver differ diff --git a/app/main.py b/app/main.py index 8a699ec..61a8820 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,15 @@ from aiogram import Dispatcher -from aiogram.utils.executor import start_webhook +from aiogram.utils.executor import start_polling, start_webhook from core.bot import bot, dispatcher from core.logger import logger from core.scheduler import asyncio_schedule -from settings import WEBAPP_HOST, WEBAPP_PORT, WEBHOOK_PATH, WEBHOOK_URL +from settings import ( + START_WITH_WEBHOOK, + WEBAPP_HOST, + WEBAPP_PORT, + WEBHOOK_PATH, + WEBHOOK_URL, +) async def on_startup(dp: Dispatcher) -> None: @@ -24,8 +30,18 @@ async def on_shutdown(dp: Dispatcher) -> None: logger.warning('Bye!') -if __name__ == '__main__': +def bot_polling() -> None: + logger.info("Start bot in polling mode") + start_polling( + dispatcher=dispatcher, + skip_updates=True, + on_startup=on_startup, + on_shutdown=on_shutdown, + ) + +def bot_webhook() -> None: + logger.info("Start bot with webhook") start_webhook( dispatcher=dispatcher, webhook_path=WEBHOOK_PATH, @@ -35,3 +51,10 @@ if __name__ == '__main__': host=WEBAPP_HOST, port=WEBAPP_PORT, ) + + +if __name__ == '__main__': + if START_WITH_WEBHOOK: + bot_webhook() + else: + bot_polling() diff --git a/app/settings.py b/app/settings.py index 6aac19c..617bec0 100644 --- a/app/settings.py +++ b/app/settings.py @@ -25,3 +25,5 @@ WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}" # webserver settings WEBAPP_HOST = config('WEBAPP_HOST') # or ip WEBAPP_PORT = config('WEBAPP_PORT', cast=int) + +START_WITH_WEBHOOK = config('START_WITH_WEBHOOK', cast=bool)