import os import requests import gradio as gr from huggingface_hub import WebhooksServer with gr.Blocks() as ui: gr.Markdown("# šŸš€ Webhook Translator is Running (Secure Mode)!") app = WebhooksServer(ui=ui) @app.add_webhook("/discord") async def forward_to_discord(payload: dict): print("--- NEW WEBHOOK RECEIVED ---") # 1. Securely grab the URL and clean it (removes invisible spaces and fixes capital 'Https') raw_url = os.environ.get("DISCORD_WEBHOOK_URL", "") discord_url = raw_url.strip().replace("Https://", "https://") if not discord_url: print("ERROR: Could not find DISCORD_WEBHOOK_URL in Space Secrets.") return {"status": "Error"} # 2. Extract the data repo_name = payload.get("repo", {}).get("name", "Unknown Repo") action = payload.get("event", {}).get("action", "updated") scope = payload.get("event", {}).get("scope", "repo") # 3. Format the message if scope == "discussion.comment": discussion_title = payload.get("discussion", {}).get("title", "a discussion") comment = payload.get("comment", {}).get("content", "") url = payload.get("comment", {}).get("url", {}).get("web", f"https://huggingface.co/{repo_name}") message = f"šŸ’¬ **New Comment on `{repo_name}`**\n**Thread:** {discussion_title}\n> {comment}\nšŸ”— {url}" elif scope == "discussion": discussion_title = payload.get("discussion", {}).get("title", "a discussion") url = payload.get("discussion", {}).get("url", {}).get("web", f"https://huggingface.co/{repo_name}") message = f"šŸ“¢ **New Discussion in `{repo_name}`**\n**Thread:** {discussion_title}\nšŸ”— {url}" else: repo_type = payload.get("repo", {}).get("type", "model") url = payload.get("repo", {}).get("url", {}).get("web", f"https://huggingface.co/{repo_name}") message = f"šŸš€ **Hugging Face Alert:** The {repo_type} `{repo_name}` was just **{action}**.\nšŸ”— {url}" discord_message = {"content": message} print("Attempting to send message to Discord...") # 4. Send the message with a fallback mechanism headers = {"User-Agent": "HuggingFace-Webhook-Catcher/1.0", "Content-Type": "application/json"} try: response = requests.post(discord_url, json=discord_message, headers=headers) if response.status_code in [200, 204]: print("SUCCESS! Message sent to Discord.") else: print(f"ERROR! Discord rejected it. Reason: {response.text}") except Exception as e: print(f"CRASH: Failed to connect to Discord: {e}") # If HF blocks the DNS resolution, try Discord's legacy API domain if "NameResolutionError" in str(e): print("Attempting fallback to discordapp.com...") fallback_url = discord_url.replace("discord.com", "discordapp.com") try: fallback_response = requests.post(fallback_url, json=discord_message, headers=headers) if fallback_response.status_code in [200, 204]: print("SUCCESS using fallback URL!") else: print(f"Fallback failed. Reason: {fallback_response.text}") except Exception as e2: print(f"Fallback also failed: {e2}") return {"status": "Processed"} if __name__ == "__main__": # Disable SSR to fix the 405 Method Not Allowed error app.launch(ssr_mode=False)