46 lines
1.6 KiB
Python
46 lines
1.6 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 ~/.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=<the bot's access token>
|
|
MATTERMOST_RECIPIENT=<the recipient's username>
|
|
|
|
Where that file exists, loop-claude sends the human a DM when it asks a question, a release is
|
|
ready to cut, or the loop stops with exit 1.
|
|
|
|
Exit 0 once sent, 1, saying why, when the send failed, and 2 on misuse: bad arguments, an empty
|
|
message, or a ~/.claude/.env that is missing, 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 [<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
|