Ohne SDK: Protokoll direkt nutzen
Für TypeScript und Python gibt es fertige SDKs: Node.js und Python. Diese Seite ist für andere Sprachen oder wenn du wissen willst, was darunter passiert.
Das Tessera-Protokoll ist JSON über HTTP und WebSocket. Jede Sprache mit einer HTTP-Bibliothek und einem WebSocket-Client kann damit Text-Bots bauen. Die vollständige Beschreibung steht in der Protokoll-Referenz; hier ist der kürzeste Weg.
Der Ablauf
- Anmelden: Beim ersten Start
POST /api/v1/auth/registermit{ "username", "password", "bot_token" }(siehe Bots registrieren), danach immerPOST /api/v1/auth/loginmit{ "username", "password" }. Die Antwort enthält jeweilstokenund die eigene Benutzer-IDuser. - Gateway öffnen: WebSocket auf
wss://<host>/gateway(lokalws://127.0.0.1:8640/gateway). - Der Server schickt
{"op": "hello", "d": {"protocol_version": 1, "heartbeat_interval_ms": 30000}}. - Identify schicken, bevor ein Heartbeat-Intervall vorbei ist:
{"op": "identify", "d": {"protocol_version": 1, "token": "…", "intents": 8}} - Der Server antwortet mit
{"op": "ready", "d": {"user": 6, "snapshot": {…}}}. - Danach kommen Events als
{"op": "dispatch", "d": {"seq": 17, "event": {"type": "message_created", …}}}. - Heartbeat alle
heartbeat_interval_ms:{"op": "heartbeat", "d": {"last_seq": 17}}. Der Server bestätigt mit{"op": "heartbeat_ack"}. Fehlen zwei Intervalle lang Heartbeats, trennt er die Verbindung. - Handeln über die REST-API mit
Authorization: Bearer <token>, z. B.POST /api/v1/channels/{id}/messagesmit{ "content": "pong" }.
Unbekannte op-Werte und Event-Typen ignorierst du einfach; neue kommen hinzu, ohne dass sich die Protokollversion ändert.
Beispiel in Python
Derselbe Ping-Bot wie in Erste Schritte, mit requests und websockets:
"""Ping-Bot in Python: antwortet auf !ping mit pong.
Braucht: pip install requests websockets
Aufruf: TESSERA_URL=http://127.0.0.1:8640 TESSERA_BOT_USER=pingbot \
TESSERA_BOT_PASSWORD="ein langes passwort" TESSERA_BOT_TOKEN=… \
python ping_bot.py
TESSERA_BOT_TOKEN nur beim ersten Start: Damit legt der Bot sein Konto an.
Danach meldet er sich ohne Token an.
"""
import asyncio
import json
import os
import requests
import websockets
URL = os.environ["TESSERA_URL"].rstrip("/")
API = URL + "/api/v1"
GATEWAY = URL.replace("http", "ws", 1) + "/gateway"
MESSAGES = 8 # Intent für Nachrichten
def login():
body = {
"username": os.environ["TESSERA_BOT_USER"],
"password": os.environ["TESSERA_BOT_PASSWORD"],
}
if "TESSERA_BOT_TOKEN" in os.environ:
# Einmalig: Bot-Konto mit dem Token vom Admin anlegen.
body["bot_token"] = os.environ["TESSERA_BOT_TOKEN"]
res = requests.post(API + "/auth/register", json=body)
else:
res = requests.post(API + "/auth/login", json=body)
res.raise_for_status()
auth = res.json()
return auth["token"], auth["user"]
def send_message(token, channel, content):
res = requests.post(
f"{API}/channels/{channel}/messages",
headers={"Authorization": f"Bearer {token}"},
json={"content": content},
)
res.raise_for_status()
async def heartbeat(ws, interval_ms, state):
while True:
await asyncio.sleep(interval_ms / 1000)
await ws.send(json.dumps({"op": "heartbeat", "d": {"last_seq": state["seq"]}}))
async def main():
token, me = login()
async with websockets.connect(GATEWAY) as ws:
hello = json.loads(await ws.recv())
assert hello["op"] == "hello", hello
await ws.send(json.dumps({"op": "identify", "d": {
"protocol_version": 1,
"token": token,
"intents": MESSAGES,
}}))
ready = json.loads(await ws.recv())
assert ready["op"] == "ready", ready
print("verbunden als", me)
state = {"seq": None}
beat = asyncio.create_task(
heartbeat(ws, hello["d"]["heartbeat_interval_ms"], state))
try:
async for raw in ws:
msg = json.loads(raw)
if msg["op"] != "dispatch":
continue
state["seq"] = msg["d"]["seq"]
event = msg["d"]["event"]
if (event["type"] == "message_created"
and event["author"] != me
and event["body"]["kind"] == "plaintext"
and event["body"]["data"].strip() == "!ping"):
await asyncio.to_thread(
send_message, token, event["channel"], "pong")
finally:
beat.cancel()
if __name__ == "__main__":
asyncio.run(main())
pip install requests websockets
# erster Start, mit dem Token aus den Servereinstellungen
TESSERA_URL=http://127.0.0.1:8640 TESSERA_BOT_USER=pingbot \
TESSERA_BOT_PASSWORD="ein langes passwort" TESSERA_BOT_TOKEN=… python ping_bot.py
# danach ohne TESSERA_BOT_TOKEN
Für einen Bot im Dauerbetrieb fehlt hier noch das Neuverbinden nach einem Abbruch; das Rust-Beispiel zeigt, wie es geht.
Sprache
Für Sprachräume musst du MLS (RFC 9420) und SFrame sprechen und das UDP-Paketformat der SFU umsetzen. Das ist aufwendig; nimm dafür das Rust-SDK.