mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-05-15 12:27:16 +00:00
9584992c83
* simplex-chat-python: split Client from Bot, add request/response API
Client is now the base class for SimpleX participants that talk TO
services (monitors, probes, automated participants). Bot extends Client
with server features (address, auto-accept, welcome, commands).
New methods on Client (inherited by Bot):
connect_to(link) idempotent contact handshake
send_and_wait(id, text) send a message and await the reply
events() async iterator over chat events
@on_message(contact_id=N) filter by sender in decorators
BotProfile renamed to Profile (alias kept). New ContactAlreadyExistsError
subclass for cleaner error handling.
* simplex-chat-python: narrow event payload type per @on_event tag
@client.on_event("contactConnected") now types the handler's event
parameter as CEvt.ContactConnected instead of the unnarrowed
CEvt.ChatEvent union — mirroring how @on_message narrows by
content_type.
The 50 overloads are generated by the Haskell codegen into _events.py
(as a Protocol class), so new events stay in sync automatically.
Client.on_event is exposed as a property typed as that Protocol; the
runtime implementation is unchanged.
SimpleX Chat Python library
Python 3.11+ client for SimpleX Chat bots. Equivalent to the Node.js library.
Install
pip install simplex-chat
The native libsimplex is downloaded lazily on first use. To pre-fetch:
python -m simplex_chat install # sqlite (default)
python -m simplex_chat install --backend postgres # linux-x86_64 only
Quick start
import re
from simplex_chat import Bot, BotProfile, Message, SqliteDb, TextMessage
bot = Bot(
profile=BotProfile(display_name="Squaring bot"),
db=SqliteDb(file_prefix="./squaring_bot"),
welcome="Send me a number, I'll square it.",
)
@bot.on_message(content_type="text", text=re.compile(r"^-?\d+(\.\d+)?$"))
async def square(msg: TextMessage) -> None:
n = float(msg.text or "0")
await msg.reply(f"{n} * {n} = {n * n}")
@bot.on_message(content_type="text")
async def fallback(msg: Message) -> None:
await msg.reply("Send me a number, like 7 or 3.14.")
if __name__ == "__main__":
bot.run()
bot.run() blocks. The connection address is logged on startup — paste it into a SimpleX client to talk to the bot. Ctrl+C to stop.
Three decorators: @bot.on_message(...), @bot.on_command(name), @bot.on_event(tag). Message handlers are first-match-wins in registration order, so register specific filters first and catch-alls last.
See examples/squaring_bot.py for the full example.
Development
uv venv && source .venv/bin/activate
uv pip install -e '.[dev]'
ruff check && pyright && pytest tests/
Wire types under src/simplex_chat/types/_*.py are generated. Regenerate with cabal test simplex-chat-test --test-options='--match Python'.
Release
Manual for now. Bump _version.py:__version__, build a wheel, upload to PyPI:
uv build --wheel
uv publish --token "$PYPI_TOKEN"