Spaces:
Runtime error
Runtime error
alan5543 commited on
Commit ·
b25a009
1
Parent(s): 0caab1a
try fix
Browse files
main.py
CHANGED
|
@@ -6,7 +6,6 @@ import os
|
|
| 6 |
from fastapi import FastAPI, Request, HTTPException # New imports
|
| 7 |
from telegram import Update
|
| 8 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
| 9 |
-
import json
|
| 10 |
|
| 11 |
from config import TELEGRAM_BOT_TOKEN, logger # Ensure logger is imported
|
| 12 |
from mcp_server_config import mcp_server_configs # Make sure this file exists and contains configs
|
|
@@ -83,49 +82,28 @@ async def shutdown_event():
|
|
| 83 |
await mcp_client.cleanup()
|
| 84 |
logger.info("MCP Client cleaned up.")
|
| 85 |
|
| 86 |
-
@app.post(f"/webhook/
|
| 87 |
async def telegram_webhook(request: Request):
|
| 88 |
"""
|
| 89 |
Endpoint to receive Telegram webhook updates.
|
| 90 |
"""
|
| 91 |
-
# Log incoming request
|
| 92 |
-
logger.info(f"Incoming webhook request from IP: {request.client.host}")
|
| 93 |
-
logger.debug(f"Request headers: {dict(request.headers)}")
|
| 94 |
-
|
| 95 |
if not telegram_application:
|
| 96 |
-
logger.
|
| 97 |
-
raise HTTPException(status_code=503, detail="Service Unavailable: Bot not initialized")
|
| 98 |
|
| 99 |
try:
|
| 100 |
-
# Get
|
| 101 |
update_json = await request.json()
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
# Create Update object
|
| 105 |
update = Update.de_json(update_json, telegram_application.bot)
|
| 106 |
-
logger.info(f"Processing update ID: {update.update_id} of type: {update.effective_message.content_type if update.effective_message else 'None'}")
|
| 107 |
-
|
| 108 |
-
# Log message details if available
|
| 109 |
-
if update.message:
|
| 110 |
-
logger.info(f"Message from {update.message.from_user.id} ({update.message.from_user.username}): "
|
| 111 |
-
f"Chat ID: {update.message.chat.id}, Text: {update.message.text}")
|
| 112 |
-
elif update.callback_query:
|
| 113 |
-
logger.info(f"Callback query from {update.callback_query.from_user.id}: "
|
| 114 |
-
f"Data: {update.callback_query.data}")
|
| 115 |
|
| 116 |
-
# Process the update
|
| 117 |
await telegram_application.process_update(update)
|
| 118 |
-
logger.debug(f"Successfully processed update {update.update_id}")
|
| 119 |
-
|
| 120 |
-
return {"status": "ok"}
|
| 121 |
|
| 122 |
-
|
| 123 |
-
logger.error(f"Invalid JSON received: {e}")
|
| 124 |
-
raise HTTPException(status_code=400, detail="Invalid JSON format")
|
| 125 |
-
|
| 126 |
except Exception as e:
|
| 127 |
-
logger.error(f"Error processing
|
| 128 |
-
|
| 129 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 130 |
|
| 131 |
@app.get("/")
|
|
|
|
| 6 |
from fastapi import FastAPI, Request, HTTPException # New imports
|
| 7 |
from telegram import Update
|
| 8 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
|
|
|
| 9 |
|
| 10 |
from config import TELEGRAM_BOT_TOKEN, logger # Ensure logger is imported
|
| 11 |
from mcp_server_config import mcp_server_configs # Make sure this file exists and contains configs
|
|
|
|
| 82 |
await mcp_client.cleanup()
|
| 83 |
logger.info("MCP Client cleaned up.")
|
| 84 |
|
| 85 |
+
@app.post(f"/webhook/{TELEGRAM_BOT_TOKEN}") # Use the bot token in the path for security
|
| 86 |
async def telegram_webhook(request: Request):
|
| 87 |
"""
|
| 88 |
Endpoint to receive Telegram webhook updates.
|
| 89 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if not telegram_application:
|
| 91 |
+
logger.error("Telegram Application not initialized when webhook received.")
|
| 92 |
+
raise HTTPException(status_code=503, detail="Service Unavailable: Bot not initialized.")
|
| 93 |
|
| 94 |
try:
|
| 95 |
+
# Get the JSON payload from the request
|
| 96 |
update_json = await request.json()
|
| 97 |
+
# Create a Telegram Update object from the JSON
|
|
|
|
|
|
|
| 98 |
update = Update.de_json(update_json, telegram_application.bot)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Process the update using the PTB application
|
| 101 |
await telegram_application.process_update(update)
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
return {"status": "ok"} # Telegram expects a 200 OK response
|
|
|
|
|
|
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
+
logger.error(f"Error processing webhook update: {e}", exc_info=True)
|
| 106 |
+
# Return 500 status code for internal errors
|
| 107 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 108 |
|
| 109 |
@app.get("/")
|