iozxv commited on
Commit
4c6ec88
ยท
verified ยท
1 Parent(s): e22e753

Upload 6 files

Browse files
Files changed (2) hide show
  1. README.md +50 -4
  2. main.py +17 -12
README.md CHANGED
@@ -1,5 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- license: apache-2.0
3
- sdk: docker
4
- emoji: ๐Ÿ“‰
5
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.
main.py CHANGED
@@ -264,20 +264,25 @@ async def remove_url_command(client, message):
264
  return
265
 
266
  if len(message.command) < 2:
267
- await message.reply_text("Usage: `/remove <hf_space_url>`")
268
  return
269
 
270
- url_input = message.command[1]
271
- try:
272
- success, normalized = database.remove_url(url_input, user_id)
273
- if success:
274
- await message.reply_text(f"โœ… **Success!** URL removed from ping list:\n`{normalized}`")
275
- else:
276
- await message.reply_text(f"โŒ URL not found in ping list:\n`{normalized}`")
277
- except PermissionError as e:
278
- await message.reply_text(f"โŒ **Permission Denied:** {str(e)}")
279
- except Exception as e:
280
- await message.reply_text(f"โŒ **Error:** {str(e)}")
 
 
 
 
 
281
 
282
  @bot.on_message(filters.command("list_url"))
283
  async def list_url_command(client, message):
 
264
  return
265
 
266
  if len(message.command) < 2:
267
+ await message.reply_text("Usage: `/remove <hf_space_url1> <hf_space_url2> ...`")
268
  return
269
 
270
+ urls_input = message.command[1:]
271
+ results = []
272
+
273
+ for url_input in urls_input:
274
+ try:
275
+ success, normalized = database.remove_url(url_input, user_id)
276
+ if success:
277
+ results.append(f"โœ… Removed: `{normalized}`")
278
+ else:
279
+ results.append(f"โŒ Not found: `{normalized}`")
280
+ except PermissionError as e:
281
+ results.append(f"โŒ Permission Denied: {url_input} - {str(e)}")
282
+ except Exception as e:
283
+ results.append(f"โŒ Error: {url_input} - {str(e)}")
284
+
285
+ await message.reply_text("\n".join(results))
286
 
287
  @bot.on_message(filters.command("list_url"))
288
  async def list_url_command(client, message):