/ python-bots ← all documentation

Write a bot in Python

The yapbot wrapper speaks the same gateway and REST protocol as the clients, and turns it into commands, events and a handful of coroutines. No protobuf, no socket code, and one package to install.

1. Python bot library

The wrapper is the yapbot package, published on PyPI since 3 December 2026: pip install yapbot is the whole install. It needs Python 3.10 or newer and brings aiohttp and protobuf with it; the generated gateway stubs ship inside the package at yapbot/_proto, so there is no protobuf build step. The source is client/bots/python/yapbot in the repository, and pip install client/bots/python installs the same package from a checkout. Only yapbot is imported, so it does not collide with the unrelated yap project on PyPI.

pip install yapbot                                # Python 3.10+, aiohttp and protobuf

cd client/bots/python                             # to run the example bot
python -m venv .venv && . .venv/bin/activate     # Windows: .venv\Scripts\activate
pip install -e .                                  # yapbot from this checkout
python -m bots.polly

Create the account in the Cheatsheet developer portal first: New Application returns the token once, and the server keeps only its hash. A bot has no password, so that token is the whole credential.

A bot in ten lines

import asyncio
from yapbot import Bot, Context

bot = Bot("https://api.getyap.dev", "yapbot_...", prefix="!")

@bot.command("ping", help="check that the bot is awake")
async def ping(ctx: Context) -> None:
    await ctx.reply("pong")

asyncio.run(bot.run())

run() opens one gateway socket, sends Hello{access_token, protocol_version: 1} as its first frame, pings at half the advertised heartbeat_ms, and reconnects with exponential backoff (1 s, doubling, capped at 60 s) when the network drops. A token the server rejects, revokes or rotates raises BotStopped instead of spinning: rotating it in the portal is the only fix.

Commands

@bot.command("roll", help="roll dice", aliases=("dice",))
async def roll(ctx: Context, spec: str = "1d6") -> None:
    await ctx.reply(spec)

A message starting with the prefix (! by default) has its first word matched against command names and aliases; the rest is split on whitespace into ctx.args. Handlers may be sync or async, run concurrently, and a failing one is logged and answered once with "Something went wrong running that command." rather than closing the socket.

Slash commands

@bot.slash("kiss", description="kiss someone")
async def kiss(ctx: Context, who: str = "") -> None:
    await ctx.reply(f"*kisses {who or ctx.author}*")

Registration publishes the list when the socket reports Ready (PUT /bots/{id}/commands), replacing whatever the bot published before; a failure to publish is logged and not fatal. Names are 1–32 of a-z, 0-9, - or _, descriptions up to 100 characters, at most 25 commands. Picking one in a client only fills the composer with /name  — invoking it is an ordinary message — so the same handler answers both forms, in the desktop app and in the browser client.

@bot.command("roll", help="roll dice")
@bot.slash("roll", description="roll dice")
async def roll(ctx: Context, spec: str = "1d6") -> None:
    ...  # one function, both spellings

Events

@bot.event("message")
async def every_message(message):
    ...  # plain text only; sealed bodies never reach this

@bot.event("ready")
async def on_ready(ready):
    print(bot.username, len(bot.spaces))

ready and message are raised by the library; every other gateway frame arrives under its own name (member_join, channel_create, …). The bot's own messages are skipped, and so is anything beginning with yap1: — a bot holds no encryption key, so sealed bodies are never handed to handlers or commands. Another bot's messages are skipped too, so two bots cannot trade commands; bot.latency_ms is the last Ping/Pong round trip.

What a command gets

On ctxWhat it is
channel_id, space_id, message_idIds as integers. space_id is resolved on demand when the bot was installed after it connected, and is 0 if the channel is not visible.
author, author_idThe sender's name and id.
content, argsThe whole message, and the words after the command name.
await ctx.reply(text)Sends to the same channel through the gateway, so it is ordered with what the bot just read.
await ctx.history(limit=50)GET /channels/{id}/messages for the same channel.

On the bot itself: user_id, username, latency_ms, spaces, channel_space, and rest once run() has started. await bot.send(channel_id, text) sends by gateway, await bot.space_of(channel_id) resolves a channel the bot connected before, and await bot.close() stops the loop.

REST

The same token works on REST, so bot.rest exposes the calls a bot normally needs without protobuf or a socket. Every method takes ids as int or str and raises YapError with the server's own status, code and message:

from yapbot import YapError

try:
    await bot.rest.create_channel(space_id, "logs", category="Team")
except YapError as exc:
    print(exc.status, exc.code, exc.message)   # 403 forbidden:
MethodCall it makes
spaces()GET /spaces
members(space), roles(space)GET /spaces/{id}/members, /roles
create_category(space, name)POST /spaces/{id}/categories — needs MANAGE_CHANNELS
create_channel(space, name, *, kind="text", category="")POST /spaces/{id}/channels — needs MANAGE_CHANNELS
delete_channel(channel)DELETE /channels/{id} — needs MANAGE_CHANNELS
history(channel, limit=50, before=None)GET /channels/{id}/messages
send_message(channel, content)POST /channels/{id}/messages; the gateway path is bot.send()
edit_message(message, content), delete_message(message)PATCH / DELETE /messages/{id}
add_reaction(message, emoji), remove_reaction(message, emoji)PUT / DELETE /messages/{id}/reactions/{emoji}
pins(channel), pin(channel, message), unpin(channel, message)GET /channels/{id}/pins, PUT / DELETE /channels/{id}/pins/{message}
create_invite(space), update_channel(channel, name=, category=)POST /spaces/{id}/invites; PATCH /channels/{id}
ban(space, user, reason=""), unban(space, user), kick(space, user), bans(space)PUT / DELETE /spaces/{id}/bans/{user}, DELETE /spaces/{id}/members/{user}, GET /spaces/{id}/bans
update_role(role, name=, color=, permissions=), delete_role(role), set_member_roles(space, user, roles)PATCH / DELETE /roles/{id}, PUT /spaces/{id}/members/{user}/roles
commands(space)GET /spaces/{id}/commands — every bot's commands in that space
set_commands(bot_id, commands)PUT /bots/{id}/commands; what @bot.slash publishes for you

Messages are capped at 4,000 characters and one gateway frame at 64 KiB; send() raises rather than letting the server refuse it. Bots read and send by the same permission bits as anyone else — inside a space they are ordinary members — and account-level actions (spaces, DMs, friends, profiles, API keys, encryption keys) belong to people and answer 403. Self-hosting is the exception to the portal: on your own server, create the bot with POST /api/v1/bots using your session token.

The complete reference — every class, decorator, event and REST method with its signature — is on yapbot API reference.

Sources: client/bots/python (library, bots/polly.py example and README.md); the release is yapbot on PyPI.

Updated . Yap technical documentation is part of the documentation index; the product itself is at getyap.dev.