Callback Echo Bot¶
An event-driven echo bot using the callback API. Functionally identical to the Simple Echo Bot but replaces the manual polling loop with on_message and run_forever.
See examples/callback_echo.py in the repository.
Usage¶
How It Works¶
- Creates a
WeiLinkinstance and logs in via QR code. - Registers a handler with
@wl.on_message-- the handler is called for every incoming message. - Calls
wl.run_forever(), which starts a background polling thread and blocks the main thread. - On
Ctrl+CorSIGTERM,run_forever()stops the dispatcher and callswl.close()automatically.
Key Features Demonstrated¶
- Decorator-based handlers --
@wl.on_messageregisters a callback, no manualrecv()loop needed. - Blocking dispatcher --
run_forever()handles polling, signal trapping, and cleanup. - Automatic shutdown --
Ctrl+Ctriggers a graceful stop; notry/finallyboilerplate required.
Polling vs Callback¶
| Aspect | Polling (recv() loop) |
Callback (on_message) |
|---|---|---|
| Control flow | You own the loop | Framework owns the loop |
| Boilerplate | while True + try/finally |
Decorator + run_forever() |
| Best for | Custom scheduling, batching | Simple request/response bots |