iozxv commited on
Commit
35d87fb
ยท
verified ยท
1 Parent(s): 2b8b852

Upload 6 files

Browse files
Files changed (2) hide show
  1. README.md +50 -5
  2. database.py +3 -1
README.md CHANGED
@@ -1,6 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- license: apache-2.0
3
- title: C1
4
- sdk: docker
5
- emoji: ๐Ÿƒ
6
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Space Pinger Bot ๐Ÿš€
2
+
3
+ A Telegram bot built with **Pyrogram** and **FastAPI** to keep Hugging Face spaces alive by pinging them sequentially every 5 minutes. The database containing authorized users, admins, and space URLs is automatically created and synced to a Hugging Face dataset for persistent storage.
4
+
5
+ ## ๐Ÿ› ๏ธ Required Secret Environment Variables
6
+
7
+ When deploying this bot on Hugging Face Spaces (or any Docker platform), you **MUST** configure the following variables in the Space settings (under **Variables and Secrets**):
8
+
9
+ 1. **`API_ID`**: The API ID for your Telegram application (get it from [my.telegram.org](https://my.telegram.org)).
10
+ 2. **`API_HASH`**: The API Hash for your Telegram application (get it from [my.telegram.org](https://my.telegram.org)).
11
+ 3. **`BOT_TOKEN`**: The Telegram Bot Token generated by [@BotFather](https://t.me/BotFather).
12
+ 4. **`ADMIN_ID`**: The numeric Telegram ID of the **Super Admin** (e.g., `123456789`). The Super Admin has highest authority (e.g., can remove admins).
13
+ 5. **`HF_TOKEN`**: A Hugging Face User Access Token with **WRITE** permissions (generate it under your Hugging Face account settings at [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)).
14
+ 6. **`HF_DATASET_URL`**: The full URL of your Hugging Face dataset repository (e.g. `https://huggingface.co/datasets/your_username/your_dataset_name` or simply the repository ID like `your_username/your_dataset_name`). The bot will automatically create this dataset if it doesn't already exist and maintain `database.json` within it.
15
+
16
  ---
17
+
18
+ ## ๐Ÿค– Available Commands
19
+
20
+ ### ๐Ÿ’ฌ Public Commands (Anyone)
21
+ - `/id` - View your Telegram User ID (used to request authorization).
22
+ - `/start` - Displays a simple welcome message.
23
+ - `/help` - Explains what the bot does.
24
+ - `/commands` - List commands matching the caller's authorization level.
25
+
26
+ ### ๐Ÿ‘ค Authorized User Commands
27
+ - `/add <hf_url>` - Register a Hugging Face Space URL. (You can also just send a raw Hugging Face URL directly, and the bot will normalize and register it automatically).
28
+ - `/remove <hf_url>` - Remove a Space URL you registered.
29
+ - `/list_url` - List the Space URLs that *you* registered.
30
+
31
+ ### ๐Ÿ‘ฎ Admin Commands
32
+ - `/add_user <user_id>` - Authorize a user to register and manage their own URLs.
33
+ - `/remove_user <user_id>` - Deauthorize a user.
34
+ - `/add_admin <user_id>` - Promote a user to Admin.
35
+ - `/list_url` - List **ALL** registered Space URLs in the system along with who registered them.
36
+ - `/list` - List all Admins and Users (excluding the Super Admin).
37
+
38
+ ### ๐Ÿ‘‘ Super Admin Commands
39
+ - `/remove_admin <user_id>` - Demote an Admin back to a regular user.
40
+ - `/list` - List the Super Admin, all Admins, and all Users.
41
+
42
+ ---
43
+
44
+ ## ๐Ÿณ Deployment to Hugging Face Spaces
45
+
46
+ 1. Create a new Space on Hugging Face: [huggingface.co/new-space](https://huggingface.co/new-space).
47
+ 2. Choose **Docker** as the SDK.
48
+ 3. Select **Blank** (or any Docker template).
49
+ 4. Go to the Space **Settings** page and add the **Secrets** listed above.
50
+ 5. Push the repository files (`Dockerfile`, `requirements.txt`, `config.py`, `database.py`, `main.py`) to the Space.
51
+ 6. The space will automatically build the Docker image, run the FastAPI web server on port `7860`, launch the Telegram bot, and begin the sequential 5-minute pinger loop.
database.py CHANGED
@@ -6,8 +6,10 @@ import asyncio
6
  from huggingface_hub import HfApi, hf_hub_download
7
  import config
8
 
 
 
9
  DATA_FILE_NAME = "database.json"
10
- LOCAL_PATH = os.path.join(os.getcwd(), DATA_FILE_NAME)
11
 
12
  # In-memory database state
13
  db_state = {
 
6
  from huggingface_hub import HfApi, hf_hub_download
7
  import config
8
 
9
+ import tempfile
10
+
11
  DATA_FILE_NAME = "database.json"
12
+ LOCAL_PATH = os.path.join(tempfile.gettempdir(), DATA_FILE_NAME)
13
 
14
  # In-memory database state
15
  db_state = {