init
Browse files
bot.py
CHANGED
|
@@ -36,7 +36,7 @@ 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}",
|
|
@@ -45,58 +45,59 @@ def fetch_ngrok_data(user_id):
|
|
| 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
|
| 55 |
-
if error_message:
|
| 56 |
-
return None, error_message # Return error if fetching tunnels fails
|
| 57 |
-
|
| 58 |
endpoints = [tunnel.get("public_url", "").replace("tcp://", "")
|
| 59 |
for tunnel in tunnels if tunnel.get("proto") == "tcp"]
|
| 60 |
-
return endpoints
|
| 61 |
|
| 62 |
def get_ngrok_runtime(user_id):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
"""Helper function to create the inline keyboard buttons for Ngrok."""
|
| 99 |
-
return InlineKeyboardMarkup(
|
| 100 |
[
|
| 101 |
[
|
| 102 |
InlineKeyboardButton("π Refresh", callback_data=f"refresh_ngrok|{user_id}|{message_id}"),
|
|
@@ -105,43 +106,15 @@ def create_ngrok_buttons(user_id, message_id):
|
|
| 105 |
[InlineKeyboardButton("Set API", callback_data=f"set_api|{user_id}|{message_id}")]
|
| 106 |
]
|
| 107 |
)
|
| 108 |
-
|
| 109 |
-
def create_ngrok_status_message(user_id, message_id):
|
| 110 |
-
"""Helper function to create Ngrok status message and buttons."""
|
| 111 |
-
runtime_message, error_message = get_ngrok_runtime(user_id)
|
| 112 |
-
|
| 113 |
-
# Create buttons once, to avoid duplication
|
| 114 |
-
buttons = create_ngrok_buttons(user_id, message_id)
|
| 115 |
-
|
| 116 |
-
if error_message:
|
| 117 |
-
full_message = error_message
|
| 118 |
-
else:
|
| 119 |
-
endpoints, error_message = get_ngrok_tunnels(user_id)
|
| 120 |
-
if error_message:
|
| 121 |
-
full_message = error_message
|
| 122 |
-
else:
|
| 123 |
-
# If there are endpoints, create the message accordingly
|
| 124 |
-
endpoint_message = "\n".join(endpoints) if endpoints else "No TCP endpoints available."
|
| 125 |
-
full_message = f"{runtime_message}\n\n<b>Endpoints:</b>\n<code>{endpoint_message}</code>"
|
| 126 |
-
|
| 127 |
return full_message, buttons
|
| 128 |
|
| 129 |
-
@bot.on_message(filters.command("start"))
|
| 130 |
-
async def start(client: Client, message: Message):
|
| 131 |
-
user_id = message.from_user.id
|
| 132 |
-
logging.debug(f"Received /start command from user {user_id}")
|
| 133 |
-
|
| 134 |
-
text, buttons = create_ngrok_status_message(user_id, message.id)
|
| 135 |
-
await message.reply(text, reply_markup=buttons)
|
| 136 |
-
logging.debug("Reply sent with Ngrok status")
|
| 137 |
-
|
| 138 |
@bot.on_callback_query(filters.regex(r"^set_api\|(\d+)\|(\d+)$"))
|
| 139 |
async def set_api_callback(client: Client, callback_query: CallbackQuery):
|
| 140 |
user_id = int(callback_query.matches[0].group(1))
|
| 141 |
prompt_message = await callback_query.message.reply("Please send your Ngrok API key as a reply to this message.")
|
| 142 |
await callback_query.answer()
|
| 143 |
|
| 144 |
-
# Start the task to delete the prompt if no reply is received
|
| 145 |
asyncio.create_task(delete_if_no_reply(user_id, prompt_message))
|
| 146 |
|
| 147 |
@bot.on_message(filters.reply)
|
|
@@ -149,30 +122,17 @@ async def capture_api_key(client: Client, message: Message):
|
|
| 149 |
if message.reply_to_message and message.reply_to_message.text == "Please send your Ngrok API key as a reply to this message.":
|
| 150 |
user_id = message.from_user.id
|
| 151 |
api_key = message.text.strip()
|
| 152 |
-
|
| 153 |
-
# Optionally validate the API key format here
|
| 154 |
-
if not validate_api_key(api_key):
|
| 155 |
-
await message.reply("Invalid API key format. Please try again.")
|
| 156 |
-
return
|
| 157 |
-
|
| 158 |
user_api_keys[user_id] = api_key
|
| 159 |
await message.reply("Ngrok API key set successfully!")
|
| 160 |
-
|
| 161 |
-
# Optionally delete the prompt message if needed
|
| 162 |
await message.reply_to_message.delete()
|
| 163 |
|
| 164 |
async def delete_if_no_reply(user_id: int, prompt_message: Message):
|
| 165 |
-
await asyncio.sleep(
|
| 166 |
if user_id not in user_api_keys:
|
| 167 |
try:
|
| 168 |
await prompt_message.delete()
|
| 169 |
except Exception as e:
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
def validate_api_key(api_key: str) -> bool:
|
| 173 |
-
"""Basic validation for the Ngrok API key format."""
|
| 174 |
-
# Implement your validation logic; for example:
|
| 175 |
-
return isinstance(api_key, str) and len(api_key) > 0 # Adjust this as needed
|
| 176 |
|
| 177 |
@bot.on_callback_query(filters.regex(r"^refresh_ngrok\|(\d+)\|(\d+)$"))
|
| 178 |
async def refresh_ngrok(client: Client, callback_query: CallbackQuery):
|
|
|
|
| 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 |
+
raise Exception("Ngrok API key not found.")
|
| 40 |
|
| 41 |
headers = {
|
| 42 |
"Authorization": f"Bearer {api_key}",
|
|
|
|
| 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 |
+
raise Exception(f"Error fetching Ngrok data: {str(e)}")
|
| 52 |
|
| 53 |
def get_ngrok_tunnels(user_id):
|
| 54 |
+
tunnels = fetch_ngrok_data(user_id)
|
|
|
|
|
|
|
|
|
|
| 55 |
endpoints = [tunnel.get("public_url", "").replace("tcp://", "")
|
| 56 |
for tunnel in tunnels if tunnel.get("proto") == "tcp"]
|
| 57 |
+
return endpoints
|
| 58 |
|
| 59 |
def get_ngrok_runtime(user_id):
|
| 60 |
+
try:
|
| 61 |
+
tunnels = fetch_ngrok_data(user_id)
|
| 62 |
+
tcp_tunnel = next((tunnel for tunnel in tunnels if tunnel["proto"] == "tcp"), None)
|
| 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 |
+
runtime_message = f"π’ Ngrok TCP tunnel running for {int(elapsed_hours)}h {int(elapsed_minutes)}m" if elapsed_hours > 0 else f"π’ Ngrok TCP tunnel running for {int(elapsed_minutes)} minutes"
|
| 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 |
+
if tcp_tunnel_or_error is None:
|
| 87 |
+
return "β No active TCP tunnels found", None
|
| 88 |
+
|
| 89 |
+
status_message = runtime_message
|
| 90 |
+
|
| 91 |
+
endpoints = get_ngrok_tunnels(user_id)
|
| 92 |
+
if endpoints:
|
| 93 |
+
endpoint_message = "\n".join(endpoints)
|
| 94 |
+
else:
|
| 95 |
+
endpoint_message = "No TCP endpoints available."
|
| 96 |
|
| 97 |
+
# Combine the status message with endpoint information
|
| 98 |
+
full_message = f"{status_message}\n\n<b>Endpoints:</b>\n<code>{endpoint_message}</code>"
|
| 99 |
|
| 100 |
+
buttons = InlineKeyboardMarkup(
|
|
|
|
|
|
|
| 101 |
[
|
| 102 |
[
|
| 103 |
InlineKeyboardButton("π Refresh", callback_data=f"refresh_ngrok|{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_callback_query(filters.regex(r"^set_api\|(\d+)\|(\d+)$"))
|
| 113 |
async def set_api_callback(client: Client, callback_query: CallbackQuery):
|
| 114 |
user_id = int(callback_query.matches[0].group(1))
|
| 115 |
prompt_message = await callback_query.message.reply("Please send your Ngrok API key as a reply to this message.")
|
| 116 |
await callback_query.answer()
|
| 117 |
|
|
|
|
| 118 |
asyncio.create_task(delete_if_no_reply(user_id, prompt_message))
|
| 119 |
|
| 120 |
@bot.on_message(filters.reply)
|
|
|
|
| 122 |
if message.reply_to_message and message.reply_to_message.text == "Please send your Ngrok API key as a reply to this message.":
|
| 123 |
user_id = message.from_user.id
|
| 124 |
api_key = message.text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
user_api_keys[user_id] = api_key
|
| 126 |
await message.reply("Ngrok API key set successfully!")
|
|
|
|
|
|
|
| 127 |
await message.reply_to_message.delete()
|
| 128 |
|
| 129 |
async def delete_if_no_reply(user_id: int, prompt_message: Message):
|
| 130 |
+
await asyncio.sleep(5)
|
| 131 |
if user_id not in user_api_keys:
|
| 132 |
try:
|
| 133 |
await prompt_message.delete()
|
| 134 |
except Exception as e:
|
| 135 |
+
print(f"Error deleting prompt message: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
@bot.on_callback_query(filters.regex(r"^refresh_ngrok\|(\d+)\|(\d+)$"))
|
| 138 |
async def refresh_ngrok(client: Client, callback_query: CallbackQuery):
|