Skip to content

Commit 1011057

Browse files
committed
feat: control group or user permisions who can use a bot
1 parent baf2bb0 commit 1011057

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
BOT_TOKEN="your_telegram_bot_token_here"
22
DEBUG=False
3+
LIMIT_BOT_ACCESS=False # if True, bot will only work for users in ALLOWED_USERNAMES or ALLOWED_CHAT_IDS
4+
ALLOWED_USERNAMES= # list of allowed usernames as strings separated by commas
5+
ALLOWED_CHAT_IDS= # list of allowed chat ids as strings separated by commas

src/main.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,9 @@
1010
from telegram.constants import MessageEntityType
1111
from logger import print_logs
1212
from video_utils import compress_video, download_video, cleanup_file
13+
from permissions import is_user_or_chat_not_allowed, supported_sites
1314

1415
load_dotenv()
15-
supported_sites = [
16-
"**https://",
17-
"instagram.com/",
18-
"tiktok.com/",
19-
"reddit.com/",
20-
"x.com/",
21-
"youtube.com/shorts",
22-
]
23-
2416

2517
def load_responses():
2618
"""Function loading bot responses."""
@@ -97,6 +89,11 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): #
9789
await update.message.reply_text(random.choice(responses))
9890
return
9991

92+
# Check if user is not allowed
93+
if is_user_or_chat_not_allowed(update.effective_user.username, update.effective_chat.id):
94+
await update.message.reply_text("You are not allowed to use this bot")
95+
return
96+
10097
# Handle Instagram stories
10198
if "instagram.com/stories/" in message_text:
10299
await update.message.reply_text("Сторіз не можу скачати. Треба логін")
@@ -114,7 +111,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): #
114111

115112
# Download the video
116113
video_path = download_video(url)
117-
114+
# Check if video was downloaded
118115
if not video_path or not os.path.exists(video_path):
119116
return
120117

src/permissions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Module for handling user permissions and access control.
2+
This module provides functionality to check if users are allowed to access
3+
the bot based on their Telegram usernames and chat IDs configured in environment variables.
4+
"""
5+
6+
import os
7+
from typing import Optional
8+
9+
allowed_usernames = [x.strip() for x in os.getenv("ALLOWED_USERNAMES", "").split(",") if x]
10+
allowed_chat_ids = [int(x) for x in os.getenv("ALLOWED_CHAT_IDS", "").split(",") if x]
11+
limit_bot_access = os.getenv("LIMIT_BOT_ACCESS")
12+
13+
# Check if user or chat is not allowed. Returns True if not allowed, False if allowed
14+
def is_user_or_chat_not_allowed(username: Optional[str], chat_id: int) -> bool:
15+
"""Check if username or chat_id is not in the allowed lists.
16+
17+
Args:
18+
username: Telegram username to check
19+
20+
Returns:
21+
True if neither user nor chat is allowed, False if either is allowed
22+
"""
23+
# default case when no limits are set
24+
if limit_bot_access == "False":
25+
return False
26+
27+
# If chat_id is allowed, grant access regardless of username
28+
if chat_id in allowed_chat_ids:
29+
return False
30+
31+
# Otherwise check if username is allowed
32+
return username not in allowed_usernames
33+
34+
supported_sites = [
35+
"**https://",
36+
"instagram.com/",
37+
"tiktok.com/",
38+
"reddit.com/",
39+
"x.com/",
40+
"youtube.com/shorts",
41+
]

0 commit comments

Comments
 (0)