Adapt to New list-users API Format

This commit is contained in:
Whispering Wind
2025-09-08 23:39:26 +03:30
committed by GitHub
parent 101b9e0ff4
commit 441cd2caaa
3 changed files with 75 additions and 71 deletions

View File

@ -48,7 +48,7 @@ def process_add_user_step1(message):
try: try:
users_data = json.loads(result) users_data = json.loads(result)
existing_users = {user_key.lower() for user_key in users_data.keys()} existing_users = {user['username'].lower() for user in users_data}
if username.lower() in existing_users: if username.lower() in existing_users:
bot.reply_to(message, f"Username '{escape_markdown(username)}' already exists. Please choose a different username:", reply_markup=create_cancel_markup()) bot.reply_to(message, f"Username '{escape_markdown(username)}' already exists. Please choose a different username:", reply_markup=create_cancel_markup())
bot.register_next_step_handler(message, process_add_user_step1) bot.register_next_step_handler(message, process_add_user_step1)
@ -105,7 +105,7 @@ def process_add_user_step3(message, username, traffic_limit):
bot.send_chat_action(message.chat.id, 'typing') bot.send_chat_action(message.chat.id, 'typing')
uri_info_command = f"python3 {CLI_PATH} show-user-uri -u \"{username}\" -ip 4 -n" uri_info_command = f"python3 {CLI_PATH} show-user-uri -u \"{username}\" -ip 4 -n -s"
uri_info_output = run_cli_command(uri_info_command) uri_info_output = run_cli_command(uri_info_command)
direct_uri = None direct_uri = None

View File

