testing webhook and telegram library to see if it works

This commit is contained in:
Hosein
2025-12-24 14:40:38 +03:30
parent f1ce1c263d
commit b4c3ad40dc
2 changed files with 38 additions and 10 deletions
+32 -9
View File
@@ -1,5 +1,6 @@
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.responses import HTMLResponse, JSONResponse
import logging
app = FastAPI(
title="Vercel + FastAPI",
@@ -7,17 +8,39 @@ app = FastAPI(
version="1.0.0",
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@app.post("/api/telegram_webhook")
async def telegram_webhook(request: Request):
import telebot
from bot import get_bot
try:
import telebot
from bot import get_bot
bot = get_bot()
json_data = await request.json()
logger.info(f"Received update: {json_data}")
update = telebot.types.Update.de_json(json_data)
# Process the update directly instead of using process_new_updates
if update.message:
try:
# Call your message handlers directly
bot.process_new_updates([update])
except Exception as e:
logger.error(f"Error processing update: {e}")
# Still return 200 to Telegram to acknowledge receipt
return JSONResponse(content={"ok": True}, status_code=200)
return JSONResponse(content={"ok": True}, status_code=200)
bot = get_bot()
json_data = await request.json()
update = telebot.types.Update.de_json(json_data)
bot.process_new_updates([update])
return {"ok": True}
except Exception as e:
logger.error(f"Webhook error: {e}")
# Return 200 even on error to prevent Telegram from retrying
return JSONResponse(content={"ok": True}, status_code=200)
@app.post("/api/test")
@@ -61,4 +84,4 @@ def read_root():
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=5001, reload=True)
uvicorn.run("main:app", host="0.0.0.0", port=5001, reload=True)