diff --git a/bot.py b/bot.py
new file mode 100644
index 0000000..59e7300
--- /dev/null
+++ b/bot.py
@@ -0,0 +1,17 @@
+import os
+
+_bot = None
+
+def get_bot():
+ global _bot
+ if _bot is None:
+ import telebot
+ BOT_TOKEN = os.environ.get("BOT_TOKEN")
+ _bot = telebot.TeleBot(BOT_TOKEN)
+
+ # Register handlers
+ @_bot.message_handler(commands=["start"])
+ def start(message):
+ _bot.reply_to(message, "Welcome!")
+
+ return _bot
\ No newline at end of file
diff --git a/main.py b/main.py
index fab4e65..67fd7ed 100644
--- a/main.py
+++ b/main.py
@@ -1,16 +1,5 @@
-from fastapi import FastAPI
+from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
-import os
-import telebot
-
-BOT_TOKEN = os.environ["BOT_TOKEN"]
-bot = telebot.TeleBot(BOT_TOKEN)
-
-@bot.message_handler(commands=["start"])
-def start(message):
- bot.reply_to(message, "Welcome!")
-
-
app = FastAPI(
title="Vercel + FastAPI",
@@ -21,15 +10,21 @@ app = FastAPI(
@app.post("/api/telegram_webhook")
async def telegram_webhook(request: Request):
+ import telebot
+ from bot import get_bot
+
+ bot = get_bot()
json_data = await request.json()
update = telebot.types.Update.de_json(json_data)
bot.process_new_updates([update])
return {"ok": True}
+
@app.post("/api/test")
async def test():
return {"message": "Test successful"}
+
@app.get("/api/data")
def get_sample_data():
return {
@@ -57,313 +52,11 @@ def get_item(item_id: int):
@app.get("/", response_class=HTMLResponse)
def read_root():
- return """
-
-
-
-
-
- Vercel + FastAPI
-
-
-
-
-
-
-
-
Vercel + FastAPI
-
-
from fastapi import FastAPI
-
-app = FastAPI()
-
-@app.get("/")
-def read_root():
- return {"Python": "on Vercel"}
-
-
-
-
-
-
Interactive API Docs
-
Explore this API's endpoints with the interactive Swagger UI. Test requests and view response schemas in real-time.
-
Open Swagger UI →
-
-
-
-
Sample Data
-
Access sample JSON data through our REST API. Perfect for testing and development purposes.
-
Get Data →
-
-
-
-
-
-
- """
+ return """
+
+ Vercel + FastAPI
+ Vercel + FastAPI
+ """
if __name__ == "__main__":