Merge pull request #316 from SeyedHashtag/original
Added note support for user creation and editing within the Telegram bot
This commit is contained in:
@ -16,6 +16,13 @@ def create_cancel_markup(back_step=None):
|
||||
markup.row(types.KeyboardButton("❌ Cancel"))
|
||||
return markup
|
||||
|
||||
def create_cancel_markup_with_skip(back_step=None, username=None, traffic_limit=None):
|
||||
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
|
||||
if back_step:
|
||||
markup.row(types.KeyboardButton("⬅️ Back"))
|
||||
markup.row(types.KeyboardButton("⏭️ Skip"), types.KeyboardButton("❌ Cancel"))
|
||||
return markup
|
||||
|
||||
@bot.message_handler(func=lambda message: is_admin(message.from_user.id) and message.text == '➕ Add User')
|
||||
def add_user(message):
|
||||
msg = bot.reply_to(message, "Enter username (only letters, numbers, and underscores are allowed):", reply_markup=create_cancel_markup())
|
||||
@ -100,7 +107,37 @@ def process_add_user_step3(message, username, traffic_limit):
|
||||
bot.register_next_step_handler(message, process_add_user_step3, username, traffic_limit)
|
||||
return
|
||||
|
||||
msg = bot.reply_to(message, "Enter note (optional, press Skip to continue):", reply_markup=create_cancel_markup_with_skip(back_step=process_add_user_step3, username=username, traffic_limit=traffic_limit))
|
||||
bot.register_next_step_handler(msg, process_add_user_step4, username, traffic_limit, expiration_days)
|
||||
except ValueError:
|
||||
bot.reply_to(message, "Invalid expiration days. Please enter a number:", reply_markup=create_cancel_markup(back_step=process_add_user_step2))
|
||||
bot.register_next_step_handler(message, process_add_user_step3, username, traffic_limit)
|
||||
|
||||
def process_add_user_step4(message, username, traffic_limit, expiration_days):
|
||||
if message.text == "❌ Cancel":
|
||||
bot.reply_to(message, "Process canceled.", reply_markup=create_main_markup())
|
||||
return
|
||||
if message.text == "⬅️ Back":
|
||||
msg = bot.reply_to(message, "Enter expiration days:", reply_markup=create_cancel_markup(back_step=process_add_user_step2))
|
||||
bot.register_next_step_handler(msg, process_add_user_step3, username, traffic_limit)
|
||||
return
|
||||
|
||||
try:
|
||||
if message.text == "⏭️ Skip":
|
||||
note = None
|
||||
else:
|
||||
note = message.text.strip()
|
||||
if len(note) > 200:
|
||||
bot.reply_to(message, "Note is too long (max 200 characters). Please enter a shorter note or press Skip:", reply_markup=create_cancel_markup_with_skip(back_step=process_add_user_step3, username=username, traffic_limit=traffic_limit))
|
||||
bot.register_next_step_handler(message, process_add_user_step4, username, traffic_limit, expiration_days)
|
||||
return
|
||||
|
||||
# Build command with or without note
|
||||
if note is not None:
|
||||
add_user_command = f"python3 {CLI_PATH} add-user -u \"{username}\" -t {traffic_limit} -e {expiration_days} -n \"{note}\""
|
||||
else:
|
||||
add_user_command = f"python3 {CLI_PATH} add-user -u \"{username}\" -t {traffic_limit} -e {expiration_days}"
|
||||
|
||||
add_user_feedback = run_cli_command(add_user_command).strip()
|
||||
|
||||
bot.send_chat_action(message.chat.id, 'typing')
|
||||
|
||||
@ -71,12 +71,16 @@ def process_show_user(message):
|
||||
|
||||
display_username = escape_markdown(actual_username)
|
||||
|
||||
note = user_details.get('note', '')
|
||||
note_display = f"📝 Note: {escape_markdown(note)}" if note else "📝 Note: None"
|
||||
|
||||
formatted_details = (
|
||||
f"\n🆔 Name: {display_username}\n"
|
||||
f"📊 Traffic Limit: {user_details.get('max_download_bytes', 0) / (1024 ** 3):.2f} GB\n"
|
||||
f"📅 Days: {user_details.get('expiration_days', 'N/A')}\n"
|
||||
f"⏳ Creation: {user_details.get('account_creation_date', 'N/A')}\n"
|
||||
f"💡 Blocked: {user_details.get('blocked', 'N/A')}\n\n"
|
||||
f"💡 Blocked: {user_details.get('blocked', 'N/A')}\n"
|
||||
f"{note_display}\n\n"
|
||||
f"{traffic_message}"
|
||||
)
|
||||
|
||||
@ -113,7 +117,8 @@ def process_show_user(message):
|
||||
markup.add(types.InlineKeyboardButton("📅 Edit Expiration", callback_data=f"edit_expiration:{actual_username}"),
|
||||
types.InlineKeyboardButton("🔑 Renew Password", callback_data=f"renew_password:{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("📝 Edit Note", callback_data=f"edit_note:{actual_username}"))
|
||||
markup.add(types.InlineKeyboardButton("⛔ Block User", callback_data=f"block_user:{actual_username}"))
|
||||
|
||||
caption = formatted_details
|
||||
if uri_v4:
|
||||
@ -129,9 +134,9 @@ def process_show_user(message):
|
||||
parse_mode="Markdown"
|
||||
)
|
||||
|
||||
@bot.callback_query_handler(func=lambda call: call.data.startswith('edit_') or call.data.startswith('renew_') or call.data.startswith('block_') or call.data.startswith('reset_') or call.data.startswith('ipv6_'))
|
||||
@bot.callback_query_handler(func=lambda call: call.data.startswith(('edit_', 'renew_', 'block_', 'reset_', 'ipv6_', 'set_new_note', 'clear_note')))
|
||||
def handle_edit_callback(call):
|
||||
action, username = call.data.split(':')
|
||||
action, username = call.data.split(':', 1)
|
||||
display_username = escape_markdown(username)
|
||||
|
||||
if action == 'edit_username':
|
||||
@ -143,6 +148,33 @@ def handle_edit_callback(call):
|
||||
elif action == 'edit_expiration':
|
||||
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)
|
||||
elif action == 'edit_note':
|
||||
command = f"python3 {CLI_PATH} get-user -u \"{username}\""
|
||||
user_result = run_cli_command(command)
|
||||
current_note = ""
|
||||
try:
|
||||
user_details = json.loads(user_result)
|
||||
current_note = user_details.get('note', '')
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
markup = types.InlineKeyboardMarkup()
|
||||
markup.add(types.InlineKeyboardButton("✏️ Set New Note", callback_data=f"set_new_note:{username}"))
|
||||
markup.add(types.InlineKeyboardButton("🗑️ Clear Note", callback_data=f"clear_note:{username}"))
|
||||
|
||||
message_text = f"Select an action for the note of {display_username}:"
|
||||
if current_note:
|
||||
message_text = f"Current note for {display_username}: `{escape_markdown(current_note)}`\n\nSelect an action:"
|
||||
|
||||
bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id)
|
||||
bot.send_message(call.message.chat.id, message_text, reply_markup=markup, parse_mode="Markdown")
|
||||
elif action == 'set_new_note':
|
||||
msg = bot.edit_message_text(f"Enter new note for {display_username}:", call.message.chat.id, call.message.message_id)
|
||||
bot.register_next_step_handler(msg, process_edit_note, username)
|
||||
elif action == 'clear_note':
|
||||
command = f"python3 {CLI_PATH} edit-user -u \"{username}\" --note \"\""
|
||||
result = run_cli_command(command)
|
||||
bot.edit_message_text(result, chat_id=call.message.chat.id, message_id=call.message.message_id)
|
||||
elif action == 'renew_password':
|
||||
command = f"python3 {CLI_PATH} edit-user -u \"{username}\" -rp"
|
||||
result = run_cli_command(command)
|
||||
@ -182,11 +214,11 @@ def handle_edit_callback(call):
|
||||
|
||||
@bot.callback_query_handler(func=lambda call: call.data.startswith('confirm_block:'))
|
||||
def handle_block_confirmation(call):
|
||||
_, username, block_status = call.data.split(':')
|
||||
_, username, block_status = call.data.split(':', 2)
|
||||
flag = '-b' if block_status == 'true' else '--unblocked'
|
||||
command = f"python3 {CLI_PATH} edit-user -u \"{username}\" {flag}"
|
||||
result = run_cli_command(command)
|
||||
bot.send_message(call.message.chat.id, result)
|
||||
bot.edit_message_text(result, call.message.chat.id, call.message.message_id)
|
||||
|
||||
def process_edit_username(message, username):
|
||||
new_username = message.text.strip()
|
||||
@ -211,3 +243,16 @@ def process_edit_expiration(message, username):
|
||||
bot.reply_to(message, result)
|
||||
except ValueError:
|
||||
bot.reply_to(message, "Invalid expiration days. Please enter a number.")
|
||||
|
||||
def process_edit_note(message, username):
|
||||
note_input = message.text.strip()
|
||||
|
||||
if len(note_input) > 200:
|
||||
bot.reply_to(message, "Note is too long (max 200 characters). Please enter a shorter note:")
|
||||
bot.register_next_step_handler(message, process_edit_note, username)
|
||||
return
|
||||
|
||||
command = f"python3 {CLI_PATH} edit-user -u \"{username}\" --note \"{note_input}\""
|
||||
|
||||
result = run_cli_command(command)
|
||||
bot.reply_to(message, result)
|
||||
|
||||
Reference in New Issue
Block a user