diff --git a/.github/workflows/service-test.yml b/.github/workflows/service-test.yml index 975c146..b44b2bb 100644 --- a/.github/workflows/service-test.yml +++ b/.github/workflows/service-test.yml @@ -45,6 +45,11 @@ jobs: REPO_URL=${{ github.server_url }}/${{ github.repository }} EOF + - name: Run tests + run: | + python -m pip install -r requirements.txt + python -m pytest test/models.py + - name: Build and Test Service run: | docker compose build diff --git a/Dockerfile b/Dockerfile index eb44a2b..b067c07 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ FROM python:3.11-slim +EXPOSE 8000 + WORKDIR /app COPY requirements.txt . @@ -13,3 +15,4 @@ CMD ["python3", "test/bot.py"] # docker compose build # docker compose up +# docker system prune diff --git a/docker-compose.yml b/docker-compose.yml index 73bffaa..d913bbd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,3 +7,5 @@ services: - FEEDBACK_CHANNEL_ID - MONGO_URL - REPO_URL + ports: + - "8000:8000" diff --git a/requirements.txt b/requirements.txt index 6c6504e..e530eca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,10 @@ curl_cffi pendulum pydantic pymongo +pytest python-dotenv python-telegram-bot[ext] selenium -webdriver-manager +starlette +uvicorn +webdriver_manager diff --git a/src/bot.py b/src/bot.py index 3adecc6..b666789 100644 --- a/src/bot.py +++ b/src/bot.py @@ -5,10 +5,16 @@ import re import sys import traceback +import asyncio from typing import Any, Callable, TypeAlias, cast import pendulum from dotenv import load_dotenv +import uvicorn +from starlette.applications import Starlette +from starlette.requests import Request +from starlette.responses import PlainTextResponse, Response +from starlette.routing import Route from telegram import ( ForceReply, InlineKeyboardMarkup, @@ -37,6 +43,7 @@ UserCache: TypeAlias = dict[str, Any] load_dotenv() + FEEDBACK_CHANNEL_ID = str(os.getenv("FEEDBACK_CHANNEL_ID")) @@ -591,7 +598,7 @@ def create_conversation_handlers() -> list[ConversationHandler]: return handlers -def main(env=Environment.PROD) -> None: +async def main(env=Environment.PROD) -> None: """Initialize and start the bot""" print("Starting bot...") global DB @@ -622,8 +629,28 @@ def main(env=Environment.PROD) -> None: job_queue = application.job_queue job_queue.run_repeating(callback=finder.run, interval=60, data={"db": DB}) - application.run_polling() + async def ping(_: Request) -> Response: + return PlainTextResponse("Pong") + + starlette_app = Starlette( + routes=[ + Route("/ping", ping, methods=["GET"]), + ] + ) + webserver = uvicorn.Server( + config=uvicorn.Config( + app=starlette_app, + host="0.0.0.0", + port=8000, + use_colors=False, + ) + ) + + async with application: + await application.start() + await webserver.serve() + await application.stop() if __name__ == "__main__": - main() + asyncio.run(main()) diff --git a/test/bot.py b/test/bot.py index cfdf4d5..79bcfc4 100644 --- a/test/bot.py +++ b/test/bot.py @@ -1,6 +1,7 @@ import os import sys import logging +import asyncio sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from src import bot @@ -13,4 +14,4 @@ if __name__ == "__main__": - bot.main(Environment.DEV) + asyncio.run(bot.main(Environment.DEV))