Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 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 |
+
# --- Get Token from Hugging Face Secrets ---
|
| 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 confirmation message that the trick is working."""
|
| 23 |
+
logger.info("Received /start command. Replying now.")
|
| 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 |
+
"""Start the bot using the alternative domain."""
|
| 33 |
+
if not TOKEN:
|
| 34 |
+
logger.error("FATAL: TELEGRAM_TOKEN not found in Hugging Face Secrets!")
|
| 35 |
+
return
|
| 36 |
+
|
| 37 |
+
logger.info(f"Initializing bot to use API base: {ALT_API_URL}")
|
| 38 |
+
|
| 39 |
+
# Create the Application and explicitly set the base_url
|
| 40 |
+
application = Application.builder().token(TOKEN).base_url(ALT_API_URL).build()
|
| 41 |
+
|
| 42 |
+
# Register the /start command handler
|
| 43 |
+
application.add_handler(CommandHandler("start", start_command))
|
| 44 |
+
|
| 45 |
+
# Start polling
|
| 46 |
+
logger.info("Bot is starting, polling for updates...")
|
| 47 |
+
application.run_polling()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
main()
|