56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
"""Send a message from the notifier's bot to the human.
|
|
|
|
usage: notify [<message>]
|
|
|
|
Sends <message>, or else stdin, through the notifier that a project's config names, from the bot to the
|
|
recipient that the project's env file names. Run it inside the project's checkout, or a worktree of it.
|
|
The project's config, ~/.claude-projects/<key>/config.toml, sets `notifier` to "mattermost" or "slack"
|
|
and `env-file` to a path outside the checkout and the config's folder, such as
|
|
"~/.claude-projects/<key>.env". The env file holds the notifier's KEY=value lines, and only its owner
|
|
may read it, since it holds the bot's token. For mattermost:
|
|
|
|
MATTERMOST_URL=https://chat.example
|
|
MATTERMOST_TOKEN=<the bot's access token>
|
|
MATTERMOST_RECIPIENT=<the recipient's username>
|
|
|
|
For slack, a bot token with the `chat:write` scope, and the recipient's member ID, which Slack shows
|
|
under Profile → ⋮ → Copy member ID:
|
|
|
|
SLACK_TOKEN=<the bot's token, xoxb-…>
|
|
SLACK_RECIPIENT=<the recipient's member ID, U…>
|
|
|
|
Exit 0 once the message is sent.
|
|
Exit 1, saying why, when the notifier cannot be reached or refuses the message.
|
|
Exit 2, saying why, on misuse: bad arguments, an empty message, a directory with no project config,
|
|
a config that sets no `notifier` or no `env-file`, or an env file that is missing or unreadable, lacks
|
|
a key, names a Mattermost server off https, or that others may read. None of these messages holds the
|
|
token.
|
|
"""
|
|
import typing
|
|
|
|
from failure import Failure
|
|
from notifier.notify import Sender
|
|
|
|
USAGE = "usage: notify [<message>]"
|
|
|
|
|
|
def main(argv: list[str], stdin: typing.TextIO, sender: typing.Callable[[], Sender | Failure], stderr: typing.TextIO) -> int:
|
|
if argv[:1] in (["-h"], ["--help"]):
|
|
print(__doc__.strip())
|
|
return 0
|
|
if len(argv) > 1:
|
|
print(f"notify: 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("notify: the message is empty", file=stderr)
|
|
return 2
|
|
found = sender()
|
|
if isinstance(found, Failure):
|
|
print(f"notify: {found.reason}", file=stderr)
|
|
return 2
|
|
if failed := found(message):
|
|
print(f"notify: {failed.reason}", file=stderr)
|
|
return 1
|
|
return 0
|