TensorGuy01 commited on
Commit
c0ae816
·
verified ·
1 Parent(s): 74be2a4

Update x.py

Browse files
Files changed (1) hide show
  1. x.py +62 -15
x.py CHANGED
@@ -1,18 +1,65 @@
 
 
 
 
 
 
 
1
  import requests
2
- import gradio as gr
 
 
 
3
 
4
- def check_ip_region():
 
 
 
 
5
  try:
6
- ip_info = requests.get("https://ipinfo.io/json", timeout=5).json()
7
- result = (
8
- f"Public IP: {ip_info.get('ip')}\n"
9
- f"Region: {ip_info.get('region')}\n"
10
- f"Country: {ip_info.get('country')}\n"
11
- f"City: {ip_info.get('city')}"
12
- )
13
- except Exception as e:
14
- result = f"Failed to get IP info: {e}"
15
- return result
16
-
17
- iface = gr.Interface(fn=check_ip_region, inputs=[], outputs="text")
18
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()