Spaces:
Sleeping
Sleeping
Download app.py from Hardik893508/Forwarderbot: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/spaces/Hardik893508/Forwarderbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Hardik893508/Forwarderbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Hardik893508/Forwarderbot/resolve/main/app.py
1.87 kB
| import os | |
| import re | |
| from flask import Flask, request | |
| import telebot | |
| # Environment variables se secrets uthayenge | |
| BOT_TOKEN = os.environ.get("BOT_TOKEN") | |
| GROUP_CHAT_ID = os.environ.get("GROUP_CHAT_ID") | |
| SPACE_ID = os.environ.get("SPACE_ID") # Hugging Face yeh automatic deta hai | |
| bot = telebot.TeleBot(BOT_TOKEN) | |
| app = Flask(__name__) | |
| # Link filter karne ke liye regex | |
| LINK_REGEX = r'(https?://[^\s]+)' | |
| def getMessage(): | |
| json_string = request.get_data().decode('utf-8') | |
| update = telebot.types.Update.de_json(json_string) | |
| if update.message: | |
| message = update.message | |
| # Sirf private chat se aaye links ko forward karega | |
| if message.chat.type == 'private' and message.text: | |
| links = re.findall(LINK_REGEX, message.text) | |
| if links: | |
| for link in links: | |
| try: | |
| # Timeout 10 seconds set kiya hai taaki network delay se crash na ho | |
| bot.send_message(GROUP_CHAT_ID, f"New Link Shared:\n{link}", timeout=10) | |
| except Exception as e: | |
| print(f"Error forwarding link: {e}") | |
| return "!", 200 | |
| def webhook_setup(): | |
| bot.remove_webhook() | |
| # Automatic aapke space ka correct URL banayega | |
| if SPACE_ID: | |
| # Username aur Space name ko small letters me convert karega | |
| clean_space_id = SPACE_ID.lower().replace("/", "-") | |
| space_url = f"https://{clean_space_id}.hf.space" | |
| status = bot.set_webhook(url=space_url + '/' + BOT_TOKEN) | |
| if status: | |
| return f"🎉 Success! Bot Webhook set to: {space_url}", 200 | |
| return "❌ Failed to set Webhook. Check SPACE_ID or Token.", 500 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |