Quick Start¶
Build and run a minimal echo bot, then expand it safely.
Expected outcome
You should receive incoming text and reply with an echo message in the same chat.
Minimal Bot¶
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")
bot = Tryx(backend)
@bot.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 bot.run()
if __name__ == "__main__":
asyncio.run(main())
How It Works¶
- backend persists pairing/session state
Tryxruntime wires event dispatcher@bot.on(EvMessage)registers handlerTryxClientexecutes namespace/root API calls
Runtime Flow¶
- Create backend storage.
- Create
Tryxbot instance. - Register handlers with
@bot.on(EventClass). - Start runtime with
await bot.run(). - Use
TryxClientinside 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
bot = Tryx(SqliteBackend("whatsapp.db"))
bot.run_blocking()
Warning
run_blocking() is convenient for small scripts. Prefer explicit async runtime control for larger systems.
Next Steps¶
- Read Authentication Flow to understand pairing and session persistence.
- Explore Client API Gateway for all namespace methods.
- Review Event Model before building complex logic.
- Continue with Tutorial: Command Bot.