Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
|
|
|
| 3 |
from telegram import Update
|
| 4 |
from telegram.ext import Application, CommandHandler, ContextTypes
|
| 5 |
|
|
@@ -9,44 +10,50 @@ logging.basicConfig(
|
|
| 9 |
)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
# ---
|
| 13 |
-
#
|
| 14 |
TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# --- Bot Command Handler ---
|
| 23 |
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 24 |
-
"""Sends a confirmation message that the
|
| 25 |
-
logger.info("Received /start command
|
| 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
|
| 32 |
-
def main() -> None:
|
| 33 |
-
"""
|
| 34 |
if not TOKEN:
|
| 35 |
logger.error("FATAL: TELEGRAM_TOKEN not found in Hugging Face Secrets!")
|
|
|
|
| 36 |
return
|
| 37 |
|
| 38 |
-
|
| 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 |
-
#
|
| 47 |
-
logger.info("
|
| 48 |
-
application.
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
-
main()
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
import asyncio
|
| 4 |
from telegram import Update
|
| 5 |
from telegram.ext import Application, CommandHandler, ContextTypes
|
| 6 |
|
|
|
|
| 10 |
)
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
+
# --- Pre-configured Settings ---
|
| 14 |
+
# Your Bot Token is the ONLY thing kept in secrets for security reasons.
|
| 15 |
TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 16 |
|
| 17 |
+
# PRE-CONFIGURED: The public URL for your specific Space "understanding/Testtg".
|
| 18 |
+
WEBHOOK_URL = "https://understanding-testtg.hf.space"
|
| 19 |
+
|
| 20 |
+
# PRE-CONFIGURED: A randomly generated secret password for the webhook as you requested.
|
| 21 |
+
WEBHOOK_SECRET = "rAnd0mSecr3t_202508071721"
|
| 22 |
+
|
| 23 |
+
# The port Hugging Face Spaces use.
|
| 24 |
+
PORT = int(os.getenv("PORT", 7860))
|
| 25 |
|
| 26 |
|
| 27 |
# --- Bot Command Handler ---
|
| 28 |
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 29 |
+
"""Sends a confirmation message that the webhook test is successful."""
|
| 30 |
+
logger.info("SUCCESS: Received /start command via pre-configured webhook!")
|
| 31 |
+
await update.message.reply_text("Success! The pre-configured files and webhook method are working.")
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
+
# --- Main Webhook Logic ---
|
| 35 |
+
async def main() -> None:
|
| 36 |
+
"""Sets up and runs the webhook bot."""
|
| 37 |
if not TOKEN:
|
| 38 |
logger.error("FATAL: TELEGRAM_TOKEN not found in Hugging Face Secrets!")
|
| 39 |
+
logger.error("Please make sure you have added your bot token to the secrets.")
|
| 40 |
return
|
| 41 |
|
| 42 |
+
# Create the Application instance.
|
| 43 |
+
application = Application.builder().token(TOKEN).build()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
application.add_handler(CommandHandler("start", start_command))
|
| 45 |
|
| 46 |
+
# Tell Telegram's servers where to send updates.
|
| 47 |
+
logger.info(f"Setting webhook for Telegram to call us at: {WEBHOOK_URL}")
|
| 48 |
+
await application.bot.set_webhook(url=WEBHOOK_URL, secret_token=WEBHOOK_SECRET)
|
| 49 |
|
| 50 |
+
# Start the web server to listen for incoming webhooks from Telegram.
|
| 51 |
+
logger.info(f"Starting web server on port {PORT} to listen for webhooks...")
|
| 52 |
+
await application.run_webhook(
|
| 53 |
+
listen="0.0.0.0",
|
| 54 |
+
port=PORT,
|
| 55 |
+
secret_token=WEBHOOK_SECRET,
|
| 56 |
+
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
asyncio.run(main())
|