File size: 16,146 Bytes
351c02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1f4d96
351c02a
 
 
 
 
 
 
 
 
 
 
 
 
596dcf7
 
b42332e
351c02a
 
 
596dcf7
b42332e
 
596dcf7
 
 
b42332e
351c02a
596dcf7
351c02a
 
9222ced
 
351c02a
b42332e
 
351c02a
 
 
73d4d68
351c02a
73d4d68
 
 
 
 
351c02a
 
73d4d68
351c02a
 
bea4cb0
351c02a
 
 
 
73d4d68
 
 
351c02a
78bdb87
73d4d68
78bdb87
 
73d4d68
 
78bdb87
73d4d68
351c02a
 
73d4d68
 
 
 
351c02a
 
73d4d68
351c02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eba9c1
 
 
544d445
351c02a
bea4cb0
351c02a
 
 
544d445
 
 
 
1eba9c1
 
 
 
 
544d445
 
78bdb87
351c02a
 
 
 
 
 
 
 
 
1eba9c1
351c02a
 
 
 
02fd48f
 
 
 
 
 
 
351c02a
 
02fd48f
351c02a
 
 
 
02fd48f
351c02a
 
 
 
02fd48f
 
 
 
 
 
 
 
351c02a
 
70d8fba
351c02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import os
import sys
import logging
import nest_asyncio
import random
import asyncio
import pytz
from flask import Flask
from threading import Thread
from datetime import datetime, timedelta
from pymongo import MongoClient
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from telethon import TelegramClient, events, Button
from telethon.sessions import StringSession
from telethon.tl import functions
import re

# ───── Utils ─────
adjectives = ["Silent", "Crazy", "Golden", "Hidden", "Brave", "Fierce", "Mysterious", "Savage", "Royal", "Witty", "Legendary", "Epic", "Noble", "Clever", "Loyal", "Wild", "Electric", "Fearless", "Crimson", "Frozen", "Charming", "Rebel", "Turbo", "Lone", "Radiant", "Raging", "Dusty", "Majestic", "Grumpy", "Shadow"]
nouns = ["Warriors", "Coders", "Friends", "Legends", "Pirates", "Tigers", "Wolves", "Hackers", "Ninjas", "Riders", "Hunters", "Kings", "Queens", "Giants", "Lords", "Champions", "Stormers", "Creators", "Ghosts", "Wizards", "Knights", "Samurais", "Explorers", "Titans", "Outlaws", "Rebels", "Squad", "Troopers", "Dreamers", "Avengers"]

logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(message)s", stream=sys.stdout)
log = logging.getLogger("app")

nest_asyncio.apply()
API_ID = int(os.getenv("API_ID", 3704772))
API_HASH = os.getenv("API_HASH", "b8e50a035abb851c0dd424e14cac4c06")
BOT_TOKEN = os.getenv("BOT_TOKEN")
MONGO_URI = os.getenv("MONGO_URI")
ADMIN_ID = int(os.getenv("ADMIN_ID", 6574063018))

client = MongoClient(MONGO_URI)
db = client["telethon_bots"]
accounts = db["accounts"]
plans = db["plans"]
group_logs= db["group_logs"]
bot = TelegramClient("bot", API_ID, API_HASH)
userbots = {}
scheduler = AsyncIOScheduler()

def random_group_name():
    return f"{random.choice(adjectives)} {random.choice(nouns)}"

# Timezone
tz = pytz.timezone("Asia/Kolkata")

# Dummy userbot registry
userbots = {}  # Fill this with: {user_id: bot_instance}

import random

def create_plan(account, limit):
    now = datetime.now(tz)
    end_of_day = now.replace(hour=23, minute=59, second=59, microsecond=0)

    times = set()
    while len(times) < limit:
        # Generate random seconds from now to end of day
        delta_seconds = int((end_of_day - now).total_seconds())
        random_offset = random.randint(60, delta_seconds)  # at least 1 min from now
        random_time = now + timedelta(seconds=random_offset)
        times.add(random_time.replace(microsecond=0))  # avoid microsecond collisions

    for t in sorted(times):
        plans.insert_one({
            "account_id": account["_id"],
            "scheduled_time": t.isoformat(),
            "created": 0 # optional: placeholder for future use
        })
    print(f"βœ… Created {len(times)} random plan(s) for account {account['_id']}")
    
