sh20raj commited on
Commit
e92fbac
·
1 Parent(s): 135fdce
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +33 -3
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .DS_Store
app.py CHANGED
@@ -1,7 +1,37 @@
1
- from fastapi import FastAPI
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ import httpx
3
 
4
  app = FastAPI()
5
 
6
+ # Your bot token
7
+ BOT_TOKEN = "7337693933:AAGKjpcWREFw5u4U_efy0UkRbq692QxC87k"
8
+ TELEGRAM_API_URL = f"https://api.telegram.org/bot{BOT_TOKEN}"
9
+
10
+ @app.post("/webhook")
11
+ async def telegram_webhook(request: Request):
12
+ data = await request.json()
13
+
14
+ # Extract chat ID and message text
15
+ chat_id = data.get("message", {}).get("chat", {}).get("id")
16
+ message_text = data.get("message", {}).get("text", "").lower()
17
+
18
+ # Respond to "hi"
19
+ if message_text == "hi":
20
+ await send_message(chat_id, "Hi!")
21
+
22
+ return {"status": "ok"}
23
+
24
+ async def send_message(chat_id: int, text: str):
25
+ url = f"{TELEGRAM_API_URL}/sendMessage"
26
+ payload = {"chat_id": chat_id, "text": text}
27
+ async with httpx.AsyncClient() as client:
28
+ await client.post(url, json=payload)
29
+
30
  @app.get("/")
31
+ def root():
32
+ # Default welcome message for visitors
33
+ return {
34
+ "message": "Welcome to the Telegram Bot API!",
35
+ "instructions": "Set up your Telegram bot by setting the webhook to this URL.",
36
+ "status": "Bot is running!"
37
+ }