From df3d1d64fb23fa478c6ed1a01e0dc8082ba241ad Mon Sep 17 00:00:00 2001 From: Hosein Date: Tue, 23 Dec 2025 14:05:57 +0330 Subject: [PATCH] adding bot.py for bot logic --- bot.py | 17 +++ main.py | 331 ++------------------------------------------------------ 2 files changed, 29 insertions(+), 319 deletions(-) create mode 100644 bot.py 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__":