Spaces:
Configuration error
Configuration error
| import os | |
| import logging | |
| import asyncio | |
| from telegram import Update | |
| from telegram.ext import Application, CommandHandler, ContextTypes | |
| # --- Basic Setup --- | |
| logging.basicConfig( | |
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # --- Pre-configured Settings --- | |
| # Your Bot Token is the ONLY thing kept in secrets for security reasons. | |
| TOKEN = os.getenv("TELEGRAM_TOKEN") | |
| # PRE-CONFIGURED: The public URL for your specific Space "understanding/Testtg". | |
| WEBHOOK_URL = "https://understanding-testtg.hf.space" | |
| # PRE-CONFIGURED: A randomly generated secret password for the webhook as you requested. | |
| WEBHOOK_SECRET = "rAnd0mSecr3t_202508071721" | |
| # The port Hugging Face Spaces use. | |
| PORT = int(os.getenv("PORT", 7860)) | |
| # --- Bot Command Handler --- | |
| async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| """Sends a confirmation message that the webhook test is successful.""" | |
| logger.info("SUCCESS: Received /start command via pre-configured webhook!") | |
| await update.message.reply_text("Success! The pre-configured files and webhook method are working.") | |
| # --- Main Webhook Logic --- | |
| async def main() -> None: | |
| """Sets up and runs the webhook bot.""" | |
| if not TOKEN: | |
| logger.error("FATAL: TELEGRAM_TOKEN not found in Hugging Face Secrets!") | |
| logger.error("Please make sure you have added your bot token to the secrets.") | |
| return | |
| # Create the Application instance. | |
| application = Application.builder().token(TOKEN).build() | |
| application.add_handler(CommandHandler("start", start_command)) | |
| # Tell Telegram's servers where to send updates. | |
| logger.info(f"Setting webhook for Telegram to call us at: {WEBHOOK_URL}") | |
| await application.bot.set_webhook(url=WEBHOOK_URL, secret_token=WEBHOOK_SECRET) | |
| # Start the web server to listen for incoming webhooks from Telegram. | |
| logger.info(f"Starting web server on port {PORT} to listen for webhooks...") | |
| await application.run_webhook( | |
| listen="0.0.0.0", | |
| port=PORT, | |
| secret_token=WEBHOOK_SECRET, | |
| ) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |