Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,49 +3,43 @@ import logging
|
|
| 3 |
from telegram import Update
|
| 4 |
from telegram.ext import Application, CommandHandler, ContextTypes
|
| 5 |
|
| 6 |
-
#
|
| 7 |
logging.basicConfig(
|
| 8 |
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
| 9 |
)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 14 |
|
| 15 |
-
# --- Define the alternative Telegram API domain ---
|
| 16 |
-
# This is the most important part of our test!
|
| 17 |
-
ALT_API_URL = "https://api.tele.gs/bot"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
# --- Bot Command Handler ---
|
| 21 |
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 22 |
-
"""Sends a
|
| 23 |
-
logger.info("Received /start command
|
| 24 |
-
await update.message.reply_text(
|
| 25 |
-
"Hello World! If you are seeing this, the trick worked! "
|
| 26 |
-
f"This bot is talking to Telegram via: {ALT_API_URL}"
|
| 27 |
-
)
|
| 28 |
|
| 29 |
|
| 30 |
-
# --- Main Bot Logic ---
|
| 31 |
def main() -> None:
|
| 32 |
-
"""
|
| 33 |
if not TOKEN:
|
| 34 |
-
logger.error("FATAL: TELEGRAM_TOKEN not found in
|
| 35 |
return
|
| 36 |
|
| 37 |
-
logger.info(
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
# Register the /start command handler
|
| 43 |
application.add_handler(CommandHandler("start", start_command))
|
| 44 |
|
| 45 |
-
# Start
|
| 46 |
logger.info("Bot is starting, polling for updates...")
|
| 47 |
application.run_polling()
|
| 48 |
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
main()
|
|
|
|
|
|
| 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 |
+
|