"""Send a direct message from the Mattermost bot to the human. usage: mattermost-dm [] Sends , or else stdin, from the bot to the recipient that ~/.claude/.env names. That file holds three KEY=value lines, and only its owner may read it, since it holds the bot's token: MATTERMOST_URL=https://chat.example MATTERMOST_TOKEN= MATTERMOST_RECIPIENT= Exit 0 once the message is sent. Exit 1, saying why, when Mattermost cannot be reached or refuses the message. Exit 2, saying why, on misuse: bad arguments, an empty message, or a ~/.claude/.env that is missing or unreadable, lacks a key, names a server off https, or that others may read. """ import typing from mattermost_dm.dm import Config, Http, send from failure import Failure USAGE = "usage: mattermost-dm []" def main(argv: list[str], stdin: typing.TextIO, config: typing.Callable[[], Config | Failure], http: Http, stderr: typing.TextIO) -> int: if argv[:1] in (["-h"], ["--help"]): print(__doc__.strip()) return 0 if len(argv) > 1: print(f"mattermost-dm: takes one message, or none to read stdin; got {len(argv)} arguments\n{USAGE}", file=stderr) return 2 message = (argv[0] if argv else stdin.read()).strip() if not message: print("mattermost-dm: the message is empty", file=stderr) return 2 found = config() if isinstance(found, Failure): print(f"mattermost-dm: {found.reason}", file=stderr) return 2 if failed := send(found, message, http): print(f"mattermost-dm: {failed.reason}", file=stderr) return 1 return 0