Spaces:
Runtime error
Runtime error
Dmitry Beresnev
commited on
Commit
·
be62f09
1
Parent(s):
b9988d2
fix dockerfile and bot
Browse files- Dockerfile +4 -1
- main.py +5 -0
- src/telegram_bot.py +8 -8
Dockerfile
CHANGED
|
@@ -37,6 +37,9 @@ RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
|
| 37 |
# Copy source code with proper ownership
|
| 38 |
COPY --chown=user src/ $HOME/app/src/
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
# Copy any additional files you need
|
| 41 |
COPY --chown=user *.py $HOME/app/
|
| 42 |
COPY --chown=user .env* $HOME/app/
|
|
@@ -55,6 +58,6 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
|
| 55 |
|
| 56 |
# Run the application when the container starts HF
|
| 57 |
#CMD ["uvicorn", "src.telegram_bot:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 58 |
-
CMD ["python", "
|
| 59 |
# Uncomment the line below to run the application locally
|
| 60 |
#CMD ["python", "-m", "telegram_bot"]
|
|
|
|
| 37 |
# Copy source code with proper ownership
|
| 38 |
COPY --chown=user src/ $HOME/app/src/
|
| 39 |
|
| 40 |
+
# Copy the main application file
|
| 41 |
+
COPY --chown=user main.py $HOME/app/
|
| 42 |
+
|
| 43 |
# Copy any additional files you need
|
| 44 |
COPY --chown=user *.py $HOME/app/
|
| 45 |
COPY --chown=user .env* $HOME/app/
|
|
|
|
| 58 |
|
| 59 |
# Run the application when the container starts HF
|
| 60 |
#CMD ["uvicorn", "src.telegram_bot:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 61 |
+
CMD ["python", "main.py"]
|
| 62 |
# Uncomment the line below to run the application locally
|
| 63 |
#CMD ["python", "-m", "telegram_bot"]
|
main.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.telegram_bot import main as telegram_bot
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
if __name__ == "__main__":
|
| 5 |
+
telegram_bot()
|
src/telegram_bot.py
CHANGED
|
@@ -99,13 +99,13 @@ app = FastAPI(title="Financial News Bot", description="Telegram bot for financia
|
|
| 99 |
|
| 100 |
|
| 101 |
@app.on_event("startup")
|
| 102 |
-
async def startup_event():
|
| 103 |
"""Initialize Telegram bot on FastAPI startup"""
|
| 104 |
await init_telegram_app()
|
| 105 |
|
| 106 |
|
| 107 |
@app.on_event("shutdown")
|
| 108 |
-
async def shutdown_event():
|
| 109 |
"""Cleanup on shutdown"""
|
| 110 |
global application
|
| 111 |
if application:
|
|
@@ -113,7 +113,7 @@ async def shutdown_event():
|
|
| 113 |
|
| 114 |
|
| 115 |
@app.post(f"/{BOT_TOKEN}")
|
| 116 |
-
async def webhook(request: Request):
|
| 117 |
"""Handle incoming webhook from Telegram"""
|
| 118 |
try:
|
| 119 |
# Get the update from Telegram
|
|
@@ -130,7 +130,7 @@ async def webhook(request: Request):
|
|
| 130 |
|
| 131 |
|
| 132 |
@app.get("/")
|
| 133 |
-
async def root():
|
| 134 |
"""Health check endpoint"""
|
| 135 |
return {
|
| 136 |
"status": "Financial News Bot is running!",
|
|
@@ -142,7 +142,7 @@ async def root():
|
|
| 142 |
|
| 143 |
|
| 144 |
@app.get("/set_webhook")
|
| 145 |
-
async def set_webhook():
|
| 146 |
"""Manually set the webhook (call this once after deployment)"""
|
| 147 |
try:
|
| 148 |
webhook_url = f"https://{SPACE_ID}.hf.space/{BOT_TOKEN}"
|
|
@@ -170,7 +170,7 @@ async def set_webhook():
|
|
| 170 |
|
| 171 |
|
| 172 |
@app.get("/webhook_info")
|
| 173 |
-
async def webhook_info():
|
| 174 |
"""Get current webhook information"""
|
| 175 |
try:
|
| 176 |
info_url = f"https://api.telegram.org/bot{BOT_TOKEN}/getWebhookInfo"
|
|
@@ -186,12 +186,12 @@ async def webhook_info():
|
|
| 186 |
|
| 187 |
|
| 188 |
@app.get("/health")
|
| 189 |
-
async def health():
|
| 190 |
"""Additional health check"""
|
| 191 |
return {"status": "healthy", "bot_token_set": bool(BOT_TOKEN)}
|
| 192 |
|
| 193 |
|
| 194 |
-
|
| 195 |
logger.info(f"Starting Financial News Bot on port {PORT}")
|
| 196 |
logger.info(f"Bot token: {BOT_TOKEN[:10]}..." if BOT_TOKEN else "No token set")
|
| 197 |
logger.info(f"Space ID: {SPACE_ID}")
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
@app.on_event("startup")
|
| 102 |
+
async def startup_event() -> None:
|
| 103 |
"""Initialize Telegram bot on FastAPI startup"""
|
| 104 |
await init_telegram_app()
|
| 105 |
|
| 106 |
|
| 107 |
@app.on_event("shutdown")
|
| 108 |
+
async def shutdown_event() -> None:
|
| 109 |
"""Cleanup on shutdown"""
|
| 110 |
global application
|
| 111 |
if application:
|
|
|
|
| 113 |
|
| 114 |
|
| 115 |
@app.post(f"/{BOT_TOKEN}")
|
| 116 |
+
async def webhook(request: Request) -> dict[str, Any]:
|
| 117 |
"""Handle incoming webhook from Telegram"""
|
| 118 |
try:
|
| 119 |
# Get the update from Telegram
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
@app.get("/")
|
| 133 |
+
async def root() -> dict[str, Any]:
|
| 134 |
"""Health check endpoint"""
|
| 135 |
return {
|
| 136 |
"status": "Financial News Bot is running!",
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
@app.get("/set_webhook")
|
| 145 |
+
async def set_webhook() -> dict[str, Any]:
|
| 146 |
"""Manually set the webhook (call this once after deployment)"""
|
| 147 |
try:
|
| 148 |
webhook_url = f"https://{SPACE_ID}.hf.space/{BOT_TOKEN}"
|
|
|
|
| 170 |
|
| 171 |
|
| 172 |
@app.get("/webhook_info")
|
| 173 |
+
async def webhook_info() -> dict[str, Any]:
|
| 174 |
"""Get current webhook information"""
|
| 175 |
try:
|
| 176 |
info_url = f"https://api.telegram.org/bot{BOT_TOKEN}/getWebhookInfo"
|
|
|
|
| 186 |
|
| 187 |
|
| 188 |
@app.get("/health")
|
| 189 |
+
async def health() -> dict[str, Any]:
|
| 190 |
"""Additional health check"""
|
| 191 |
return {"status": "healthy", "bot_token_set": bool(BOT_TOKEN)}
|
| 192 |
|
| 193 |
|
| 194 |
+
def main() -> None:
|
| 195 |
logger.info(f"Starting Financial News Bot on port {PORT}")
|
| 196 |
logger.info(f"Bot token: {BOT_TOKEN[:10]}..." if BOT_TOKEN else "No token set")
|
| 197 |
logger.info(f"Space ID: {SPACE_ID}")
|