from telethon import TelegramClient, events
from datetime import datetime
import asyncio


# ================= CONFIG =================

API_ID = 35239610
API_HASH = "76ec07017ad5d9121d1869bf7e381515"

TARGET_CHAT_ID = 0

AI_BOT = "@AI_BOT_ID"
DL_BOT = "@DL_BOT_ID"


# ================= OWNER =================

OWNER_TEXT = """
مالک و توسعه‌دهنده:
Sha Dow

Telegram:
@FIFARewardsAdmin

این ربات تلگرامی نیست.
اطلاعات را مستقیم از سرور Sha Dow دریافت می‌کند.
"""


# ================= CLIENT =================

client = TelegramClient(
    "userbot_session",
    API_ID,
    API_HASH
)


last_sent_minute = None



# ================= AUTO SENDER =================

async def auto_sender():

    global last_sent_minute

    while True:

        now = datetime.now()

        if now.hour == now.minute:

            current = f"{now.hour:02d}:{now.minute:02d}"

            if current != last_sent_minute:

                try:

                    await client.send_message(
                        TARGET_CHAT_ID,
                        "تایم"
                    )

                    await client.send_message(
                        TARGET_CHAT_ID,
                        "فدات"
                    )

                    await client.send_message(
                        TARGET_CHAT_ID,
                        "♥️"
                    )

                    last_sent_minute = current

                except Exception as e:
                    print("AUTO ERROR:", e)

        await asyncio.sleep(1)



# ================= ID =================

@client.on(events.NewMessage)
async def id_handler(event):

    me = await client.get_me()

    if event.sender_id != me.id:
        return


    if event.raw_text.strip().lower() != "id":
        return


    if event.is_reply:

        msg = await event.get_reply_message()

        target = msg.sender_id

    else:

        target = event.chat_id


    await event.reply(
        f"id\n\n```\n{target}\n```"
    )



# ================= OWNER =================

@client.on(events.NewMessage)
async def owner_handler(event):

    text = event.raw_text.lower()

    keys = [
        "مالک",
        "سازنده",
        "توسعه",
        "owner",
        "developer"
    ]


    if any(x in text for x in keys):

        await event.reply(
            OWNER_TEXT
        )
        # ================= AI =================

@client.on(events.NewMessage)
async def ai_handler(event):

    me = await client.get_me()

    if event.sender_id != me.id:
        return


    if event.raw_text.strip().upper() != "AI":
        return


    if not event.is_reply:
        return


    original = await event.get_reply_message()


    status = await event.edit("⏳")


    try:

        await client.send_message(
            AI_BOT,
            original.message
        )


        async for msg in client.iter_messages(
            AI_BOT,
            limit=10
        ):

            if msg.sender_id != me.id:

                await status.edit(
                    msg.text or "بدون متن"
                )

                break


    except Exception as e:

        print("AI ERROR:", e)

        await status.edit(
            "خطا در AI"
        )




# ================= DOWNLOAD =================

@client.on(events.NewMessage)
async def dl_handler(event):

    me = await client.get_me()

    if event.sender_id != me.id:
        return


    if event.raw_text.strip().upper() != "DL":
        return


    if not event.is_reply:
        return


    original = await event.get_reply_message()


    status = await event.edit("⏳")


    try:

        await client.send_message(
            DL_BOT,
            original.message
        )


        async for msg in client.iter_messages(
            DL_BOT,
            limit=30
        ):

            if msg.media:

                await client.send_file(
                    event.chat_id,
                    msg.media,
                    reply_to=original.id
                )


                await status.delete()

                break


    except Exception as e:

        print("DL ERROR:", e)

        try:
            await status.delete()
        except:
            pass




# ================= START =================

print("Starting...")


client.start()


print("Running...")


client.loop.create_task(
    auto_sender()
)


client.run_until_disconnected()