File size: 1,274 Bytes
a6e3f28
 
 
 
20eb075
 
a6e3f28
 
20eb075
a6e3f28
20eb075
 
 
 
 
 
a6e3f28
20eb075
 
 
a6e3f28
20eb075
 
d0532ac
20eb075
 
d0532ac
20eb075
 
 
 
 
 
a6e3f28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import telebot
from fastapi import FastAPI, Request
import uvicorn

TOKEN = "6655373829:AAGduLdLyNx7zUtxH73Sp3Z1vHKS35tV9WU"  # Only needed for bot processing, not for webhook setup
WEBHOOK_URL = "https://astraos-testing.hf.space/webhook"  # Webhook already set up, we just listen

bot = telebot.TeleBot(TOKEN, parse_mode="HTML")
app = FastAPI()

@app.post("/webhook")
async def telegram_webhook(request: Request):
    json_data = await request.json()
    update = telebot.types.Update.de_json(json_data)
    bot.process_new_updates([update])  # Process the received Telegram update
    return {"status": "ok"}

@app.get("/")
def home():
    return {"message": "Bot is running via webhook"}

@bot.message_handler(commands=["start"])
def start_command(message):
    app.reply_to(message, "Hello! I'm running via FastAPI webhook!")

if __name__ == "__main__":
    app.run_webhooks(
        listen="0.0.0.0",  # Listen on all available network interfaces
        port=7860,  # Use the default FastAPI port (adjust as needed)
        url_path="/webhook",  # Must match the endpoint we defined
        webhook_url=WEBHOOK_URL,  # Already set on Telegram
        max_connections=40,  # Default max connections
        drop_pending_updates=True  # Optional: drop old updates
    )