def apply_plan():
    now = datetime.now(tz)
    count = 0

    for plan in plans.find():
        try:
            plan_time = datetime.fromisoformat(plan["scheduled_time"]).astimezone(tz)
            if plan_time <= now:
                continue  # Skip past plans

            account = accounts.find_one({"_id": plan["account_id"]})
            if not account:
                print(f"⚠️ No account found for plan {plan['_id']}")
                continue

            userbot = userbots.get(account["user_id"])
            if not userbot:
                print(f"⚠️ No userbot for account {account['_id']}, skipping...")
                continue

            async def job_wrapper(ub=userbot, acc=account, pl=plan):
                await auto_create_groups(ub, acc, pl)

            job_id = f"job_{plan['_id']}"
            scheduler.add_job(
                job_wrapper,
                trigger='date',
                run_date=plan_time,
                id=job_id,
                name=f"AutoGroupPlan_{account['_id']}"
            )

            print(f"πŸ“Œ Scheduled job for account {account['_id']} at {plan_time.strftime('%H:%M:%S')} (ID: {job_id})")
            count += 1

        except Exception as e:
            print(f"❌ Error scheduling plan {plan.get('_id')}: {e}")

    print(f"βœ… Applied and scheduled {count} job(s).")


def reset_and_plan():
    print("πŸ” Resetting plans and scheduling new ones...")
    plans.delete_many({})
    for acc in accounts.find():
        limit = random.randint(3, 5)
        create_plan(acc, limit)
    apply_plan()
    print("βœ… Plans created and scheduled.")

# Schedule reset daily at 00:00 IST
scheduler.add_job(reset_and_plan, CronTrigger(hour=0, minute=0, timezone="Asia/Kolkata"), id="daily_reset_job", replace_existing=True)

# Telethon /reset command
@bot.on(events.NewMessage(pattern=r'^/reset$'))
async def handle_reset(event):
    sender = await event.get_sender()
    if sender.id != ADMIN_ID:
        return await event.reply("❌ You are not authorized to run this command.")

    for job in scheduler.get_jobs():
        if job.func.__name__ == "reset_and_plan" or job.id == "daily_reset_job":
            job.remove()

    reset_and_plan()
    scheduler.add_job(reset_and_plan, CronTrigger(hour=0, minute=0, timezone="Asia/Kolkata"), id="daily_reset_job", replace_existing=True)
    await event.reply("βœ… Schedule reset and re-planned for tonight 00:00 IST.")

@bot.on(events.NewMessage(pattern=r'^/stats$'))
async def handle_stats(event):
    tz = pytz.timezone("Asia/Kolkata")
    now = datetime.now(tz)

    msg = "πŸ“Š **Today's Schedule:**\n\n"
    msg += f"πŸ—“οΈ Date: {now.strftime('%d/%m/%Y')}\n"
    msg += f"πŸ•’ Time: {now.strftime('%I:%M %p')} IST\n\n"

    plans_today = list(plans.find())
    if not plans_today:
        return await event.reply("πŸ“­ No plans found today.")

    def fake_strike(text):
        return ''.join(c + '\u0336' for c in text)

    account_times = {}
    for plan in plans_today:
        acc = accounts.find_one({"_id": plan["account_id"]})
        if not acc:
            continue
        name = acc["name"]
        time_obj = datetime.fromisoformat(plan["scheduled_time"]).astimezone(tz)
        time_str = time_obj.strftime("%I:%M %p")
        if name not in account_times:
            account_times[name] = []
        if time_obj < now:
            mono_strike = fake_strike(time_str)
            account_times[name].append(f"`{mono_strike}`")
        else:
            account_times[name].append(f"`{time_str}`")

    for name, times in account_times.items():
        msg += f"πŸ‘€ **{name}** β†’ {', '.join(times)}\n\n"

    next_run = None
    for job in scheduler.get_jobs():
        if job.id == "daily_reset_job":
            next_run = job.next_run_time
            break
    if next_run:
        msg += f"πŸ•› Next Reset: `{next_run.astimezone(tz).strftime('%d/%m/%Y %I:%M %p')} IST`"

    await event.reply(msg, parse_mode='md')

