TensorGuy01 commited on
Commit
ff71fd7
·
verified ·
1 Parent(s): d868d22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -39
app.py CHANGED
@@ -1,65 +1,42 @@
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()
 
 
 
1
  import os
 
 
 
2
  import threading
3
  import requests
4
  from flask import Flask
5
  from telegram.ext import Application, CommandHandler
6
 
 
 
 
7
  TELEGRAM_BOT_TOKEN = "7799719627:AAFz7gF74OeajbUqqb4ZJcsJPzal8R8NN3o"
8
 
 
9
  async def get_real_region():
10
  try:
11
+ res = requests.get("https://ipapi.co/json/ ", timeout=5)
12
+ data = res.json()
13
+ return f"{data.get('country_name', '')}, {data.get('region', '')}"
 
 
 
14
  except Exception:
15
+ return "Unknown location"
16
 
17
 
18
  async def region_command(update, context):
19
+ await update.message.reply_text(f"📍 This bot is running in: {await get_real_region()}")
 
 
 
 
 
20
 
 
21
 
22
+ def start_telegram_bot():
23
+ app = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
24
+ app.add_handler(CommandHandler("region", region_command))
25
+ app.run_polling()
26
 
27
 
28
  # === Flask Web App (port 7860) ===
29
  app = Flask(__name__)
 
 
30
  @app.route("/")
31
  def home():
32
+ return "Bot is running. Send /region to Telegram bot."
 
 
 
 
33
 
34
 
 
35
  if __name__ == "__main__":
36
+ # Start Telegram bot in background
37
+ bot_thread = threading.Thread(target=start_telegram_bot)
38
+ bot_thread.daemon = True
39
+ bot_thread.start()
40
 
41
+ # Run Flask on main thread
42
+ app.run(host="0.0.0.0", port=7860)