DEV Community

William Rodriguez
William Rodriguez

Posted on

Clean CLI-in-chat: Declarative bot command routing with @on_command.

Handling bot commands shouldn't require messy string slicing and manual thread management. wconnect provides clean, declarative command handlers that automatically execute in worker pools.

Here is how you implement Declarative Command Routing in production with wconnect:

from wconnect import Wtelegram, WMessage

bot = Wtelegram()

@bot.on_command(command="start")
def cmd_start(msg: WMessage) -> None:
    bot.send(to=msg.chat_id, message=f"Welcome {msg.username}! System ready.")

@bot.on_command(command="status")
def cmd_status(msg: WMessage) -> None:
    # Runs in thread pool executor; won't block other updates
    bot.send(to=msg.chat_id, message="Node: master-01 | CPU: 12% | RAM: 4.2GB")

bot.run_consumers(block=True)
Enter fullscreen mode Exit fullscreen mode

Why This Matters:

  • Zero boilerplate decorators (@bot.on_command, @bot.on_message, @bot.consumer).
  • Stream binary files directly from RAM using WFile.
  • Non-blocking daemon poller with run_consumers(block=False).

Explore the repository on GitHub!

Author: William Steve Rodríguez Villamizar (Wisrovi)

Top comments (0)