async def auto_create_groups(userbot, acc_doc, plan_doc=None):
    try:
        title = random_group_name()
        result = await userbot(functions.channels.CreateChannelRequest(
            title=title,
            about=",",
            megagroup=True
        ))
        group = result.chats[0]
        group_id = group.id

        for _ in range(4):
            await userbot.send_message(group_id, random_group_name())
            await asyncio.sleep(2)

        try:
            stickers = ["CAACAgUAAxkBAAEKZStlTxkC-cvQJwABvHoUjRacAVIHCFcAAgECAALaTqVVCpx_Lc2ZoLguBA"]
            await userbot.send_file(group_id, random.choice(stickers))
        except:
            pass

        if plan_doc:
            plans.update_one({"_id": plan_doc["_id"]}, {"$inc": {"created": 1}})

        # Save for cleanup
        group_logs.insert_one({
            "group_id": group_id,
            "account_id": acc_doc["_id"],
            "created_on": datetime.now(tz).isoformat()
        })

        info = await userbot.get_me()
        
    except Exception as e:
        await bot.send_message(ADMIN_ID, f"❌ Group creation failed for {acc_doc['name']}: {e}")
        log.error(f"❌ Group creation failed: {e}")

async def start_userbot(session_str, owner_id):
    try:
        userbot = TelegramClient(StringSession(session_str), API_ID, API_HASH)
        await userbot.start()
        info = await userbot.get_me()
        if accounts.find_one({"user_id": info.id}):
            return None
        doc = {
            "user_id": info.id,
            "name": info.first_name,
            "username": info.username or None,
            "session": session_str,
            "created": 0,
            "owner_id": owner_id,
            "added_on": datetime.utcnow()
        }
        accounts.insert_one(doc)
        userbots[info.id] = userbot
        asyncio.create_task(userbot.run_until_disconnected())
        return info
    except Exception as e:
        log.info(f"❌ Failed to add account: {e}")
        return None

async def load_all_userbots():
    log.info("πŸ”„ Loading all stored userbots...")
    for acc in accounts.find():
        try:
            ub = TelegramClient(StringSession(acc["session"]), API_ID, API_HASH)
            await ub.start()
            userbots[acc["user_id"]] = ub
            asyncio.create_task(ub.run_until_disconnected())
            log.info(f"βœ… Loaded: {acc['name']} (@{acc.get('username', 'N/A')})")
        except Exception as e:
            await bot.send_message(ADMIN_ID, f"❌ Failed to start {acc['name']}")
            log.info(f"❌ Failed to start {acc['name']}: {e}")

# ───── Bot Commands ─────
@bot.on(events.NewMessage(pattern="/start"))
async def start_cmd(event):
    if event.sender_id != ADMIN_ID:
        return
    await event.reply(
        "πŸ‘‹ Welcome!\n\n"
        "/addacc β€” Add account\n"
        "/delacc β€” Delete an account\n"
        "/total β€” Count of connected accounts\n"
        "/stats β€” View today's schedule\n"
        "/reset β€” Reset and re-plan daily schedule\n"
        "/create <account_name> β€” Create groups for the specified account\n"
        "/help β€” Show this help message"
    )


@bot.on(events.NewMessage(pattern="/help"))
async def help_cmd(event):
    if event.sender_id != ADMIN_ID:
        return
    await event.reply(
        "πŸ› οΈ **Bot Commands Help:**\n\n"
        "/start β€” Start the bot and see the available commands\n"
        "/addacc β€” Add a new account to the bot\n"
        "/delacc β€” Delete an existing account\n"
        "/total β€” Get the total count of connected accounts\n"
        "/stats β€” View today's scheduling plan for accounts\n"
        "/reset β€” Reset and plan the schedule for the new day\n"
        "/create <account_name> β€” Create groups for the specified account\n"
        "/help β€” Show this help message\n\n"
        "These commands are available to the admin only."
    )

@bot.on(events.NewMessage(pattern="/addacc"))
async def addacc_cmd(event):
    if event.sender_id != ADMIN_ID:
        return
    async with bot.conversation(event.chat_id, timeout=60) as conv:
        await conv.send_message("✍️ Send your String Session:")
        session_msg = await conv.get_response()
        session_str = session_msg.text.strip()
        info = await start_userbot(session_str, event.sender_id)
        if info:
            await conv.send_message(f"βœ… Added: {info.first_name} (@{info.username or 'N/A'})")
        else:
            await conv.send_message("⚠️ Already added or failed.")

@bot.on(events.NewMessage(pattern="/total"))
async def total_accounts_cmd(event):
    if event.sender_id != ADMIN_ID:
        return
    count = accounts.count_documents({})
    await event.respond(f"πŸ“Š Total connected: **{count}**")

