47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
"""Send a direct message from the Mattermost bot to the human.
|
|
|
|
usage: mattermost-dm [<message>]
|
|
|
|
Sends <message>, or else stdin, from the bot to the recipient that the project's env file names. The
|
|
project's config, ~/.claude-projects/<key>/config.toml, names that file in `env-file`, a path outside
|
|
the checkout and the config's folder, such as "~/.claude-projects/<key>.env". It 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=<the bot's access token>
|
|
MATTERMOST_RECIPIENT=<the recipient's username>
|
|
|
|
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, a config that names no env file, or
|
|
an env file that is missing or unreadable, lacks a key, names a server off https, or that others may read.
|
|
No message, failure or log line holds the token.
|
|
"""
|
|
import typing
|
|
|
|
from mattermost_dm.dm import Config, Http, send
|
|
from failure import Failure
|
|
|
|
USAGE = "usage: mattermost-dm [<message>]"
|
|
|
|
|
|
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
|