Spaces:
Runtime error
Runtime error
Update x.py
Browse files
x.py
CHANGED
|
@@ -1,18 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
try:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
os.system(f"{sys.executable} -m pip install python-telegram-bot==20.0 requests flask")
|
| 6 |
+
|
| 7 |
+
import threading
|
| 8 |
import requests
|
| 9 |
+
from flask import Flask
|
| 10 |
+
from telegram.ext import Application, CommandHandler
|
| 11 |
+
|
| 12 |
+
# Install required packages on the fly (first run only)
|
| 13 |
|
| 14 |
+
# === Config ===
|
| 15 |
+
TELEGRAM_BOT_TOKEN = "7799719627:AAFz7gF74OeajbUqqb4ZJcsJPzal8R8NN3o"
|
| 16 |
+
|
| 17 |
+
# === Telegram Bot Logic ===
|
| 18 |
+
async def get_real_region():
|
| 19 |
try:
|
| 20 |
+
response = requests.get("https://ipapi.co/json/ ", timeout=5)
|
| 21 |
+
data = response.json()
|
| 22 |
+
country = data.get("country_name", "Unknown")
|
| 23 |
+
region = data.get("region", "")
|
| 24 |
+
city = data.get("city", "")
|
| 25 |
+
return f"{country} ({region}, {city})" if region or city else country
|
| 26 |
+
except Exception:
|
| 27 |
+
return "Could not determine region"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
async def region_command(update, context):
|
| 31 |
+
region = await get_real_region()
|
| 32 |
+
await update.message.reply_text(f"This bot is running in: 📍 {region}")
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def run_telegram_bot():
|
| 36 |
+
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
| 37 |
+
|
| 38 |
+
application.add_handler(CommandHandler("region", region_command))
|
| 39 |
+
|
| 40 |
+
print("Telegram bot started...")
|
| 41 |
+
application.run_polling()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# === Flask Web App (port 7860) ===
|
| 45 |
+
app = Flask(__name__)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@app.route("/")
|
| 49 |
+
def home():
|
| 50 |
+
return "This space hosts a Telegram bot. Send /region to the bot on Telegram."
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def run_flask():
|
| 54 |
+
app.run(host="0.0.0.0", port=7860)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# === Main Runner ===
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
# Start Telegram bot in a separate thread
|
| 60 |
+
telegram_thread = threading.Thread(target=run_telegram_bot)
|
| 61 |
+
telegram_thread.daemon = True
|
| 62 |
+
telegram_thread.start()
|
| 63 |
+
|
| 64 |
+
# Run Flask app in main thread
|
| 65 |
+
run_flask()
|