@bot.on(events.NewMessage(pattern="/delacc"))
async def delacc_cmd(event):
    if event.sender_id != ADMIN_ID:
        return
    user_accs = list(accounts.find({"owner_id": ADMIN_ID}))
    if not user_accs:
        await event.reply("😢 No accounts.")
        return
    buttons, row = [], []
    for idx, acc in enumerate(user_accs, 1):
        row.append(Button.inline(f"{acc['name']} (@{acc.get('username', 'N/A')})", data=f"del_{acc['user_id']}"))
        if idx % 2 == 0:
            buttons.append(row)
            row = []
    if row:
        buttons.append(row)
    await event.respond("Select to delete:", buttons=buttons)

@bot.on(events.CallbackQuery(pattern=b"del_"))
async def handle_delete(event):
    if event.sender_id != ADMIN_ID:
        return
    user_id = int(event.data.decode().split("_")[1])
    acc_doc = accounts.find_one({"user_id": user_id})
    if not acc_doc:
        await event.answer("Not found.", alert=True)
        return
    await event.edit(f"⚠️ Confirm delete {acc_doc['name']}?", buttons=[
        [Button.inline("βœ… Confirm", data=f"confdel_{user_id}"), Button.inline("❌ Cancel", data=b"cancel")]
    ])

@bot.on(events.CallbackQuery(pattern=b"confdel_"))
async def confirm_delete(event):
    if event.sender_id != ADMIN_ID:
        return
    user_id = int(event.data.decode().split("_")[1])
    acc_doc = accounts.find_one_and_delete({"user_id": user_id})
    if acc_doc:
        try:
            if user_id in userbots:
                await userbots[user_id].disconnect()
                del userbots[user_id]
            await event.edit(f"βœ… Deleted: {acc_doc['name']}")
            await bot.send_message(ADMIN_ID, f"πŸ—‘οΈ Deleted: {acc_doc['name']} (@{acc_doc.get('username', 'N/A')})")
            log.info(f"πŸ—‘οΈ Deleted account: {acc_doc['name']} (@{acc_doc.get('username', 'N/A')})")
        except Exception as e:
            await event.edit(f"Deleted from DB, error stopping: {e}")
    else:
        await event.edit("⚠️ Not found.")

@bot.on(events.CallbackQuery(data=b"cancel"))
async def cancel_cb(event):
    if event.sender_id != ADMIN_ID:
        return
    await event.edit("❌ Cancelled.")

@bot.on(events.CallbackQuery(pattern=b"pickcreate_"))
async def pick_create_callback(event):
    if event.sender_id != ADMIN_ID:
        return
    user_id = int(event.data.decode().split("_")[1])
    acc_doc = accounts.find_one({"user_id": user_id})
    if not acc_doc:
        await event.answer("Account not found.", alert=True)
        return
    if user_id not in userbots:
        await event.answer("Account is not active.", alert=True)
        return
    await event.edit(f"⏳ Creating groups for {acc_doc['name']}...")
    await auto_create_groups(userbots[user_id], acc_doc)

@bot.on(events.NewMessage(pattern=r"/create\s+(.+)", func=lambda e: e.sender_id == ADMIN_ID))
async def create_group_cmd(event):
    query = event.pattern_match.group(1).strip()
    safe_query = re.escape(query)
    matches = list(accounts.find({
        "$or": [
            {"name": {"$regex": safe_query, "$options": "i"}},
            {"username": {"$regex": safe_query, "$options": "i"}}
        ]
    }))
    if not matches:
        await event.reply("❌ No matching account found.")
        return
    if len(matches) == 1:
        acc_doc = matches[0]
        user_id = acc_doc["user_id"]
        if user_id not in userbots:
            await event.reply("⚠️ Account is not active.")
            return
        await event.reply(f"⏳ Creating groups for {acc_doc['name']}...")
        await auto_create_groups(userbots[user_id], acc_doc)
    else:
        buttons = []
        for acc in matches:
            label = f"{acc['name']} (@{acc.get('username', 'N/A')})"
            buttons.append([Button.inline(label, data=f"pickcreate_{acc['user_id']}")])
        await event.respond("⚠️ Multiple matches found. Select the correct account:", buttons=buttons)

# ───── Flask App ─────
app = Flask(__name__)
@app.route("/")
def home():
    return "βœ… Channel-making bot is running."

def run_flask():
    app.run(host="0.0.0.0", port=7860, use_reloader=False)

# ───── Main Startup ─────
async def main():
    Thread(target=run_flask).start()
    scheduler.start()
    await load_all_userbots()
    await bot.start(bot_token=BOT_TOKEN)
    log.info("πŸ€– Bot is now running on Hugging Face")
    await bot.run_until_disconnected()

if __name__ == "__main__":
    asyncio.run(main())