@ -24,25 +24,25 @@ def show_user(message):
bot.register_next_step_handler(msg, process_show_user) bot.register_next_step_handler(msg, process_show_user)
def process_show_user(message): def process_show_user(message):
username = message.text.strip().lower() username_input = message.text.strip().lower()
bot.send_chat_action(message.chat.id, 'typing') bot.send_chat_action(message.chat.id, 'typing')
command = f"python3 {CLI_PATH} list-users" command = f"python3 {CLI_PATH} list-users"
result = run_cli_command(command) result = run_cli_command(command)
try: try:
users = json.loads(result) users_list = json.loads(result)
existing_users = {user.lower(): user for user in users.keys()} existing_users = {user['username'].lower(): user['username'] for user in users_list}
if username not in existing_users: if username_input not in existing_users:
bot.reply_to(message, f"Username '{escape_markdown(message.text.strip())}' does not exist. Please enter a valid username.") bot.reply_to(message, f"Username '{escape_markdown(message.text.strip())}' does not exist. Please enter a valid username.")
return return
actual_username = existing_users[username] actual_username = existing_users[username_input]
except json.JSONDecodeError: except (json.JSONDecodeError, KeyError):
bot.reply_to(message, "Error retrieving user list. Please try again later.") bot.reply_to(message, "Error retrieving or parsing user list. Please try again later.")
return return
command = f"python3 {CLI_PATH} get-user -u {actual_username}" command = f"python3 {CLI_PATH} get-user -u \"{actual_username}\""
user_result = run_cli_command(command) user_result = run_cli_command(command)
try: try:
@ -53,7 +53,7 @@ def process_show_user(message):
status = user_details.get('status', 'Unknown') status = user_details.get('status', 'Unknown')
if upload_bytes is None or download_bytes is None: if upload_bytes is None or download_bytes is None:
traffic_message = "**Traffic Data:**\nUser not active or no traffic data available." traffic_message = "*Traffic Data:*\nUser not active or no traffic data available."
else: else:
upload_gb = upload_bytes / (1024 ** 3) upload_gb = upload_bytes / (1024 ** 3)
download_gb = download_bytes / (1024 ** 3) download_gb = download_bytes / (1024 ** 3)
@ -66,47 +66,44 @@ def process_show_user(message):
f"🌐 Status: {status}" f"🌐 Status: {status}"
) )
except json.JSONDecodeError: except json.JSONDecodeError:
bot.reply_to(message, "Failed to parse JSON data. The command output may be malformed.") bot.reply_to(message, "Failed to parse user details. The command output may be malformed.")
return return
display_username = escape_markdown(actual_username) display_username = escape_markdown(actual_username)
formatted_details = ( formatted_details = (
f"\n🆔 Name: {display_username}\n" f"\n🆔 Name: {display_username}\n"
f"📊 Traffic Limit: {user_details['max_download_bytes'] / (1024 ** 3):.2f} GB\n" f"📊 Traffic Limit: {user_details.get('max_download_bytes', 0) / (1024 ** 3):.2f} GB\n"
f"📅 Days: {user_details['expiration_days']}\n" f"📅 Days: {user_details.get('expiration_days', 'N/A')}\n"
f"⏳ Creation: {user_details['account_creation_date']}\n" f"⏳ Creation: {user_details.get('account_creation_date', 'N/A')}\n"
f"💡 Blocked: {user_details['blocked']}\n\n" f"💡 Blocked: {user_details.get('blocked', 'N/A')}\n\n"
f"{traffic_message}" f"{traffic_message}"
) )
combined_command = f"python3 {CLI_PATH} show-user-uri -u {actual_username} -ip 4 -s -n" combined_command = f"python3 {CLI_PATH} show-user-uri -u \"{actual_username}\" -ip 4 -s -n"
combined_result = run_cli_command(combined_command) combined_result = run_cli_command(combined_command)
if "Error" in combined_result or "Invalid" in combined_result:
bot.reply_to(message, combined_result)
return
result_lines = combined_result.strip().split('\n')
uri_v4 = "" uri_v4 = ""
normal_sub_sublink = "" normal_sub_link = ""
for line in result_lines: lines = combined_result.strip().split('\n')
line = line.strip() for i, line in enumerate(lines):
if line.startswith("hy2://"): if line.strip() == "IPv4:":
uri_v4 = line if i + 1 < len(lines) and lines[i+1].strip().startswith("hy2://"):
elif line.startswith("Normal-SUB Sublink:"): uri_v4 = lines[i+1].strip()
normal_sub_sublink = result_lines[result_lines.index(line) + 1].strip() elif line.strip() == "Normal-SUB Sublink:":
if i + 1 < len(lines) and (lines[i+1].strip().startswith("http://") or lines[i+1].strip().startswith("https://")):
normal_sub_link = lines[i+1].strip()
if not uri_v4: qr_link = normal_sub_link if normal_sub_link else uri_v4
bot.reply_to(message, "No valid URI found.") if not qr_link:
bot.reply_to(message, "No valid URI or Subscription link found for this user.")
return return
qr_v4 = qrcode.make(uri_v4) qr_img = qrcode.make(qr_link)
bio_v4 = io.BytesIO() bio = io.BytesIO()
qr_v4.save(bio_v4, 'PNG') qr_img.save(bio, 'PNG')
bio_v4.seek(0) bio.seek(0)
markup = types.InlineKeyboardMarkup(row_width=3) markup = types.InlineKeyboardMarkup(row_width=3)
markup.add(types.InlineKeyboardButton("Reset User", callback_data=f"reset_user:{actual_username}"), markup.add(types.InlineKeyboardButton("Reset User", callback_data=f"reset_user:{actual_username}"),
@ -118,13 +115,15 @@ def process_show_user(message):
markup.add(types.InlineKeyboardButton("Renew Creation Date", callback_data=f"renew_creation:{actual_username}"), markup.add(types.InlineKeyboardButton("Renew Creation Date", callback_data=f"renew_creation:{actual_username}"),
types.InlineKeyboardButton("Block User", callback_data=f"block_user:{actual_username}")) types.InlineKeyboardButton("Block User", callback_data=f"block_user:{actual_username}"))
caption = f"{formatted_details}\n\n**IPv4 URI:**\n\n`{uri_v4}`" caption = formatted_details
if normal_sub_sublink: if uri_v4:
caption += f"\n\n**Normal SUB:**\n{normal_sub_sublink}" caption += f"\n\n*IPv4 URI:*\n`{uri_v4}`"
if normal_sub_link:
caption += f"\n\n*Normal SUB:*\n`{normal_sub_link}`"
bot.send_photo( bot.send_photo(
message.chat.id, message.chat.id,
bio_v4, bio,
caption=caption, caption=caption,
reply_markup=markup, reply_markup=markup,
parse_mode="Markdown" parse_mode="Markdown"
@ -145,11 +144,11 @@ def handle_edit_callback(call):
msg = bot.send_message(call.message.chat.id, f"Enter new expiration days for {display_username}:") msg = bot.send_message(call.message.chat.id, f"Enter new expiration days for {display_username}:")
bot.register_next_step_handler(msg, process_edit_expiration, username) bot.register_next_step_handler(msg, process_edit_expiration, username)
elif action == 'renew_password': elif action == 'renew_password':
command = f"python3 {CLI_PATH} edit-user -u {username} -rp" command = f"python3 {CLI_PATH} edit-user -u \"{username}\" -rp"
result = run_cli_command(command) result = run_cli_command(command)
bot.send_message(call.message.chat.id, result) bot.send_message(call.message.chat.id, result)
elif action == 'renew_creation': elif action == 'renew_creation':
command = f"python3 {CLI_PATH} edit-user -u {username} -rc" command = f"python3 {CLI_PATH} edit-user -u \"{username}\" -rc"
result = run_cli_command(command) result = run_cli_command(command)
bot.send_message(call.message.chat.id, result) bot.send_message(call.message.chat.id, result)
elif action == 'block_user': elif action == 'block_user':
@ -158,11 +157,11 @@ def handle_edit_callback(call):
types.InlineKeyboardButton("False", callback_data=f"confirm_block:{username}:false")) types.InlineKeyboardButton("False", callback_data=f"confirm_block:{username}:false"))
bot.send_message(call.message.chat.id, f"Set block status for {display_username}:", reply_markup=markup) bot.send_message(call.message.chat.id, f"Set block status for {display_username}:", reply_markup=markup)
elif action == 'reset_user': elif action == 'reset_user':
command = f"python3 {CLI_PATH} reset-user -u {username}" command = f"python3 {CLI_PATH} reset-user -u \"{username}\""
result = run_cli_command(command) result = run_cli_command(command)
bot.send_message(call.message.chat.id, result) bot.send_message(call.message.chat.id, result)
elif action == 'ipv6_uri': elif action == 'ipv6_uri':
command = f"python3 {CLI_PATH} show-user-uri -u {username} -ip 6" command = f"python3 {CLI_PATH} show-user-uri -u \"{username}\" -ip 6"
result = run_cli_command(command) result = run_cli_command(command)
if "Error" in result or "Invalid" in result: if "Error" in result or "Invalid" in result:
bot.send_message(call.message.chat.id, result) bot.send_message(call.message.chat.id, result)
@ -184,20 +183,21 @@ def handle_edit_callback(call):
@bot.callback_query_handler(func=lambda call: call.data.startswith('confirm_block:')) @bot.callback_query_handler(func=lambda call: call.data.startswith('confirm_block:'))
def handle_block_confirmation(call): def handle_block_confirmation(call):
_, username, block_status = call.data.split(':') _, username, block_status = call.data.split(':')
command = f"python3 {CLI_PATH} edit-user -u {username} {'-b' if block_status == 'true' else ''}" flag = '-b' if block_status == 'true' else '--unblocked'
command = f"python3 {CLI_PATH} edit-user -u \"{username}\" {flag}"
result = run_cli_command(command) result = run_cli_command(command)
bot.send_message(call.message.chat.id, result) bot.send_message(call.message.chat.id, result)
def process_edit_username(message, username): def process_edit_username(message, username):
new_username = message.text.strip() new_username = message.text.strip()
command = f"python3 {CLI_PATH} edit-user -u {username} -nu {new_username}" command = f"python3 {CLI_PATH} edit-user -u \"{username}\" -nu \"{new_username}\""
result = run_cli_command(command) result = run_cli_command(command)
bot.reply_to(message, result) bot.reply_to(message, result)
def process_edit_traffic(message, username): def process_edit_traffic(message, username):
try: try:
new_traffic_limit = int(message.text.strip()) new_traffic_limit = int(message.text.strip())
command = f"python3 {CLI_PATH} edit-user -u {username} -nt {new_traffic_limit}" command = f"python3 {CLI_PATH} edit-user -u \"{username}\" -nt {new_traffic_limit}"
result = run_cli_command(command) result = run_cli_command(command)
bot.reply_to(message, result) bot.reply_to(message, result)
except ValueError: except ValueError:
@ -206,7 +206,7 @@ def process_edit_traffic(message, username):
def process_edit_expiration(message, username): def process_edit_expiration(message, username):
try: try:
new_expiration_days = int(message.text.strip()) new_expiration_days = int(message.text.strip())
command = f"python3 {CLI_PATH} edit-user -u {username} -ne {new_expiration_days}" command = f"python3 {CLI_PATH} edit-user -u \"{username}\" -ne {new_expiration_days}"
result = run_cli_command(command) result = run_cli_command(command)
bot.reply_to(message, result) bot.reply_to(message, result)
except ValueError: except ValueError:

View File

@ -15,38 +15,42 @@ def handle_inline_query(query):
results = [] results = []
if query_text == "block": if query_text == "block":
for username, details in users.items(): for user in users:
if details.get('blocked', False): if user.get('blocked', False):
username = user['username']
title = f"{username} (Blocked)" title = f"{username} (Blocked)"
description = f"Traffic Limit: {details['max_download_bytes'] / (1024 ** 3):.2f} GB, Expiration Days: {details['expiration_days']}" description = f"Traffic Limit: {user.get('max_download_bytes', 0) / (1024 ** 3):.2f} GB, Expiration Days: {user.get('expiration_days', 'N/A')}"
message_content = (
f"Name: {username}\n"
f"Traffic limit: {user.get('max_download_bytes', 0) / (1024 ** 3):.2f} GB\n"
f"Days: {user.get('expiration_days', 'N/A')}\n"
f"Account Creation: {user.get('account_creation_date', 'N/A')}\n"
f"Blocked: {user.get('blocked', False)}"
)
results.append(types.InlineQueryResultArticle( results.append(types.InlineQueryResultArticle(
id=username, id=username,
title=title, title=title,
description=description, description=description,
input_message_content=types.InputTextMessageContent( input_message_content=types.InputTextMessageContent(message_text=message_content)
message_text=f"Name: {username}\n"
f"Traffic limit: {details['max_download_bytes'] / (1024 ** 3):.2f} GB\n"
f"Days: {details['expiration_days']}\n"
f"Account Creation: {details['account_creation_date']}\n"
f"Blocked: {details['blocked']}"
)
)) ))
else: else:
for username, details in users.items(): for user in users:
username = user['username']
if query_text in username.lower(): if query_text in username.lower():
title = f"{username}" title = f"{username}"
description = f"Traffic Limit: {details['max_download_bytes'] / (1024 ** 3):.2f} GB, Expiration Days: {details['expiration_days']}" description = f"Traffic Limit: {user.get('max_download_bytes', 0) / (1024 ** 3):.2f} GB, Expiration Days: {user.get('expiration_days', 'N/A')}"
message_content = (
f"Name: {username}\n"
f"Traffic limit: {user.get('max_download_bytes', 0) / (1024 ** 3):.2f} GB\n"
f"Days: {user.get('expiration_days', 'N/A')}\n"
f"Account Creation: {user.get('account_creation_date', 'N/A')}\n"
f"Blocked: {user.get('blocked', False)}"
)
results.append(types.InlineQueryResultArticle( results.append(types.InlineQueryResultArticle(
id=username, id=username,
title=title, title=title,
description=description, description=description,
input_message_content=types.InputTextMessageContent( input_message_content=types.InputTextMessageContent(message_text=message_content)
message_text=f"Name: {username}\n"
f"Traffic limit: {details['max_download_bytes'] / (1024 ** 3):.2f} GB\n"
f"Days: {details['expiration_days']}\n"
f"Account Creation: {details['account_creation_date']}\n"
f"Blocked: {details['blocked']}"
)
)) ))
bot.answer_inline_query(query.id, results, cache_time=0) bot.answer_inline_query(query.id, results, cache_time=0)