|
51 | 51 | # Telegram Bot Configuration
|
52 | 52 | # =====================================
|
53 | 53 |
|
54 |
| -# Get authorized users and owner ID from environment variables |
| 54 | +# Get authorized users from environment variables |
55 | 55 | _AUTHORIZED_USERS = [
|
56 | 56 | i.strip() for i in os.getenv("AUTHORIZED_USERS", "").split(",") if i.strip()
|
57 | 57 | ]
|
58 |
| -CONTAINER_OWNER_ID = os.getenv("CONTAINER_OWNER_ID") |
59 | 58 |
|
60 | 59 | # Define states for conversation handlers
|
61 | 60 | NEW_INSTRUCTIONS, SAVE_INSTRUCTIONS = range(2)
|
62 | 61 | ADD_INSTRUCTIONS, APPEND_INSTRUCTIONS = range(2, 4)
|
63 | 62 |
|
64 |
| -# Global variables for command visibility (default: both hidden) |
65 |
| -show_new_instructions = False |
66 |
| -show_add_instructions = False |
67 |
| - |
68 | 63 | # =====================================
|
69 | 64 | # Custom Filters
|
70 | 65 | # =====================================
|
@@ -98,7 +93,7 @@ def new_chat(context: ContextTypes.DEFAULT_TYPE):
|
98 | 93 | context.chat_data["chat"] = model.start_chat(
|
99 | 94 | history=[
|
100 | 95 | {"role": "user", "parts": [clean_text]},
|
101 |
| - {"role": "model", "parts": ["Yes, i will do as you say!!!"]}, |
| 96 | + {"role": "model", "parts": ["Sure."]}, |
102 | 97 | ]
|
103 | 98 | )
|
104 | 99 |
|
@@ -128,13 +123,10 @@ async def help_command(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
|
128 | 123 |
|
129 | 124 | Chat commands:
|
130 | 125 | /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. |
138 | 130 | """
|
139 | 131 | await update.message.reply_text(help_text)
|
140 | 132 |
|
@@ -188,46 +180,6 @@ async def append_instructions(update: Update, context: ContextTypes.DEFAULT_TYPE
|
188 | 180 | await update.message.reply_text(f"Error adding instructions: {e}")
|
189 | 181 | return ConversationHandler.END
|
190 | 182 |
|
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 |
| - |
231 | 183 |
|
232 | 184 | # =====================================
|
233 | 185 | # Message and Image Handling
|
@@ -360,7 +312,7 @@ async def handle_image(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
|
360 | 312 | # Markdown to HTML Formatting
|
361 | 313 | # =====================================
|
362 | 314 |
|
363 |
| -def escape_html(text: str) -> str: |
| 315 | +def escape_html(text: str) -> str: # This line |
364 | 316 | """Escapes HTML special characters in a string."""
|
365 | 317 | text = text.replace("&", "&")
|
366 | 318 | text = text.replace("<", "<")
|
@@ -454,38 +406,31 @@ def start_bot() -> None:
|
454 | 406 | application.add_handler(CommandHandler("help", help_command, filters=AuthFilter))
|
455 | 407 | application.add_handler(CommandHandler("new", newchat_command, filters=AuthFilter))
|
456 | 408 |
|
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) |
489 | 434 |
|
490 | 435 | # Add message and image handlers
|
491 | 436 | application.add_handler(MessageHandler(MessageFilter, handle_message))
|
|
0 commit comments