attendantelectro commited on
Commit
3d431c0
·
verified ·
1 Parent(s): e56417b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from telegram import Update, Bot
3
+ from telegram.ext import Dispatcher, MessageHandler, Filters
4
+ import os
5
+
6
+ # توکن ربات تلگرام
7
+ TOKEN = os.getenv("TELEGRAM_TOKEN")
8
+ bot = Bot(token=TOKEN)
9
+ dispatcher = Dispatcher(bot, None, use_context=True)
10
+
11
+ # تابع برای پردازش پیام‌ها
12
+ def handle_message(update: Update, context):
13
+ update.message.reply_text(f"سلام! تو گفتی: {update.message.text}")
14
+
15
+ # اضافه کردن هندلر
16
+ dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))
17
+
18
+ # FastAPI App
19
+ app = FastAPI()
20
+
21
+ # Webhook Endpoint
22
+ @app.post("/webhook")
23
+ async def webhook(request: Request):
24
+ update = Update.de_json(await request.json(), bot)
25
+ dispatcher.process_update(update)
26
+ return {"status": "ok"}
27
+
28
+ # برای اجرا در Hugging Face Spaces
29
+ if __name__ == "__main__":
30
+ import uvicorn
31
+ uvicorn.run(app, host="0.0.0.0", port=7860)