forked from Sankar8098/Advanced-File-Store-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
169 lines (139 loc) · 5.75 KB
/
Copy pathconfig.py
File metadata and controls
169 lines (139 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
from dotenv import load_dotenv
import logging
from typing import List, Optional
from pathlib import Path
# Configure logging with more detailed format
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
def validate_token(token: Optional[str], name: str) -> str:
"""Validate Telegram bot token format."""
if not token:
raise ValueError(f"{name} is not set")
if not token.strip():
raise ValueError(f"{name} cannot be empty")
if not token.count(':') == 1:
raise ValueError(f"{name} format is invalid")
return token.strip()
def validate_api_credentials() -> tuple:
"""Validate API credentials."""
api_id = os.getenv('API_ID')
api_hash = os.getenv('API_HASH')
if not api_id or not api_hash:
raise ValueError("API_ID and API_HASH must be set")
try:
api_id = int(api_id)
except ValueError:
raise ValueError("API_ID must be a number")
if not isinstance(api_hash, str) or len(api_hash) != 32:
raise ValueError("API_HASH must be a 32-character string")
return api_id, api_hash
def parse_admin_list(admins_str: Optional[str]) -> List[int]:
"""Parse admin list from environment variable."""
if not admins_str:
return []
try:
return [int(x.strip()) for x in admins_str.split() if x.strip().isdigit()]
except ValueError:
logger.warning("Invalid admin ID found in ADMINS list")
return []
class Config:
"""Configuration class with enhanced validation and features."""
# Bot Settings
TELEGRAM_BOT_TOKEN = validate_token(os.getenv('TELEGRAM_BOT_TOKEN'), 'TELEGRAM_BOT_TOKEN')
WORKER_BOT_TOKEN = validate_token(os.getenv('WORKER_BOT_TOKEN'), 'WORKER_BOT_TOKEN')
# API Settings
API_ID, API_HASH = validate_api_credentials()
# User Settings
OWNER_ID = int(os.getenv('OWNER_ID', '0'))
if OWNER_ID == 0:
logger.warning("OWNER_ID not set! Some features may be limited")
CHANNEL_ID = int(os.getenv('CHANNEL_ID', '0'))
if CHANNEL_ID == 0:
logger.warning("CHANNEL_ID not set! Channel features will be disabled")
# Database Settings
MONGODB_URI = os.getenv('MONGODB_URI')
if not MONGODB_URI:
raise ValueError("MONGODB_URI is not set")
# Performance Settings
MAX_CONNECTIONS = int(os.getenv('MAX_CONNECTIONS', '100'))
CONNECTION_TIMEOUT = int(os.getenv('CONNECTION_TIMEOUT', '5000'))
# Channel and Subscription Settings
FORCE_SUB_CHANNEL = int(os.getenv('FORCE_SUB_CHANNEL', '0'))
FORCE_SUB_MESSAGE = os.getenv(
'FORCE_SUB_MESSAGE',
'<b>⚠️ Please join our channel to use this bot!</b>\n\n'
'Click the button below to join:'
)
JOIN_REQUEST_ENABLED = os.getenv('JOIN_REQUEST_ENABLED', 'False').lower() == 'true'
# Bot Customization
START_MESSAGE = os.getenv(
'START_MESSAGE',
'<b>👋 Welcome to our File Store Bot!</b>\n\n'
'<i>I can help you store and share files securely. '
'Send me any file to get started!</i>'
)
START_PIC = os.getenv('START_PIC', 'https://graph.org/file/29c1cf05d61f49ed3aa0b.jpg')
RANDOM_START_PIC = os.getenv('RANDOM_START_PIC', 'True').lower() == 'true'
CUSTOM_CAPTION = os.getenv(
'CUSTOM_CAPTION',
'{filename}\n\n'
'💾 Size: {filesize}\n'
'👤 Shared by: @{username}\n\n'
'🤖 @YourBotUsername'
)
BOT_STATS_TEXT = os.getenv(
'BOT_STATS_TEXT',
'📊 <b>Bot Statistics</b>\n\n'
'👥 Users: {total_users:,}\n'
'📁 Files: {total_files:,}\n'
'💾 Storage: {storage_used}\n'
'⚡️ Uptime: {uptime}'
)
USER_REPLY_TEXT = os.getenv(
'USER_REPLY_TEXT',
'👋 Hello!\n\n'
'Use /help to see available commands.\n'
'Use /start to restart the bot.'
)
# Admin Settings
ADMINS: List[int] = parse_admin_list(os.getenv('ADMINS'))
if OWNER_ID != 0 and OWNER_ID not in ADMINS:
ADMINS.append(OWNER_ID)
NOTIFY_ON_JOIN = os.getenv('NOTIFY_ON_JOIN', 'True').lower() == 'true'
LOG_CHANNEL = int(os.getenv('LOG_CHANNEL', '0'))
if LOG_CHANNEL == 0:
logger.warning("LOG_CHANNEL not set! Logging to channel will be disabled")
# URL Shortener Settings
GET2SHORT_API_KEY = os.getenv('GET2SHORT_API_KEY', '')
MODIJIURL_API_KEY = os.getenv('MODIJIURL_API_KEY', '')
# File Settings
MAX_FILE_SIZE = int(os.getenv('MAX_FILE_SIZE', str(2 * 1024 * 1024 * 1024))) # 2GB default
ALLOWED_MIME_TYPES = {
'video': ['video/mp4', 'video/x-matroska', 'video/webm'],
'audio': ['audio/mpeg', 'audio/mp4', 'audio/ogg'],
'document': ['application/pdf', 'application/zip', 'application/x-rar-compressed']
}
# Cache Settings
CACHE_TIME = int(os.getenv('CACHE_TIME', '300')) # 5 minutes default
CACHE_SIZE = int(os.getenv('CACHE_SIZE', '1000'))
@classmethod
def get_allowed_mime_types(cls) -> List[str]:
"""Get flat list of all allowed MIME types."""
return [mime for types in cls.ALLOWED_MIME_TYPES.values() for mime in types]
@classmethod
def is_mime_type_allowed(cls, mime_type: str) -> bool:
"""Check if a MIME type is allowed."""
return mime_type in cls.get_allowed_mime_types()
@classmethod
def get_file_type(cls, mime_type: str) -> Optional[str]:
"""Get file type category from MIME type."""
for file_type, mime_types in cls.ALLOWED_MIME_TYPES.items():
if mime_type in mime_types:
return file_type
return None