Skip to content

Quick Start

Build and run a minimal echo client, then expand it safely.

Expected outcome

You should receive incoming text and reply with an echo message in the same chat.

Minimal Client

import asyncio

from tryx.backend import SqliteBackend
from tryx.client import Tryx, TryxClient
from tryx.events import EvMessage
from tryx.waproto.whatsapp_pb2 import Message

backend = SqliteBackend("whatsapp.db")
app = Tryx(backend)


@app.on(EvMessage)
async def on_message(client: TryxClient, event: EvMessage) -> None:
    text = event.data.get_text() or "<non-text>"
    chat = event.data.message_info.source.chat
    await client.send_message(chat, Message(conversation=f"Echo: {text}"))


async def main() -> None:
    await app.run()


if __name__ == "__main__":
    asyncio.run(main())

How It Works

  1. backend persists pairing/session state
  2. Tryx runtime wires event dispatcher
  3. @app.on(EvMessage) registers handler
  4. TryxClient executes namespace/root API calls

Runtime Flow

  1. Create backend storage.
  2. Create Tryx client instance.
  3. Register handlers with @app.on(EventClass).
  4. Start runtime with await app.run().
  5. Use TryxClient inside handlers for API calls.

First Production Hardening

  • deduplicate with message id
  • bound retries for network operations
  • validate command input
  • keep admin-only commands restricted
  • keep handlers short
  • offload heavy work to worker queue

Blocking Script Mode

For quick scripts without manual event loop management:

from tryx.backend import SqliteBackend
from tryx.client import Tryx

app = Tryx(SqliteBackend("whatsapp.db"))
app.run_blocking()

Warning

run_blocking() is convenient for small scripts. Prefer explicit async runtime control for larger systems.

Next Steps