understanding commited on
Commit
bb49838
·
verified ·
1 Parent(s): f9ed90b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -3,43 +3,50 @@ import logging
3
  from telegram import Update
4
  from telegram.ext import Application, CommandHandler, ContextTypes
5
 
6
- # Standard logging setup so you can see what's happening
7
  logging.basicConfig(
8
  format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
9
  )
10
  logger = logging.getLogger(__name__)
11
 
12
- # Get the Telegram Bot Token from environment variables (or Hugging Face Secrets)
 
13
  TOKEN = os.getenv("TELEGRAM_TOKEN")
14
 
 
 
 
 
15
 
16
- # This function is called when a user sends the /start command
 
17
  async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
18
- """Sends a simple 'Hello, World!' message."""
19
- logger.info(f"Received /start command from user {update.effective_user.first_name}")
20
- await update.message.reply_text("Hello, World! This bot is using the original api.telegram.org domain.")
 
 
21
 
22
 
 
23
  def main() -> None:
24
- """The main function to set up and run the bot."""
25
  if not TOKEN:
26
- logger.error("FATAL: TELEGRAM_TOKEN not found in environment variables/secrets!")
27
  return
28
 
29
- logger.info("Initializing bot with default settings...")
30
 
31
- # This is the key part: A completely standard Application builder.
32
- # By not specifying a `base_url`, it defaults to `https://api.telegram.org`.
33
- application = Application.builder().token(TOKEN).build()
34
 
35
  # Register the /start command handler
36
  application.add_handler(CommandHandler("start", start_command))
37
 
38
- # Start the bot. It will continuously "poll" Telegram for new messages.
39
  logger.info("Bot is starting, polling for updates...")
40
  application.run_polling()
41
 
42
 
43
  if __name__ == "__main__":
44
  main()
45
-
 
3
  from telegram import Update
4
  from telegram.ext import Application, CommandHandler, ContextTypes
5
 
6
+ # --- Basic Setup ---
7
  logging.basicConfig(
8
  format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
9
  )
10
  logger = logging.getLogger(__name__)
11
 
12
+ # --- Configuration ---
13
+ # Get the Bot Token from Hugging Face Secrets. This is still the secure way.
14
  TOKEN = os.getenv("TELEGRAM_TOKEN")
15
 
16
+ # --- IMPORTANT: Using the pre-generated proxy URL you provided ---
17
+ # This URL is now hardcoded directly into the application.
18
+ # The `/bot` at the end is required for the library to function correctly.
19
+ PREGENERATED_PROXY_URL = "https://tg.lgci.workers.dev/bot"
20
 
21
+
22
+ # --- Bot Command Handler ---
23
  async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
24
+ """Sends a confirmation message that the bot is working."""
25
+ logger.info("Received /start command. Replying now.")
26
+ await update.message.reply_text(
27
+ "Hello! This bot is working using the pre-generated proxy at tg.lgci.workers.dev."
28
+ )
29
 
30
 
31
+ # --- Main Bot Logic ---
32
  def main() -> None:
33
+ """The main function to set up and run the bot via the pre-generated proxy."""
34
  if not TOKEN:
35
+ logger.error("FATAL: TELEGRAM_TOKEN not found in Hugging Face Secrets!")
36
  return
37
 
38
+ logger.info(f"Initializing bot to use pre-generated API base: {PREGENERATED_PROXY_URL}")
39
 
40
+ # Build the application, telling it to use the hardcoded proxy URL as the API endpoint.
41
+ application = Application.builder().token(TOKEN).base_url(PREGENERATED_PROXY_URL).build()
 
42
 
43
  # Register the /start command handler
44
  application.add_handler(CommandHandler("start", start_command))
45
 
46
+ # Start polling for updates through the proxy
47
  logger.info("Bot is starting, polling for updates...")
48
  application.run_polling()
49
 
50
 
51
  if __name__ == "__main__":
52
  main()