|
from fastapi import FastAPI, Request |
|
import httpx |
|
|
|
app = FastAPI() |
|
|
|
|
|
BOT_TOKEN = "7337693933:AAGKjpcWREFw5u4U_efy0UkRbq692QxC87k" |
|
TELEGRAM_API_URL = f"https://api.telegram.org/bot{BOT_TOKEN}" |
|
|
|
@app.post("/webhook") |
|
async def telegram_webhook(request: Request): |
|
data = await request.json() |
|
|
|
|
|
chat_id = data.get("message", {}).get("chat", {}).get("id") |
|
message_text = data.get("message", {}).get("text", "").lower() |
|
|
|
|
|
if message_text == "hi": |
|
await send_message(chat_id, "Hi!") |
|
|
|
return {"status": "ok"} |
|
|
|
async def send_message(chat_id: int, text: str): |
|
url = f"{TELEGRAM_API_URL}/sendMessage" |
|
payload = {"chat_id": chat_id, "text": text} |
|
async with httpx.AsyncClient() as client: |
|
await client.post(url, json=payload) |
|
|
|
@app.get("/") |
|
def root(): |
|
|
|
return { |
|
"message": "Welcome to the Telegram Bot API!", |
|
"instructions": "Set up your Telegram bot by setting the webhook to this URL.", |
|
"status": "Bot is running!" |
|
} |
|
|