testing webhook and telegram library to see if it works
This commit is contained in:
@@ -7,11 +7,16 @@ def get_bot():
|
|||||||
if _bot is None:
|
if _bot is None:
|
||||||
import telebot
|
import telebot
|
||||||
BOT_TOKEN = os.environ.get("BOT_TOKEN")
|
BOT_TOKEN = os.environ.get("BOT_TOKEN")
|
||||||
_bot = telebot.TeleBot(BOT_TOKEN)
|
_bot = telebot.TeleBot(BOT_TOKEN, threaded=False) # threaded=False for serverless
|
||||||
|
|
||||||
# Register handlers
|
# Register handlers
|
||||||
@_bot.message_handler(commands=["start"])
|
@_bot.message_handler(commands=["start"])
|
||||||
def start(message):
|
def start(message):
|
||||||
_bot.reply_to(message, "Welcome!")
|
_bot.reply_to(message, "Welcome!")
|
||||||
|
|
||||||
|
# Add a handler for all other messages
|
||||||
|
@_bot.message_handler(func=lambda message: True)
|
||||||
|
def echo_all(message):
|
||||||
|
_bot.reply_to(message, f"You said: {message.text}")
|
||||||
|
|
||||||
return _bot
|
return _bot
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse, JSONResponse
|
||||||
|
import logging
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Vercel + FastAPI",
|
title="Vercel + FastAPI",
|
||||||
@@ -7,17 +8,39 @@ app = FastAPI(
|
|||||||
version="1.0.0",
|
version="1.0.0",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/telegram_webhook")
|
@app.post("/api/telegram_webhook")
|
||||||
async def telegram_webhook(request: Request):
|
async def telegram_webhook(request: Request):
|
||||||
|
try:
|
||||||
import telebot
|
import telebot
|
||||||
from bot import get_bot
|
from bot import get_bot
|
||||||
|
|
||||||
bot = get_bot()
|
bot = get_bot()
|
||||||
json_data = await request.json()
|
json_data = await request.json()
|
||||||
|
|
||||||
|
logger.info(f"Received update: {json_data}")
|
||||||
|
|
||||||
update = telebot.types.Update.de_json(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])
|
bot.process_new_updates([update])
|
||||||
return {"ok": True}
|
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)
|
||||||
|
|
||||||
|
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")
|
@app.post("/api/test")
|
||||||
|
|||||||
Reference in New Issue
Block a user