Skip to content

Commit

Permalink
Merge pull request #6 from aaron-ang/port-binding
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-ang authored Jan 9, 2025
2 parents 1ea8cf3 + 9e2383d commit c9e30a4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/service-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM python:3.11-slim

EXPOSE 8000

WORKDIR /app

COPY requirements.txt .
Expand All @@ -13,3 +15,4 @@ CMD ["python3", "test/bot.py"]

# docker compose build
# docker compose up
# docker system prune
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ services:
- FEEDBACK_CHANNEL_ID
- MONGO_URL
- REPO_URL
ports:
- "8000:8000"
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ curl_cffi
pendulum
pydantic
pymongo
pytest
python-dotenv
python-telegram-bot[ext]
selenium
webdriver-manager
starlette
uvicorn
webdriver_manager
33 changes: 30 additions & 3 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -37,6 +43,7 @@
UserCache: TypeAlias = dict[str, Any]

load_dotenv()

FEEDBACK_CHANNEL_ID = str(os.getenv("FEEDBACK_CHANNEL_ID"))


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
3 changes: 2 additions & 1 deletion test/bot.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,4 +14,4 @@


if __name__ == "__main__":
bot.main(Environment.DEV)
asyncio.run(bot.main(Environment.DEV))

0 comments on commit c9e30a4

Please sign in to comment.