DEV Community

William Rodriguez
William Rodriguez

Posted on

CLI en el chat: Enrutamiento declarativo de comandos con @on_command.

Procesar comandos de bot no debería implicar recortes de strings ni gestión manual de hilos. wconnect ofrece manejadores declarativos que se ejecutan automáticamente en pools de trabajadores.

Así se implementa Declarative Command Routing en entornos reales con 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

Por qué es clave:

  • Decoradores sin boilerplate (@bot.on_command, @bot.on_message, @bot.consumer).
  • Streaming de archivos binarios directo desde RAM usando WFile.
  • Poller daemon no bloqueante con run_consumers(block=False).

¡Visita el repositorio en GitHub!

Top comments (0)