File size: 1,144 Bytes
a6e3f28
 
 
 
dfa56fa
 
 
 
a6e3f28
 
20eb075
a6e3f28
20eb075
 
dfa56fa
20eb075
 
dfa56fa
 
20eb075
a6e3f28
20eb075
 
 
a6e3f28
a9b7913
20eb075
2bf20a1
20eb075
 
dfa56fa
 
 
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
import telebot
from fastapi import FastAPI, Request
import uvicorn

# Your bot token is used for processing updates only.
TOKEN = "6655373829:AAGduLdLyNx7zUtxH73Sp3Z1vHKS35tV9WU"
# This is your externally configured webhook URL.
WEBHOOK_URL = "https://astraos-testing.hf.space/webhook"

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

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

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

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

if __name__ == "__main__":
    # Since the webhook is already set on Telegram, we only need to start the FastAPI server.
    # Using uvicorn.run here means we are simply listening on the provided endpoint.
    uvicorn.run(app, host="0.0.0.0", port=7860)