Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- .env.example +2 -0
- Dockerfile +18 -0
- app.py +93 -0
- requirements.txt +4 -0
- users.txt +1 -0
.env.example
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BOT_TOKEN=your_telegram_bot_token_here
|
| 2 |
+
SPACE_NAME=your_huggingface_space_name
|
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Copy requirements first for better caching
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
|
| 8 |
+
# Install dependencies
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy all files
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Expose port
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Command to run the app
|
| 18 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import telebot
|
| 3 |
+
from flask import Flask, request
|
| 4 |
+
import threading
|
| 5 |
+
import time
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Initialize Flask app
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
# Get bot token from environment variables
|
| 15 |
+
BOT_TOKEN = os.getenv('BOT_TOKEN', '8686080430:AAFdzAYwM4hNTsyiknJMIfBFTtSOHkgn-E8')
|
| 16 |
+
bot = telebot.TeleBot(BOT_TOKEN)
|
| 17 |
+
|
| 18 |
+
# Admin user IDs
|
| 19 |
+
admin_id = ["7474658501"]
|
| 20 |
+
|
| 21 |
+
# File to store allowed user IDs
|
| 22 |
+
USER_FILE = "users.txt"
|
| 23 |
+
|
| 24 |
+
# File to store command logs
|
| 25 |
+
LOG_FILE = "log.txt"
|
| 26 |
+
|
| 27 |
+
# Function to read user IDs from the file
|
| 28 |
+
def read_users():
|
| 29 |
+
try:
|
| 30 |
+
with open(USER_FILE, "r") as file:
|
| 31 |
+
return file.read().splitlines()
|
| 32 |
+
except FileNotFoundError:
|
| 33 |
+
return []
|
| 34 |
+
|
| 35 |
+
# Function to log command to the file
|
| 36 |
+
def log_command(user_id, command):
|
| 37 |
+
with open(LOG_FILE, "a") as file:
|
| 38 |
+
file.write(f"{datetime.datetime.now()} - {user_id}: {command}\n")
|
| 39 |
+
|
| 40 |
+
# List to store allowed user IDs
|
| 41 |
+
allowed_user_ids = read_users()
|
| 42 |
+
|
| 43 |
+
# Bot handlers (copy from your original m.py)
|
| 44 |
+
@bot.message_handler(commands=['start'])
|
| 45 |
+
def start_command(message):
|
| 46 |
+
user_id = str(message.chat.id)
|
| 47 |
+
if user_id in allowed_user_ids:
|
| 48 |
+
bot.reply_to(message, "Welcome! The bot is ready to use.")
|
| 49 |
+
else:
|
| 50 |
+
bot.reply_to(message, "Access denied. You are not authorized to use this bot.")
|
| 51 |
+
|
| 52 |
+
@bot.message_handler(commands=['help'])
|
| 53 |
+
def help_command(message):
|
| 54 |
+
user_id = str(message.chat.id)
|
| 55 |
+
if user_id in allowed_user_ids:
|
| 56 |
+
help_text = """
|
| 57 |
+
Available commands:
|
| 58 |
+
/start - Start the bot
|
| 59 |
+
/help - Show this help message
|
| 60 |
+
"""
|
| 61 |
+
bot.reply_to(message, help_text)
|
| 62 |
+
else:
|
| 63 |
+
bot.reply_to(message, "Access denied.")
|
| 64 |
+
|
| 65 |
+
# Webhook route for Hugging Face Spaces
|
| 66 |
+
@app.route('/' + BOT_TOKEN, methods=['POST'])
|
| 67 |
+
def webhook():
|
| 68 |
+
if request.headers.get('content-type') == 'application/json':
|
| 69 |
+
json_string = request.get_data().decode('utf-8')
|
| 70 |
+
update = telebot.types.Update.de_json(json_string)
|
| 71 |
+
bot.process_new_updates([update])
|
| 72 |
+
return '', 200
|
| 73 |
+
return '', 400
|
| 74 |
+
|
| 75 |
+
# Health check endpoint
|
| 76 |
+
@app.route('/')
|
| 77 |
+
def health_check():
|
| 78 |
+
return "Bot is running!", 200
|
| 79 |
+
|
| 80 |
+
# Function to set webhook
|
| 81 |
+
def set_webhook():
|
| 82 |
+
# In Hugging Face Spaces, the webhook URL will be your space URL + bot token
|
| 83 |
+
webhook_url = f"https://{os.getenv('SPACE_NAME')}.hf.space/{BOT_TOKEN}"
|
| 84 |
+
bot.remove_webhook()
|
| 85 |
+
bot.set_webhook(url=webhook_url)
|
| 86 |
+
print(f"Webhook set to: {webhook_url}")
|
| 87 |
+
|
| 88 |
+
if __name__ == '__main__':
|
| 89 |
+
# Set webhook when the app starts
|
| 90 |
+
set_webhook()
|
| 91 |
+
|
| 92 |
+
# Run Flask app
|
| 93 |
+
app.run(host='0.0.0.0', port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pyTelegramBotAPI
|
| 2 |
+
requests
|
| 3 |
+
python-dotenv
|
| 4 |
+
flask
|
users.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
7474658501
|