from telethon import events import asyncio from telethon.errors import DataInvalidError from . import kanha_bot target_chat = "@HeXamonbot" teamkanha = "@teamkanha" MAX_CLICKS = 30 RETRY_CLICKS = 4 BOT_DELAY = 2 # seconds async def get_latest_message_with_button(client, chat_id): """ Get the latest message from the bot that still has a button. """ async for msg in client.iter_messages(chat_id, limit=1): if msg.buttons: return msg return None @kanha_bot.on(events.NewMessage(chats=target_chat)) async def handle_lottery_start(event): """Auto-click Poke Lottery buttons until no more are found.""" client = event.client # Only trigger on welcome message if not event.raw_text.lower().startswith("welcome to the poke lottery"): return clicks = 0 retries = 0 message = event last_message = message try: await asyncio.sleep(1) # let bot load buttons while clicks < MAX_CLICKS: # Always refresh current message message = await client.get_messages(message.chat_id, ids=message.id) if message.buttons: try: await message.click(0, 0) clicks += 1 retries = 0 except DataInvalidError: raise RuntimeError("Button click failed: DataInvalidError") except Exception as e: raise RuntimeError(f"Button click failed: {str(e)}") await asyncio.sleep(BOT_DELAY) last_message = message continue # If current message has no button → check latest message with button latest_with_button = await get_latest_message_with_button(client, message.chat_id) if latest_with_button: last_message = latest_with_button try: await last_message.click(0, 0) clicks += 1 retries = 0 except Exception: retries += 1 else: retries += 1 if retries >= RETRY_CLICKS: break await asyncio.sleep(BOT_DELAY) except Exception as e: try: await client.send_message( teamkanha, f"⚠️ Lottery AutoClicker Error:\n\n`{str(e)}`" ) except Exception: pass