Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
import logging
|
4 |
-
from
|
|
|
5 |
|
6 |
# --- Configuration ---
|
7 |
# Set up basic logging
|
@@ -51,23 +52,32 @@ if template_content is None:
|
|
51 |
# You might want to exit(1) here or use a default fallback template
|
52 |
# template_content = "Default template: An error occurred fetching the primary one."
|
53 |
|
54 |
-
# Initialize the
|
55 |
-
#
|
56 |
-
logger.info("Initializing
|
57 |
try:
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
except Exception as e:
|
61 |
-
logger.error(f"Failed to initialize
|
62 |
exit(1)
|
63 |
|
64 |
# --- Bot Event Handlers ---
|
65 |
-
@
|
66 |
-
async def start_handler(
|
67 |
"""Handler for the /start command."""
|
68 |
-
|
69 |
-
|
70 |
-
logger.info(f"Received /start command from {sender_name} (ID: {
|
71 |
|
72 |
# Example of using the fetched template content
|
73 |
start_message = f"Hello {sender_name}!\n\n"
|
@@ -76,40 +86,37 @@ async def start_handler(event):
|
|
76 |
else:
|
77 |
start_message += "Could not load the template content."
|
78 |
|
79 |
-
await
|
80 |
|
81 |
-
@
|
82 |
-
async def help_handler(
|
83 |
"""Handler for the /help command."""
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
86 |
|
87 |
# Add more handlers as needed
|
88 |
-
# @
|
89 |
-
# async def message_handler(
|
90 |
# """Handles any other message."""
|
91 |
-
#
|
|
|
92 |
# # Process the message...
|
93 |
-
#
|
94 |
|
95 |
|
96 |
# --- Main Execution ---
|
97 |
-
|
98 |
-
"""Main function to start the bot."""
|
99 |
if template_content is not None:
|
100 |
logger.info("Template Content Loaded:\n" + template_content[:200] + "..." if len(template_content) > 200 else template_content) # Log first 200 chars
|
101 |
else:
|
102 |
logger.warning("Template content is not available.")
|
103 |
|
104 |
logger.info("Starting bot...")
|
105 |
-
#
|
106 |
-
|
107 |
-
|
108 |
-
# Keep the client running until disconnected
|
109 |
-
await client.run_until_disconnected()
|
110 |
logger.info("Bot stopped.")
|
111 |
|
112 |
-
if __name__ == '__main__':
|
113 |
-
# Run the main function in the asyncio event loop
|
114 |
-
client.loop.run_until_complete(main())
|
115 |
-
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
import logging
|
4 |
+
from hydrogram import Client, filters
|
5 |
+
from hydrogram.types import Message # Import Message type hint
|
6 |
|
7 |
# --- Configuration ---
|
8 |
# Set up basic logging
|
|
|
52 |
# You might want to exit(1) here or use a default fallback template
|
53 |
# template_content = "Default template: An error occurred fetching the primary one."
|
54 |
|
55 |
+
# Initialize the Hydrogram (Pyrogram) client
|
56 |
+
# "my_bot" is the session name. A file named my_bot.session will be created.
|
57 |
+
logger.info("Initializing Hydrogram Client...")
|
58 |
try:
|
59 |
+
# When using bot_token, api_id and api_hash are used to authorize the bot owner's actions (if needed)
|
60 |
+
# or simply required by the library structure.
|
61 |
+
app = Client(
|
62 |
+
name="my_bot",
|
63 |
+
api_id=API_ID,
|
64 |
+
api_hash=API_HASH,
|
65 |
+
bot_token=BOT_TOKEN
|
66 |
+
# You might need 'plugins' argument if structuring with plugins
|
67 |
+
# plugins=dict(root="plugins") # Example if you have a 'plugins' folder
|
68 |
+
)
|
69 |
+
logger.info("Hydrogram Client initialized.")
|
70 |
except Exception as e:
|
71 |
+
logger.error(f"Failed to initialize Hydrogram Client: {e}")
|
72 |
exit(1)
|
73 |
|
74 |
# --- Bot Event Handlers ---
|
75 |
+
@app.on_message(filters.command("start"))
|
76 |
+
async def start_handler(client: Client, message: Message):
|
77 |
"""Handler for the /start command."""
|
78 |
+
sender_name = message.from_user.first_name if message.from_user else "User"
|
79 |
+
sender_id = message.from_user.id if message.from_user else "Unknown ID"
|
80 |
+
logger.info(f"Received /start command from {sender_name} (ID: {sender_id})")
|
81 |
|
82 |
# Example of using the fetched template content
|
83 |
start_message = f"Hello {sender_name}!\n\n"
|
|
|
86 |
else:
|
87 |
start_message += "Could not load the template content."
|
88 |
|
89 |
+
await message.reply_text(start_message) # Use reply_text for text messages
|
90 |
|
91 |
+
@app.on_message(filters.command("help"))
|
92 |
+
async def help_handler(client: Client, message: Message):
|
93 |
"""Handler for the /help command."""
|
94 |
+
sender_id = message.from_user.id if message.from_user else "Unknown ID"
|
95 |
+
logger.info(f"Received /help command from {sender_id}")
|
96 |
+
await message.reply_text(
|
97 |
+
"This is a sample bot (Hydrogram).\nCommands:\n/start - Show welcome message and template\n/help - Show this help message"
|
98 |
+
)
|
99 |
|
100 |
# Add more handlers as needed
|
101 |
+
# @app.on_message(filters.private & ~filters.command(["start", "help"])) # Example: handle other private messages
|
102 |
+
# async def message_handler(client: Client, message: Message):
|
103 |
# """Handles any other message."""
|
104 |
+
# sender_id = message.from_user.id if message.from_user else "Unknown ID"
|
105 |
+
# logger.info(f"Received message from {sender_id}: {message.text}")
|
106 |
# # Process the message...
|
107 |
+
# await message.reply_text(f"You said: {message.text}")
|
108 |
|
109 |
|
110 |
# --- Main Execution ---
|
111 |
+
if __name__ == '__main__':
|
|
|
112 |
if template_content is not None:
|
113 |
logger.info("Template Content Loaded:\n" + template_content[:200] + "..." if len(template_content) > 200 else template_content) # Log first 200 chars
|
114 |
else:
|
115 |
logger.warning("Template content is not available.")
|
116 |
|
117 |
logger.info("Starting bot...")
|
118 |
+
# Run the client
|
119 |
+
app.run()
|
120 |
+
# No code will execute after app.run() until the bot stops
|
|
|
|
|
121 |
logger.info("Bot stopped.")
|
122 |
|
|
|
|
|
|
|
|