mirror of
https://github.com/RichieCahill/dotfiles.git
synced 2026-04-17 13:08:19 -04:00
reworked dispatch
This commit is contained in:
@@ -18,20 +18,69 @@ from python.signal_bot.signal_client import SignalClient
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Command prefix — messages must start with this to be treated as commands.
|
|
||||||
CMD_PREFIX = "!"
|
|
||||||
|
|
||||||
HELP_TEXT = """\
|
HELP_TEXT = (
|
||||||
Available commands:
|
"Available commands:\n"
|
||||||
!inventory <text list> — update van inventory from a text list
|
" !inventory <text list> — update van inventory from a text list\n"
|
||||||
!inventory (+ photo) — update van inventory from a receipt photo
|
" !inventory (+ photo) — update van inventory from a receipt photo\n"
|
||||||
!status — show bot status
|
" !status — show bot status\n"
|
||||||
!help — show this help message
|
" !help — show this help message\n"
|
||||||
|
"Send a receipt photo with the message '!inventory' to scan it.\n"
|
||||||
|
)
|
||||||
|
|
||||||
Send a receipt photo with the message "!inventory" to scan it.\
|
|
||||||
"""
|
|
||||||
|
|
||||||
RECONNECT_DELAY = 5
|
def help_action(
|
||||||
|
signal: SignalClient,
|
||||||
|
message: SignalMessage,
|
||||||
|
_llm: LLMClient,
|
||||||
|
_registry: DeviceRegistry,
|
||||||
|
_inventory_path: Path,
|
||||||
|
_cmd: str,
|
||||||
|
) -> str:
|
||||||
|
"""Return the help text for the bot."""
|
||||||
|
signal.reply(message, HELP_TEXT)
|
||||||
|
|
||||||
|
|
||||||
|
def status_action(
|
||||||
|
signal: SignalClient,
|
||||||
|
message: SignalMessage,
|
||||||
|
llm: LLMClient,
|
||||||
|
registry: DeviceRegistry,
|
||||||
|
_inventory_path: Path,
|
||||||
|
_cmd: str,
|
||||||
|
) -> str:
|
||||||
|
"""Return the status of the bot."""
|
||||||
|
models = llm.list_models()
|
||||||
|
model_list = ", ".join(models[:10])
|
||||||
|
device_count = len(registry.list_devices())
|
||||||
|
signal.reply(
|
||||||
|
message,
|
||||||
|
f"Bot online.\nLLM: {llm.model}\nAvailable models: {model_list}\nKnown devices: {device_count}",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def unknown_action(
|
||||||
|
signal: SignalClient,
|
||||||
|
message: SignalMessage,
|
||||||
|
_llm: LLMClient,
|
||||||
|
_registry: DeviceRegistry,
|
||||||
|
_inventory_path: Path,
|
||||||
|
cmd: str,
|
||||||
|
) -> str:
|
||||||
|
"""Return an error message for an unknown command."""
|
||||||
|
signal.reply(message, f"Unknown command: {cmd}\n\n{HELP_TEXT}")
|
||||||
|
|
||||||
|
|
||||||
|
def inventory_action(
|
||||||
|
signal: SignalClient,
|
||||||
|
message: SignalMessage,
|
||||||
|
llm: LLMClient,
|
||||||
|
_registry: DeviceRegistry,
|
||||||
|
inventory_path: Path,
|
||||||
|
_cmd: str,
|
||||||
|
) -> str:
|
||||||
|
"""Process an inventory update."""
|
||||||
|
handle_inventory_update(message, signal, llm, inventory_path)
|
||||||
|
|
||||||
|
|
||||||
def dispatch(
|
def dispatch(
|
||||||
@@ -40,44 +89,31 @@ def dispatch(
|
|||||||
llm: LLMClient,
|
llm: LLMClient,
|
||||||
registry: DeviceRegistry,
|
registry: DeviceRegistry,
|
||||||
inventory_path: Path,
|
inventory_path: Path,
|
||||||
|
config: BotConfig,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Route an incoming message to the right command handler."""
|
"""Route an incoming message to the right command handler."""
|
||||||
source = message.source
|
source = message.source
|
||||||
|
prefix = config.cmd_prefix
|
||||||
if registry.is_blocked(source):
|
|
||||||
return
|
|
||||||
|
|
||||||
if not registry.is_verified(source):
|
if not registry.is_verified(source):
|
||||||
signal.reply(
|
logger.info(f"Device {source} not verified, ignoring message")
|
||||||
message,
|
|
||||||
"Your device is not verified. Ask the admin to verify your safety number over SSH.",
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
text = message.message.strip()
|
text = message.message.strip()
|
||||||
|
|
||||||
if not text.startswith(CMD_PREFIX) and not message.attachments:
|
if not text.startswith(prefix) and not message.attachments:
|
||||||
return
|
return
|
||||||
|
|
||||||
cmd = text.lstrip(CMD_PREFIX).split()[0].lower() if text.startswith(CMD_PREFIX) else ""
|
cmd = text.lstrip(prefix).split()[0].lower() if text.startswith(prefix) else ""
|
||||||
|
|
||||||
if cmd == "help":
|
commands = {
|
||||||
signal.reply(message, HELP_TEXT)
|
"help": help_action,
|
||||||
|
"status": status_action,
|
||||||
|
"inventory": inventory_action,
|
||||||
|
}
|
||||||
|
|
||||||
elif cmd == "status":
|
action = commands.get(cmd, unknown_action)
|
||||||
models = llm.list_models()
|
action(signal, message, llm, registry, inventory_path, cmd)
|
||||||
model_list = ", ".join(models[:10])
|
|
||||||
device_count = len(registry.list_devices())
|
|
||||||
signal.reply(
|
|
||||||
message,
|
|
||||||
f"Bot online.\nLLM: {llm.model}\nAvailable models: {model_list}\nKnown devices: {device_count}",
|
|
||||||
)
|
|
||||||
|
|
||||||
elif cmd == "inventory" or (message.attachments and not text.startswith(CMD_PREFIX)):
|
|
||||||
handle_inventory_update(message, signal, llm, inventory_path)
|
|
||||||
|
|
||||||
else:
|
|
||||||
signal.reply(message, f"Unknown command: {cmd}\n\n{HELP_TEXT}")
|
|
||||||
|
|
||||||
|
|
||||||
def run_loop(
|
def run_loop(
|
||||||
|
|||||||
Reference in New Issue
Block a user