Skip to content

Commit e0a0eac

Browse files
authored
Update main.py
move slowly
1 parent f02c82a commit e0a0eac

File tree

1 file changed

+32
-87
lines changed

1 file changed

+32
-87
lines changed

Diff for: main.py

+32-87
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,15 @@
5151
# Telegram Bot Configuration
5252
# =====================================
5353

54-
# Get authorized users and owner ID from environment variables
54+
# Get authorized users from environment variables
5555
_AUTHORIZED_USERS = [
5656
i.strip() for i in os.getenv("AUTHORIZED_USERS", "").split(",") if i.strip()
5757
]
58-
CONTAINER_OWNER_ID = os.getenv("CONTAINER_OWNER_ID")
5958

6059
# Define states for conversation handlers
6160
NEW_INSTRUCTIONS, SAVE_INSTRUCTIONS = range(2)
6261
ADD_INSTRUCTIONS, APPEND_INSTRUCTIONS = range(2, 4)
6362

64-
# Global variables for command visibility (default: both hidden)
65-
show_new_instructions = False
66-
show_add_instructions = False
67-
6863
# =====================================
6964
# Custom Filters
7065
# =====================================
@@ -98,7 +93,7 @@ def new_chat(context: ContextTypes.DEFAULT_TYPE):
9893
context.chat_data["chat"] = model.start_chat(
9994
history=[
10095
{"role": "user", "parts": [clean_text]},
101-
{"role": "model", "parts": ["Yes, i will do as you say!!!"]},
96+
{"role": "model", "parts": ["Sure."]},
10297
]
10398
)
10499

@@ -128,13 +123,10 @@ async def help_command(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
128123
129124
Chat commands:
130125
/new - Start a new chat session (model will forget previously generated messages)
131-
"""
132-
# Add conditional help text for owner commands
133-
if is_owner(update):
134-
help_text += """
135-
Owner commands:
136-
/toggle_new_instructions - Show/hide the /new_instructions command.
137-
/toggle_add_instructions - Show/hide the /add_instructions command.
126+
/new_instructions - Replace the bot's instructions with new ones
127+
/add_instructions - Appends additional instructions
128+
129+
Send a message to the bot to generate a response.
138130
"""
139131
await update.message.reply_text(help_text)
140132

@@ -188,46 +180,6 @@ async def append_instructions(update: Update, context: ContextTypes.DEFAULT_TYPE
188180
await update.message.reply_text(f"Error adding instructions: {e}")
189181
return ConversationHandler.END
190182

191-
# =====================================
192-
# Owner-Specific Command Handlers
193-
# =====================================
194-
195-
def is_owner(update: Update) -> bool:
196-
"""Checks if the user sending the update is the bot owner."""
197-
return str(update.effective_user.id) == CONTAINER_OWNER_ID
198-
199-
200-
async def toggle_new_instructions_command(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
201-
"""Toggles the visibility of the /new_instructions command."""
202-
if not is_owner(update):
203-
await update.message.reply_text("You are not authorized to use this command.")
204-
return
205-
206-
global show_new_instructions
207-
show_new_instructions = not show_new_instructions
208-
message = (
209-
"Command /new_instructions is now visible."
210-
if show_new_instructions
211-
else "Command /new_instructions is now hidden."
212-
)
213-
await update.message.reply_text(message)
214-
215-
216-
async def toggle_add_instructions_command(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
217-
"""Toggles the visibility of the /add_instructions command."""
218-
if not is_owner(update):
219-
await update.message.reply_text("You are not authorized to use this command.")
220-
return
221-
222-
global show_add_instructions
223-
show_add_instructions = not show_add_instructions
224-
message = (
225-
"Command /add_instructions is now visible."
226-
if show_add_instructions
227-
else "Command /add_instructions is now hidden."
228-
)
229-
await update.message.reply_text(message)
230-
231183

232184
# =====================================
233185
# Message and Image Handling
@@ -360,7 +312,7 @@ async def handle_image(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
360312
# Markdown to HTML Formatting
361313
# =====================================
362314

363-
def escape_html(text: str) -> str:
315+
def escape_html(text: str) -> str: # This line
364316
"""Escapes HTML special characters in a string."""
365317
text = text.replace("&", "&")
366318
text = text.replace("<", "<")
@@ -454,38 +406,31 @@ def start_bot() -> None:
454406
application.add_handler(CommandHandler("help", help_command, filters=AuthFilter))
455407
application.add_handler(CommandHandler("new", newchat_command, filters=AuthFilter))
456408

457-
# Add conversation handlers for instruction management (conditionally)
458-
if show_new_instructions:
459-
new_instructions_handler = ConversationHandler(
460-
entry_points=[
461-
CommandHandler(
462-
"new_instructions", new_instructions_command, filters=AuthFilter
463-
)
464-
],
465-
states={
466-
NEW_INSTRUCTIONS: [MessageHandler(filters.TEXT, save_instructions)],
467-
},
468-
fallbacks=[CommandHandler("cancel", start, filters=AuthFilter)],
469-
)
470-
application.add_handler(new_instructions_handler)
471-
472-
if show_add_instructions:
473-
add_instructions_handler = ConversationHandler(
474-
entry_points=[
475-
CommandHandler(
476-
"add_instructions", add_instructions_command, filters=AuthFilter
477-
)
478-
],
479-
states={
480-
ADD_INSTRUCTIONS: [MessageHandler(filters.TEXT, append_instructions)],
481-
},
482-
fallbacks=[CommandHandler("cancel", start, filters=AuthFilter)],
483-
)
484-
application.add_handler(add_instructions_handler)
485-
486-
# Add owner-specific command handlers
487-
application.add_handler(CommandHandler("toggle_new_instructions", toggle_new_instructions_command, filters=AuthFilter))
488-
application.add_handler(CommandHandler("toggle_add_instructions", toggle_add_instructions_command, filters=AuthFilter))
409+
# Add conversation handlers for instruction management
410+
new_instructions_handler = ConversationHandler(
411+
entry_points=[
412+
CommandHandler(
413+
"new_instructions", new_instructions_command, filters=AuthFilter
414+
)
415+
],
416+
states={
417+
NEW_INSTRUCTIONS: [MessageHandler(filters.TEXT, save_instructions)],
418+
},
419+
fallbacks=[CommandHandler("cancel", start, filters=AuthFilter)],
420+
)
421+
add_instructions_handler = ConversationHandler(
422+
entry_points=[
423+
CommandHandler(
424+
"add_instructions", add_instructions_command, filters=AuthFilter
425+
)
426+
],
427+
states={
428+
ADD_INSTRUCTIONS: [MessageHandler(filters.TEXT, append_instructions)],
429+
},
430+
fallbacks=[CommandHandler("cancel", start, filters=AuthFilter)],
431+
)
432+
application.add_handler(new_instructions_handler)
433+
application.add_handler(add_instructions_handler)
489434

490435
# Add message and image handlers
491436
application.add_handler(MessageHandler(MessageFilter, handle_message))

0 commit comments

Comments
 (0)