"""milestones.py: celebrates the 1 000th, 5 000th, 10 000th… message in each chat room, in Python. It counts the members' messages (never a bot's, its own included), and at a milestone it says who sent it and sends client-milestone, which the pages turn into confetti (confetti.js). Python 3.10+. pip install -r requirements.txt, then python milestones.py (reads .env). Its key (dashboard, New API key): rights subscribe and client_events, kind presence, channels: prefix presence-chat-. """ from pikoman_bot import Bot, env ROOMS = env("ROOMS", "presence-chat-lobby,presence-chat-help").split(",") MILESTONES = {int(n) for n in env("MILESTONES", "1000,5000,10000,25000,50000,100000").split(",")} # Join tokens signed with the bot's own key. Access 3: it may speak in the rooms. bot = Bot("Milestones", access=3) for name in ROOMS: room = bot.join(name) counts = {"messages": 0} # the members' messages in this room since the bot started @room.on("message") async def message(sender, data, info, room=room, counts=counts): # A bot never hears its own messages; other bots are skipped by the server's list of bots. if info.get("bot") or room.is_bot(sender): return counts["messages"] += 1 n = counts["messages"] if n not in MILESTONES: return # Who sent it, from the member data your backend signed into their join token: never from # what the message says, which the sender could write anything into. member = room.member(sender) who = member.get("name", "Someone") await room.send("message", {"text": f"🎉 Message {n} in this room! {who} sent it."}) await room.send("milestone", {"count": n, "name": who, "user_id": member.get("user_id"), "connection": sender}) print(f"Milestones {', '.join(str(n) for n in sorted(MILESTONES))} in {', '.join(ROOMS)}") bot.start()