Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import telebot
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
+
import uvicorn
|
4 |
+
|
5 |
+
TOKEN = "YOUR_BOT_TOKEN"
|
6 |
+
WEBHOOK_URL = "https://yourdomain.com/webhook" # Replace with your actual webhook URL
|
7 |
+
|
8 |
+
bot = telebot.TeleBot(TOKEN, parse_mode="HTML")
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
@app.post("/webhook")
|
13 |
+
async def webhook(request: Request):
|
14 |
+
json_data = await request.json()
|
15 |
+
update = telebot.types.Update.de_json(json_data)
|
16 |
+
bot.process_new_updates([update])
|
17 |
+
return {"status": "ok"}
|
18 |
+
|
19 |
+
@app.get("/")
|
20 |
+
def home():
|
21 |
+
return {"message": "Bot is running"}
|
22 |
+
|
23 |
+
@app.on_event("startup")
|
24 |
+
async def on_startup():
|
25 |
+
bot.remove_webhook()
|
26 |
+
bot.set_webhook(WEBHOOK_URL)
|
27 |
+
print(f"Webhook set to {WEBHOOK_URL}")
|
28 |
+
|
29 |
+
@app.on_event("shutdown")
|
30 |
+
async def on_shutdown():
|
31 |
+
bot.remove_webhook()
|
32 |
+
print("Webhook removed")
|
33 |
+
|
34 |
+
@bot.message_handler(commands=["start"])
|
35 |
+
def start_command(message):
|
36 |
+
bot.reply_to(message, "Hello! I am a FastAPI Telegram bot!")
|
37 |
+
|
38 |
+
if __name__ == "__main__":
|
39 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|