-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
278 lines (213 loc) Β· 8.84 KB
/
Copy pathbot.py
File metadata and controls
278 lines (213 loc) Β· 8.84 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from telebot import TeleBot
from telebot.apihelper import requests
from telebot.types import ReplyKeyboardMarkup, KeyboardButton
from dotenv import load_dotenv
import os
load_dotenv()
BOT_TOKEN = os.getenv("BOT_TOKEN")
API_URL = os.getenv("API_URL")
admin_id = int(os.getenv("ADMIN_ID"))
bot = TeleBot(BOT_TOKEN)
user_states = {}
def is_admin(message):
return message.from_user.id == admin_id
@bot.message_handler(commands=["start"])
def start(message):
if is_admin(message):
bot.send_message(
message.chat.id,
"π Welcome Admin!\n\nUse /create to generate new users."
)
@bot.message_handler(commands=["create"])
def create(message):
if is_admin(message):
chat_id = message.chat.id
user_states[chat_id] = {}
bot.send_message(chat_id, "π’ How many users do you want to create?")
bot.register_next_step_handler(message, get_user_count)
def get_user_count(message):
chat_id = message.chat.id
try:
user_count = int(message.text)
if user_count <= 0:
raise ValueError
except ValueError:
bot.send_message(chat_id, "β Please enter a valid positive number.")
return bot.register_next_step_handler(message, get_user_count)
user_states[chat_id]["user_count"] = user_count
bot.send_message(chat_id, "π Enter the panel address:")
bot.register_next_step_handler(message, get_address_panel)
def get_address_panel(message):
chat_id = message.chat.id
address_panel = message.text.strip()
user_states[chat_id]["address"] = address_panel
bot.send_message(chat_id, "π€ Enter the panel username:")
bot.register_next_step_handler(message, get_username_panel)
def get_username_panel(message):
chat_id = message.chat.id
username_panel = message.text.strip()
user_states[chat_id]["panel_username"] = username_panel
bot.send_message(chat_id, "π Enter the panel password:")
bot.register_next_step_handler(message, get_password_panel)
def get_password_panel(message):
chat_id = message.chat.id
password_panel = message.text.strip()
user_states[chat_id]["panel_password"] = password_panel
bot.send_message(chat_id, "π€ Enter new users base username:")
bot.register_next_step_handler(message, get_base_username)
def get_base_username(message):
chat_id = message.chat.id
base_username = message.text.strip()
user_states[chat_id]["base_username"] = base_username
markup = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
markup.row(KeyboardButton("active"), KeyboardButton("on_hold"))
bot.send_message(
chat_id,
"βοΈ Select user status:",
reply_markup=markup
)
bot.register_next_step_handler(message, get_status_new_user)
def get_status_new_user(message):
chat_id = message.chat.id
status = message.text.strip()
if status not in ["active", "on_hold"]:
bot.send_message(chat_id, "β Please choose one of: active / on_hold")
return bot.register_next_step_handler(message, get_status_new_user)
user_states[chat_id]["status"] = status
if status == "active":
bot.send_message(chat_id, "β³ Enter expiration (days, 0 for unlimited):")
bot.register_next_step_handler(message, get_expire_days)
else:
user_states[chat_id]["expire_days"] = 0
bot.send_message(chat_id, "π¦ Enter data limit (GB):")
bot.register_next_step_handler(message, get_data_limit)
def get_expire_days(message):
chat_id = message.chat.id
try:
expire_days = int(message.text)
if expire_days < 0:
raise ValueError
except ValueError:
bot.send_message(chat_id, "β Please enter a valid number (0 or more).")
return bot.register_next_step_handler(message, get_expire_days)
user_states[chat_id]["expire_days"] = expire_days
bot.send_message(chat_id, "π¦ Enter data limit (GB):")
bot.register_next_step_handler(message, get_data_limit)
def get_data_limit(message):
chat_id = message.chat.id
try:
data_limit_gb = int(message.text)
if data_limit_gb <= 0:
raise ValueError
except ValueError:
bot.send_message(chat_id, "β Please enter a positive number for data limit (GB).")
return bot.register_next_step_handler(message, get_data_limit)
user_states[chat_id]["data_limit_gb"] = data_limit_gb
bot.send_message(chat_id, "π Enter group IDs (comma separated, e.g. 1,2,3):")
bot.register_next_step_handler(message, get_group_ids)
def get_group_ids(message):
chat_id = message.chat.id
text = message.text.strip()
try:
group_ids = [int(x.strip()) for x in text.split(",") if x.strip()]
if not group_ids:
raise ValueError
except ValueError:
bot.send_message(chat_id, "β Please enter valid group IDs (comma separated numbers).")
return bot.register_next_step_handler(message, get_group_ids)
user_states[chat_id]["group_ids"] = group_ids
status = user_states[chat_id]["status"]
if status == "on_hold":
bot.send_message(chat_id, "β± Enter on-hold timeout (days):")
bot.register_next_step_handler(message, get_on_hold_timeout)
else:
user_states[chat_id]["on_hold_timeout_days"] = 0
user_states[chat_id]["on_hold_expire_days"] = 0
bot.send_message(chat_id, "β³ Creating users, please waitβ¦")
create_users_for_chat(chat_id)
def get_on_hold_timeout(message):
chat_id = message.chat.id
try:
days = int(message.text)
if days <= 0:
raise ValueError
except ValueError:
bot.send_message(chat_id, "β Please enter a positive number for on-hold timeout (days).")
return bot.register_next_step_handler(message, get_on_hold_timeout)
user_states[chat_id]["on_hold_timeout_days"] = days
bot.send_message(chat_id, "β Enter on-hold expire duration (days):")
bot.register_next_step_handler(message, get_on_hold_expire_duration)
def get_on_hold_expire_duration(message):
chat_id = message.chat.id
try:
days = int(message.text)
if days <= 0:
raise ValueError
except ValueError:
bot.send_message(chat_id, "β Please enter a positive number for expire duration (days).")
return bot.register_next_step_handler(message, get_on_hold_expire_duration)
user_states[chat_id]["on_hold_expire_days"] = days
bot.send_message(chat_id, "β³ Creating users, please waitβ¦")
create_users_for_chat(chat_id)
def create_users_for_chat(chat_id):
state = user_states.get(chat_id)
if not state:
bot.send_message(chat_id, "β οΈ Session expired. Please start again with /create.")
return
user_count = state["user_count"]
address = state["address"]
panel_username = state["panel_username"]
panel_password = state["panel_password"]
base_username = state["base_username"]
status = state["status"]
expire_days = state["expire_days"]
data_limit_gb = state["data_limit_gb"]
group_ids = state["group_ids"]
on_hold_timeout_days = state.get("on_hold_timeout_days", 0)
on_hold_expire_days = state.get("on_hold_expire_days", 0)
expire = expire_days
data_limit = data_limit_gb
on_hold_timeout = on_hold_timeout_days if on_hold_timeout_days > 0 else None
on_hold_expire_duration = on_hold_expire_days if on_hold_expire_days > 0 else None
success_count = 0
fail_count = 0
for i in range(user_count):
username = f"{base_username}-{i+1}"
payload = {
"address": address,
"panel_username": panel_username,
"panel_password": panel_password,
"username": username,
"status": status,
"expire": expire,
"data_limit": data_limit,
"group_ids": group_ids,
"on_hold_timeout": on_hold_timeout,
"on_hold_expire_duration": on_hold_expire_duration,
}
try:
resp = requests.post(API_URL, json=payload)
data = resp.json()
if resp.status_code == 200 and data.get("success", True):
success_count += 1
bot.send_message(chat_id, f"β
User {username} created successfully.")
else:
fail_count += 1
bot.send_message(
chat_id,
f"β Failed to create user {username}.\n"
f"Status: {resp.status_code}\nResponse: {data}"
)
except Exception as e:
fail_count += 1
bot.send_message(chat_id, f"β Error while creating user {username}:\n{e}")
bot.send_message(
chat_id,
f"π Done.\n"
f"β
Success: {success_count}\n"
f"β Failed: {fail_count}"
)
user_states.pop(chat_id, None)
def start_bot():
print("π€ Bot is now running...")
bot.polling()