init
Browse files
bot.py
CHANGED
|
@@ -36,19 +36,20 @@ user_api_keys = {}
|
|
| 36 |
def fetch_ngrok_data(user_id):
|
| 37 |
api_key = user_api_keys.get(user_id, default_api_key)
|
| 38 |
if not api_key:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
headers = {
|
| 42 |
"Authorization": f"Bearer {api_key}",
|
| 43 |
"Ngrok-Version": "2"
|
| 44 |
}
|
| 45 |
-
|
| 46 |
try:
|
| 47 |
response = requests.get("https://api.ngrok.com/tunnels", headers=headers)
|
|
|
|
|
|
|
| 48 |
response.raise_for_status()
|
| 49 |
-
return response.json().get("tunnels", [])
|
| 50 |
except requests.RequestException as e:
|
| 51 |
-
|
| 52 |
|
| 53 |
def get_ngrok_tunnels(user_id):
|
| 54 |
tunnels = fetch_ngrok_data(user_id)
|
|
@@ -57,46 +58,43 @@ def get_ngrok_tunnels(user_id):
|
|
| 57 |
return endpoints
|
| 58 |
|
| 59 |
def get_ngrok_runtime(user_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
if not tcp_tunnel:
|
| 65 |
-
return None, "β No active TCP tunnels found"
|
| 66 |
-
|
| 67 |
-
start_time = datetime.fromisoformat(tcp_tunnel["started_at"].replace("Z", "+00:00")).astimezone(timezone.utc)
|
| 68 |
current_time = datetime.now(timezone.utc)
|
| 69 |
elapsed_time = current_time - start_time
|
| 70 |
-
|
| 71 |
elapsed_hours, remainder = divmod(elapsed_time.total_seconds(), 3600)
|
| 72 |
elapsed_minutes, _ = divmod(remainder, 60)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
return runtime_message, tcp_tunnel
|
| 77 |
-
|
| 78 |
except Exception as e:
|
| 79 |
return None, f"Error retrieving TCP endpoints: {str(e)}"
|
| 80 |
|
| 81 |
-
|
| 82 |
def create_ngrok_status_message(user_id, message_id):
|
| 83 |
"""Helper function to create Ngrok status message and buttons."""
|
| 84 |
runtime_message, tcp_tunnel_or_error = get_ngrok_runtime(user_id)
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
endpoints = get_ngrok_tunnels(user_id)
|
| 92 |
-
if endpoints:
|
| 93 |
-
endpoint_message = "\n".join(endpoints)
|
| 94 |
else:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
full_message = f"{status_message}\n\n<b>Endpoints:</b>\n<code>{endpoint_message}</code>"
|
| 99 |
-
|
| 100 |
buttons = InlineKeyboardMarkup(
|
| 101 |
[
|
| 102 |
[
|
|
@@ -106,7 +104,6 @@ def create_ngrok_status_message(user_id, message_id):
|
|
| 106 |
[InlineKeyboardButton("Set API", callback_data=f"set_api|{user_id}|{message_id}")]
|
| 107 |
]
|
| 108 |
)
|
| 109 |
-
|
| 110 |
return full_message, buttons
|
| 111 |
|
| 112 |
@bot.on_message(filters.command("start"))
|
|
|
|
| 36 |
def fetch_ngrok_data(user_id):
|
| 37 |
api_key = user_api_keys.get(user_id, default_api_key)
|
| 38 |
if not api_key:
|
| 39 |
+
# Return explicit error instead of raising
|
| 40 |
+
return None, "Ngrok API key not found. Please set your Ngrok API key."
|
| 41 |
headers = {
|
| 42 |
"Authorization": f"Bearer {api_key}",
|
| 43 |
"Ngrok-Version": "2"
|
| 44 |
}
|
|
|
|
| 45 |
try:
|
| 46 |
response = requests.get("https://api.ngrok.com/tunnels", headers=headers)
|
| 47 |
+
if response.status_code == 403:
|
| 48 |
+
return None, "Ngrok API key is invalid or does not have permission to access tunnels. Please set a valid API key."
|
| 49 |
response.raise_for_status()
|
| 50 |
+
return response.json().get("tunnels", []), None
|
| 51 |
except requests.RequestException as e:
|
| 52 |
+
return None, f"Error fetching Ngrok data: {str(e)}"
|
| 53 |
|
| 54 |
def get_ngrok_tunnels(user_id):
|
| 55 |
tunnels = fetch_ngrok_data(user_id)
|
|
|
|
| 58 |
return endpoints
|
| 59 |
|
| 60 |
def get_ngrok_runtime(user_id):
|
| 61 |
+
tunnels, error = fetch_ngrok_data(user_id)
|
| 62 |
+
if error:
|
| 63 |
+
return None, error
|
| 64 |
+
tcp_tunnel = next((tunnel for tunnel in tunnels if tunnel["proto"] == "tcp"), None)
|
| 65 |
+
if not tcp_tunnel:
|
| 66 |
+
return None, "β No active TCP tunnels found"
|
| 67 |
try:
|
| 68 |
+
start_time = datetime.fromisoformat(
|
| 69 |
+
tcp_tunnel["started_at"].replace("Z", "+00:00")
|
| 70 |
+
).astimezone(timezone.utc)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
current_time = datetime.now(timezone.utc)
|
| 72 |
elapsed_time = current_time - start_time
|
|
|
|
| 73 |
elapsed_hours, remainder = divmod(elapsed_time.total_seconds(), 3600)
|
| 74 |
elapsed_minutes, _ = divmod(remainder, 60)
|
| 75 |
+
runtime_message = (
|
| 76 |
+
f"π’ Ngrok TCP tunnel running for {int(elapsed_hours)}h {int(elapsed_minutes)}m"
|
| 77 |
+
if elapsed_hours > 0
|
| 78 |
+
else f"π’ Ngrok TCP tunnel running for {int(elapsed_minutes)} minutes"
|
| 79 |
+
)
|
| 80 |
return runtime_message, tcp_tunnel
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
return None, f"Error retrieving TCP endpoints: {str(e)}"
|
| 83 |
|
|
|
|
| 84 |
def create_ngrok_status_message(user_id, message_id):
|
| 85 |
"""Helper function to create Ngrok status message and buttons."""
|
| 86 |
runtime_message, tcp_tunnel_or_error = get_ngrok_runtime(user_id)
|
| 87 |
+
if runtime_message is None:
|
| 88 |
+
# There was a problem (no tunnel or error string)
|
| 89 |
+
status_message = tcp_tunnel_or_error
|
| 90 |
+
endpoints = get_ngrok_tunnels(user_id)
|
| 91 |
+
endpoint_message = "\n".join(endpoints) if endpoints else "No TCP endpoints available."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
else:
|
| 93 |
+
status_message = runtime_message
|
| 94 |
+
endpoints = get_ngrok_tunnels(user_id)
|
| 95 |
+
endpoint_message = "\n".join(endpoints) if endpoints else "No TCP endpoints available."
|
| 96 |
+
# Compose the message
|
| 97 |
full_message = f"{status_message}\n\n<b>Endpoints:</b>\n<code>{endpoint_message}</code>"
|
|
|
|
| 98 |
buttons = InlineKeyboardMarkup(
|
| 99 |
[
|
| 100 |
[
|
|
|
|
| 104 |
[InlineKeyboardButton("Set API", callback_data=f"set_api|{user_id}|{message_id}")]
|
| 105 |
]
|
| 106 |
)
|
|
|
|
| 107 |
return full_message, buttons
|
| 108 |
|
| 109 |
@bot.on_message(filters.command("start"))
|