Moderator
A word filter for your rooms: a warning at the first word from your list, and at the third within ten minutes, out of the room. Only that room; the rest of their session stays.
Works today With @pikoman/ws 1.0.0-rc.10 from npm and Pikoman as it runs now.
A folder that runs as it is, in Node and Python: moderator.mjs, moderator.py, .env.example, README.md, package.json, pikoman_bot.py, requirements.txt, words.txt.
What it does
The bot reads every chat message. When one has a word or phrase from words.txt (case and accents do not matter, and a word only counts on its own), it warns the sender by name. The warning goes to the room, or with WARN_ALONE=1 to that member alone, and REMOVE=kick or REMOVE=ban removes them at the third warning. A message has already reached the room when the bot reads it: a bot can warn and remove, not unsend.
- a message
- moderator
- warning
- kick or ban
The key it needs
subscribe and client_events, and to remove members kick or ban: unlike disconnect, which can close any connection of the app, they reach only the key’s own channels.
- Start from
- Chosen: Moderator
- Name
- Moderator
- Rights
- Not chosen:
publishNot chosen:statsNot chosen:clientsNot chosen:disconnectNot chosen:disconnect_allChosen:subscribeChosen:client_eventsNot chosen:kickNot chosen:ban - Channel kinds
- Not chosen: public Not chosen: private Chosen: presence
- Channels
- Chosen: Only some channelsprefix
presence-chat- - Addresses
- Chosen: Only these addressesthe server the bot runs on
- Expires
- Never, or in 90 days for a trial
- To remove
- add
kickforREMOVE=kick, orbanforREMOVE=ban(a ban allows a kick too)
Set up
The docs cover the key and connecting; everything about the bot itself is on this page.
- The Pro plan. A key that joins channels is a bot’s key, and bot keys come with Pro.
- The key above. In the dashboard: your app, New API key, filled in as shown. How each choice works: Keys and rights. The secret,
sk_k_….…, is shown once (what it looks like). - Your app id and the secret in
.env, copied from.env.example. Nowhere else: not in a page, not in git. - Connecting. The bot signs its own join tokens with the key, and Pikoman shows it as a bot: bots in presence channels.
- Your members join with tokens your backend signs, with their name and user id in the member data,
{"user_id": "42", "name": "Ada"}: the token endpoint.
How it works
It reads every message
Lower case, accents off, everything but letters and digits a space, then a look for each word with spaces around it: “Idiót” matches idiot, “stupidity” does not match stupid.
It knows who it is
Warnings are counted per user id from the signed member data, so a new tab does not start over. Warnings older than ten minutes are forgotten.
It warns
“Bo, please keep it friendly (warning 1 of 3).” In the room, or to Bo alone with
WARN_ALONE=1.It removes
room.kick({userId}, {reason})takes every connection of that user out of this room; withbanSecondsthey cannot join it again until the ban runs out (a day at most). They getkickedwith the reason, the others see them leave. WithoutREMOVEit keeps warning.
The source
These are the files in the zip, as they are: copy one, download one, or take the whole folder.
Node: moderator.mjs
// moderator.mjs: a word filter for your chat rooms. It reads every message, and when one has a
// word from words.txt it warns the sender; at the third warning within ten minutes it removes
// them from the room (REMOVE=kick), or removes them and keeps them out a while (REMOVE=ban).
// Node 22+. npm install, then npm start (reads .env).
//
// Warnings go to the room, which everyone sees, or with WARN_ALONE=1 to that member only; and
// REMOVE=kick or REMOVE=ban takes them out of that one room.
//
// Its key (dashboard, New API key): rights subscribe and client_events, kind presence, channels:
// prefix presence-chat-. With REMOVE=kick also the right kick; with REMOVE=ban the right ban.
import { readFileSync } from "node:fs";
import { Pikoman, OfflineAuthenticator } from "@pikoman/ws";
const ROOMS = (process.env.ROOMS || "presence-chat-lobby").split(",");
const REMOVE = process.env.REMOVE || ""; // "", "kick" or "ban" (the key needs that right)
const WARN_ALONE = process.env.WARN_ALONE === "1"; // warn the member alone
const BAN_SECONDS = Number(process.env.BAN_SECONDS || 600); // at most a day
const STRIKES = 3; // warnings before removal
const WINDOW = 10 * 60 * 1000; // warnings older than ten minutes are forgotten
/** Text as it is compared: lower case, accents off, anything but letters and digits a space. */
const plain = (text) =>
` ${text.normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ")} `;
// One word or phrase per line; lines starting with # are comments.
const WORDS = readFileSync(new URL("./words.txt", import.meta.url), "utf8")
.split("\n")
.map((line) => line.trim())
.filter((line) => line && !line.startsWith("#"))
.map((word) => plain(word));
const pikoman = new Pikoman(process.env.PIKOMAN_APP, {
ws_address: process.env.PIKOMAN_WS || "wss://next.pikoman.se",
});
const auth = OfflineAuthenticator.fromSecret(process.env.PIKOMAN_API_KEY, { name: "Moderator" }, 3);
for (const name of ROOMS) {
const room = pikoman.listenPresence(name, auth);
const strikes = new Map(); // user -> times of their warnings
room.onClient("message", (from, data) => {
if (room.isBot(from)) return;
const text = plain(String(data?.text ?? ""));
if (!WORDS.some((word) => text.includes(word))) return;
// Who it is: the user id your backend signed into their member data, never what they wrote.
const member = room.getMember(from) ?? {};
const user = member.user_id ?? from;
const who = member.name ?? "Someone";
const now = Date.now();
const recent = (strikes.get(user) ?? []).filter((t) => now - t < WINDOW);
recent.push(now);
strikes.set(user, recent);
if (recent.length < STRIKES || !REMOVE) {
const text = `${who}, please keep it friendly (warning ${Math.min(recent.length, STRIKES)} of ${STRIKES}).`;
if (WARN_ALONE) room.sendTo(from, "message", { text });
else room.send("message", { text });
if (recent.length >= STRIKES) console.log(`${name}: ${who} (${user}) has ${recent.length} warnings`);
return;
}
// The third warning: out of this room. Every connection of that user, when it has a user id.
const target = member.user_id ? { userId: member.user_id } : from;
const reason = "Language, after two warnings";
if (REMOVE === "ban") room.kick(target, { reason, banSeconds: BAN_SECONDS });
else room.kick(target, { reason });
strikes.delete(user);
room.send("message", { text: `${who} was removed from the room.` });
});
room.refused((code) => console.error(`${name}: ${code}`)); // not_allowed on a kick: check the key's rights
}
console.log(`Moderating ${ROOMS.join(", ")}: ${WORDS.length} words, ${REMOVE || "warnings only"}`);
pikoman.revoked(() => {
console.error("The bot's key was deleted.");
process.exit(1);
});Python: moderator.py
"""moderator.py: a word filter for your chat rooms, in Python. It reads every message, and when one
has a word from words.txt it warns the sender; at the third warning within ten minutes it removes
them from the room (REMOVE=kick), or removes them and keeps them out a while (REMOVE=ban).
Python 3.10+. pip install -r requirements.txt, then python moderator.py (reads .env).
Warnings go to the room, which everyone sees, or with WARN_ALONE=1 to that member only; and
REMOVE=kick or REMOVE=ban takes them out of that one room.
Its key (dashboard, New API key): rights subscribe and client_events, kind presence, channels:
prefix presence-chat-. With REMOVE=kick also the right kick; with REMOVE=ban the right ban.
"""
import re
import time
import unicodedata
from pathlib import Path
from pikoman_bot import Bot, env
ROOMS = env("ROOMS", "presence-chat-lobby").split(",")
REMOVE = env("REMOVE") # "", "kick" or "ban" (the key needs that right)
WARN_ALONE = env("WARN_ALONE") == "1" # warn the member alone
BAN_SECONDS = int(env("BAN_SECONDS", "600")) # at most a day
STRIKES = 3 # warnings before removal
WINDOW = 10 * 60 # warnings older than ten minutes are forgotten
def plain(text: str) -> str:
"""Text as it is compared: lower case, accents off, anything but letters and digits a space."""
text = "".join(c for c in unicodedata.normalize("NFKD", text) if not unicodedata.combining(c))
return " " + re.sub(r"[^\w]+|_", " ", text.lower()) + " "
# One word or phrase per line; lines starting with # are comments.
lines = (Path(__file__).with_name("words.txt")).read_text(encoding="utf-8").splitlines()
WORDS = [plain(line.strip()) for line in lines if line.strip() and not line.strip().startswith("#")]
bot = Bot("Moderator", access=3)
def moderate(room):
strikes = {} # user -> times of their warnings
@room.on("message")
async def message(sender, data, info):
if room.is_bot(sender):
return
text = plain(str((data or {}).get("text", "")))
if not any(word in text for word in WORDS):
return
# Who it is: the user id your backend signed into their member data, never what they wrote.
member = room.member(sender)
user = member.get("user_id", sender)
who = member.get("name", "Someone")
now = time.time()
recent = [t for t in strikes.get(user, []) if now - t < WINDOW] + [now]
strikes[user] = recent
if len(recent) < STRIKES or not REMOVE:
warning = f"{who}, please keep it friendly (warning {min(len(recent), STRIKES)} of {STRIKES})."
if WARN_ALONE:
await room.send_to(sender, "message", {"text": warning})
else:
await room.send("message", {"text": warning})
if len(recent) >= STRIKES:
print(f"{room.name}: {who} ({user}) has {len(recent)} warnings")
return
# The third warning: out of this room. Every connection of that user, when it has a user id.
target = {"user_id": member["user_id"]} if "user_id" in member else sender
reason = "Language, after two warnings"
await room.kick(target, reason, BAN_SECONDS if REMOVE == "ban" else None)
strikes.pop(user, None)
await room.send("message", {"text": f"{who} was removed from the room."})
for name in ROOMS:
moderate(bot.join(name))
print(f"Moderating {', '.join(ROOMS)}: {len(WORDS)} words, {REMOVE or 'warnings only'}")
bot.start()The words: words.txt
# The words and phrases the moderator warns for, one per line. Case and accents do not matter,
# and a word only counts on its own: "stupidity" does not match "stupid". Replace these with your own.
idiot
stupid
shut up
loserpikoman_bot.py: what every Python bot here shares, the tokens, the websocket and the rooms
"""pikoman_bot.py: what a Pikoman bot needs in Python, in one file next to the bot.
- Rest: signed HTTP requests (publish, to one member, stats), each with a token made for that
one request, as KEYS.md says: the method, the path and a hash of the exact body.
- Bot and Room: a websocket that joins presence channels with join tokens signed by the bot's
own API key, keeps the member list (and which members are bots), hands client events to your
handlers, sends, sends to one member and kicks. It reconnects by itself.
Python 3.10+, pip install -r requirements.txt (websockets, BSD-3-Clause; cryptography,
Apache-2.0 OR BSD-3-Clause). Reads PIKOMAN_APP, PIKOMAN_API_KEY and, for tests, PIKOMAN_WS and
PIKOMAN_HTTP from the environment or from a .env file beside the bot.
"""
import asyncio
import base64
import hashlib
import inspect
import json
import os
import random
import signal
import time
import urllib.error
import urllib.parse
import urllib.request
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from websockets.asyncio.client import connect
from websockets.exceptions import ConnectionClosed
LOCAL = ("localhost", "127.0.0.1", "[::1]")
def load_env(path=".env"):
"""KEY=value lines from .env; what the environment already has wins."""
try:
with open(path, encoding="utf-8") as file:
lines = file.read().splitlines()
except FileNotFoundError:
return
for line in lines:
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
name, value = (part.strip() for part in line.split("=", 1))
value = value.strip("\"'")
if value and name not in os.environ:
os.environ[name] = value
load_env()
def env(name, default=""):
"""A setting from the environment; empty counts as not set."""
return os.environ.get(name) or default
def b64(data: bytes) -> str:
return base64.urlsafe_b64encode(data).rstrip(b"=").decode()
def checked(address: str, secure: str) -> str:
"""https:// or wss://; plain http:// or ws:// only to this computer, so keys never go in the clear."""
parsed = urllib.parse.urlsplit(address)
if parsed.scheme != secure and not (parsed.scheme == secure[:-1] and parsed.hostname in ("localhost", "127.0.0.1", "::1")):
raise ValueError(f"{address}: use {secure}:// (plain {secure[:-1]}:// only for localhost)")
return address.rstrip("/")
class PikomanError(Exception):
def __init__(self, status, text):
super().__init__(f"{status}: {text}")
self.status = status
class Key:
"""An API key's secret, sk_<key id>.<seed>: the id goes in each token's header, the seed signs it."""
def __init__(self, secret: str):
if not secret.startswith("sk_") or "." not in secret:
raise ValueError("PIKOMAN_API_KEY looks like sk_k_….…: the secret the dashboard showed once")
key_id, seed = secret[3:].split(".", 1)
self.id = key_id
self._key = Ed25519PrivateKey.from_private_bytes(base64.urlsafe_b64decode(seed + "=" * (-len(seed) % 4)))
def sign(self, claims: dict) -> str:
"""An EdDSA JWT with the key id in its header."""
header = {"alg": "EdDSA", "typ": "JWT", "kid": self.id}
signed = b64(json.dumps(header).encode()) + "." + b64(json.dumps(claims).encode())
return signed + "." + b64(self._key.sign(signed.encode()))
def __repr__(self):
return f"Key({self.id}, [hidden])"
def target(member) -> dict:
"""A connection id ("c_…"), or {"user_id": "42"} for every connection of that user."""
if isinstance(member, dict):
return {"to_user": str(member["user_id"])}
return {"to": member}
class Rest:
"""The HTTP API, every request signed with the API key and bound to that one request."""
def __init__(self, app=None, api_key=None, address=None):
self.app = app or env("PIKOMAN_APP")
if not self.app:
raise ValueError("Set PIKOMAN_APP: your app id, app-…")
self.key = Key(api_key or env("PIKOMAN_API_KEY"))
self.address = checked(address or env("PIKOMAN_HTTP", "https://next-api.pikoman.se"), "https")
def request(self, method: str, path: str, data=None) -> dict:
body = b"" if data is None else json.dumps(data).encode()
now = int(time.time())
token = self.key.sign({
"iss": self.app, "iat": now, "exp": now + 60, "htm": method, "htu": path,
"bh": b64(hashlib.sha256(body).digest()),
})
request = urllib.request.Request(self.address + path, data=body if data is not None else None, method=method, headers={
"Authorization": "Bearer " + token,
"Content-Type": "application/json",
})
try:
with urllib.request.urlopen(request, timeout=10) as response:
text = response.read()
return json.loads(text) if text else {}
except urllib.error.HTTPError as e:
raise PikomanError(e.code, e.read().decode(errors="replace")) from None
def publish(self, channel: str, event: str, data=None, to=None) -> dict:
"""POST /{channel}/{event}; with `to`, to one member only."""
path = f"/{urllib.parse.quote(channel, safe='=@,.;')}/{urllib.parse.quote(event, safe='=@,.;')}"
if to is not None:
path += "?" + urllib.parse.urlencode(target(to))
return self.request("POST", path, data)
def stats(self, channel: str) -> dict:
return self.request("GET", f"/{urllib.parse.quote(channel, safe='=@,.;')}/stats")
async def call(fn, *args):
"""Runs a handler, plain or async; an error in it is printed and the bot goes on."""
try:
result = fn(*args)
if inspect.isawaitable(result):
await result
except Exception as e: # noqa: BLE001: one handler must never stop the bot
print(f"handler {getattr(fn, '__name__', fn)}: {e!r}")
def client_name(event: str) -> str:
return event if event.startswith("client-") else "client-" + event
class Room:
"""A presence channel the bot is in: its members, which of them are bots, and its events."""
def __init__(self, bot, name: str):
self.bot = bot
self.name = name
self.members = {} # connection id -> member data, as their join token gave it
self.bots = {} # connection id -> the API key id a bot joined with: only the server says
self._handlers = {}
self._any = []
self._joins = []
self._leaves = []
self._errors = []
def on(self, event: str):
"""@room.on("message"): fn(sender, data, info) for that client event (client-message)."""
def add(fn):
self._handlers.setdefault(client_name(event), []).append(fn)
return fn
return add
def on_any(self, fn):
"""@room.on_any: fn(sender, event, data, info) for every client event."""
self._any.append(fn)
return fn
def on_join(self, fn):
"""@room.on_join: fn(connection, data) when a member joins."""
self._joins.append(fn)
return fn
def on_leave(self, fn):
"""@room.on_leave: fn(connection, data) when a member leaves."""
self._leaves.append(fn)
return fn
def on_error(self, fn):
"""@room.on_error: fn(code), such as join_refused, not_allowed or rate_limited."""
self._errors.append(fn)
return fn
def is_bot(self, connection: str) -> bool:
return connection in self.bots
def member(self, connection: str) -> dict:
data = self.members.get(connection)
return data if isinstance(data, dict) else {}
async def send(self, event: str, data=None):
"""A client event to the other members (at most 10 a second per connection)."""
await self.bot.send({"op": "send", "channel": self.name, "event": client_name(event), "data": data, "echo": False})
async def send_to(self, member, event: str, data=None):
"""To one member only: a connection id, or {"user_id": "42"}."""
await self.bot.send({"op": "send", "channel": self.name, "event": client_name(event), "data": data, "echo": False, **target(member)})
async def kick(self, member, reason=None, ban_seconds=None):
"""Out of this room: needs the kick right (ban, with ban_seconds)."""
message = {"op": "kick", "channel": self.name, **target(member)}
if reason:
message["reason"] = reason
if ban_seconds:
message["ban_seconds"] = ban_seconds
await self.bot.send(message)
async def handle(self, message: dict):
op = message.get("op")
if op == "members":
self.members = dict(message.get("members") or {})
self.bots = dict(message.get("bots") or {})
elif op == "member_joined":
self.members[message["id"]] = message.get("data")
if message.get("bot"):
self.bots[message["id"]] = message["bot"]
for fn in self._joins:
await call(fn, message["id"], message.get("data"))
elif op == "member_left":
data = self.members.pop(message["id"], None)
self.bots.pop(message["id"], None)
for fn in self._leaves:
await call(fn, message["id"], data)
elif op == "client_event":
sender, event, data = message.get("from"), message.get("event", ""), message.get("data")
# Set by the server, never by the sender: direct (to this bot alone), user_id, bot.
info = {k: message[k] for k in ("direct", "user_id", "bot") if k in message}
for fn in self._handlers.get(event, []):
await call(fn, sender, data, info)
for fn in self._any:
await call(fn, sender, event, data, info)
elif op == "error":
print(f"{self.name}: {message.get('code')}")
for fn in self._errors:
await call(fn, message.get("code"))
class Bot:
"""One websocket for the bot, in as many rooms as it joins. await bot.run() keeps it going."""
def __init__(self, name: str, access: int = 3, app=None, api_key=None, address=None):
self.app = app or env("PIKOMAN_APP")
if not self.app:
raise ValueError("Set PIKOMAN_APP: your app id, app-…")
self.key = Key(api_key or env("PIKOMAN_API_KEY"))
self.address = checked(address or env("PIKOMAN_WS", "wss://next.pikoman.se"), "wss")
self.data = {"name": name} # what the members see next to "bot"
self.access = access # 3: may speak (the key needs client_events); 0: listens
self.rooms = {}
self.id = None
self._ws = None
self._ready = []
def join(self, channel: str) -> Room:
room = self.rooms[channel] = Room(self, channel)
return room
def on_ready(self, fn):
"""@bot.on_ready: fn() each time the bot is connected and has asked to join its rooms."""
self._ready.append(fn)
return fn
def token(self, channel: str) -> str:
"""A join token for this connection and channel, signed with the bot's API key."""
return self.key.sign({
"connection": self.id, "channel": channel, "access": self.access,
"data": self.data, "exp": int(time.time()) + 300,
})
async def send(self, message: dict):
if self._ws is not None:
await self._ws.send(json.dumps(message))
def start(self, *tasks):
"""Runs the bot (and any other coroutines, such as a web server) until Ctrl+C or SIGTERM."""
def stop(*_):
raise KeyboardInterrupt
signal.signal(signal.SIGTERM, stop) # as from systemd or podman stop: the same as Ctrl+C
async def main():
await asyncio.gather(self.run(), *tasks)
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Stopped.")
async def run(self):
delay = 1
paused = 60 # after 4402: the first wait, doubling to five minutes
while True:
try:
async with connect(f"{self.address}/{self.app}") as ws:
self._ws = ws
async for raw in ws:
message = json.loads(raw)
if message.get("op") == "connected":
delay = 1
self.id = message["id"]
for name in self.rooms:
await self.send({"op": "join", "channel": name, "token": self.token(name)})
for fn in self._ready:
await call(fn)
elif message.get("channel") in self.rooms:
await self.rooms[message["channel"]].handle(message)
elif message.get("op") == "error":
print("pikoman:", message.get("code"))
except ConnectionClosed as e:
code = e.rcvd.code if e.rcvd is not None else None
if code == 4403:
raise SystemExit("The bot's key was deleted.") from None
if code == 4401:
raise SystemExit("The app disconnected the bot.") from None
if code == 4402:
# The key's bot rights are paused (the app's owner is off Pro): not for good.
# Try again in minutes, and the bot is back on its own when Pro is.
self._ws = None
self.id = None
print(f"The bot's key is paused until the app is on Pro again; trying again in about {paused // 60} min.")
await asyncio.sleep(random.uniform(paused / 2, paused))
paused = min(paused * 2, 300)
continue
print("connection closed:", code if code is not None else "no close frame")
except OSError as e:
print("could not connect:", e)
self._ws = None
self.id = None
paused = 60
# Back again after a random wait, longer each time it fails, up to 30 s.
await asyncio.sleep(random.uniform(0.5, delay))
delay = min(delay * 2, 30)# Copy to .env and fill in. Never commit .env.
# Your app id, app-…
PIKOMAN_APP=
# The bot's own API key, sk_k_….… (rights subscribe and client_events, prefix presence-chat-; kick or ban to remove)
PIKOMAN_API_KEY=
# The rooms to moderate, comma-separated (default presence-chat-lobby)
ROOMS=
# Coming update only: kick or ban at the third warning (blank: warnings only)
REMOVE=
# Coming update only: how long a ban keeps them out, in seconds (default 600, at most 86400)
BAN_SECONDS=
# Coming update only: 1 sends each warning to that member alone
WARN_ALONE=
# Another server than wss://next.pikoman.se, for tests
PIKOMAN_WS={
"name": "pikoman-moderator-bot",
"version": "1.0.0",
"private": true,
"type": "module",
"description": "A word filter for Pikoman chat rooms: warns, and with the kick or ban right removes after three warnings.",
"license": "MIT",
"scripts": {
"start": "node --env-file=.env moderator.mjs"
},
"engines": {
"node": ">=22"
},
"dependencies": {
"@pikoman/ws": "1.0.0-rc.10"
}
}websockets==15.0.1
cryptography==46.0.3Run it
# unpack moderator-bot.zip, then in the folder:
npm install
cp .env.example .env # fill in PIKOMAN_APP and PIKOMAN_API_KEY
npm startpython -m venv .venv && . .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
python moderator.pyIdeas to take it further
- Warn for links, capitals or floods instead of words: the bot sees every message.
- Let your human moderators kick too: a
!kick Bocommand, from members whose signed data says they are moderators. - Filter before the room sees it: send messages to your backend, check them, and publish them from there.