Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ Works headless for agents (`--json` prints the `bot_message_id`).
| Command | Description |
|---------|-------------|
| `dailybot chat send` | Send a bot message to users (`-u`), channels (`-c`, a channel id or a qualified `slack:<id>` / `<platform-id>:<id>`), and/or teams (`-t`) |
| `dailybot chat update <bot_message_id>` | Edit a previously sent message (parent **or** a thread reply) |
| `dailybot chat update <bot_message_id>` | Edit a previously sent message (parent **or** a thread reply) by the id a send returned — never an id you make up |
| `dailybot conversation open` | Open (or reuse) a Slack group DM with the bot; optionally post a message (`-m`) |

```bash
Expand Down Expand Up @@ -1157,6 +1157,13 @@ dailybot chat send --payload-json '{"target_channels":["C0"],"messages":[{"messa
interactive mode (`dailybot` with no args) does this for you: it lists people
to pick from. Channels are always given by id.

**New message vs. edit:** `chat send` always posts a new message — Dailybot
creates its id and returns it as `bot_message_id`. Keep that id only to edit the
message later with `chat update <bot_message_id>` (within 72h). Never invent a
`bot_message_id` (also not inside `--payload-json`): it is update-only, and an id
Dailybot did not create is refused (`400 unknown_bot_message_id`). To tag a
message with your own reference, use `--metadata`.

**Threads & editing:** `--thread-message` (repeatable, max 10) posts replies
inside the parent's thread in a single call. Each reply gets its own
`bot_message_id` in the response, so `chat update <id>` edits the parent or any
Expand Down
39 changes: 38 additions & 1 deletion dailybot_cli/commands/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ def _execute_send(
f"{e.detail}\n Your role can only reach teammates, public channels, and teams "
"you belong to. Use an allowed target, or an org API key for org-wide reach."
),
"unknown_bot_message_id": (
"That bot_message_id was not created by Dailybot, so there is no message to "
"update. Use the id a send returned (`dailybot chat send --json`), or send a "
"new message with `dailybot chat send` (no id)."
),
"invalid_thread_responses": (
f"{e.detail}\n Thread replies allow at most 10 items, one level deep, with no "
"targeting of their own (they inherit the parent's recipients)."
Expand Down Expand Up @@ -568,6 +573,25 @@ def _build_extra_buttons(
return extra


SEND_WITH_BOT_MESSAGE_ID_ERROR: str = (
"bot_message_id is update-only: it is the id Dailybot returned for a message "
"it already sent. Leave it out of --payload-json to send a new message, or "
"edit that message with `dailybot chat update <bot_message_id>`."
)


def _payload_names_a_message(payload_json: str) -> bool:
"""Whether a `chat send` raw body tries to name an existing message.

Invalid JSON is left to `_assemble_payload`, which reports it.
"""
try:
raw: Any = json.loads(payload_json)
except json.JSONDecodeError:
return False
return isinstance(raw, dict) and bool(raw.get("bot_message_id"))


def _assemble_payload(
*,
payload_json: str | None,
Expand Down Expand Up @@ -707,7 +731,13 @@ def chat_send(
json_mode: bool,
thread_messages_raw: tuple[str, ...],
) -> None:
"""Send a bot message to users, channels, or teams.
"""Send a new bot message to users, channels, or teams.

Every send posts a new message. Dailybot creates its id and returns it as
bot_message_id (shown as "Message ID"; --json prints it). Keep that id only
to edit the message later with `dailybot chat update <bot_message_id>`
(within 72h). Never invent a bot_message_id: a send carries none, and an
id Dailybot did not create is refused.

\b
dailybot chat send -c C0123456789 -m "Deploy finished 🚀"
Expand Down Expand Up @@ -758,6 +788,9 @@ def chat_send(
"--send-as-user <uuid> explicitly (this needs a login session)."
)
raise SystemExit(1)
if payload_json is not None and _payload_names_a_message(payload_json):
print_error(SEND_WITH_BOT_MESSAGE_ID_ERROR)
raise SystemExit(1)
payload: dict[str, Any] = _assemble_payload(
payload_json=payload_json,
metadata=metadata,
Expand Down Expand Up @@ -823,6 +856,10 @@ def chat_update(
) -> None:
"""Edit a previously sent message by its bot_message_id.

BOT_MESSAGE_ID is the id a send returned (`chat send --json` prints it, as
does each --thread-message reply), never one you choose: an id Dailybot did
not create is refused. Send the message's targets again with the new content.

\b
dailybot chat update <bot_message_id> -c C0123 -m "Status: DONE ✅"

Expand Down
38 changes: 38 additions & 0 deletions tests/chat_commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,44 @@ def test_send_payload_json_passthrough(
sent = client.send_chat_message.call_args[0][0]
assert sent["messages"] == [{"message": "a"}]

@patch("dailybot_cli.commands.chat._resolve_agent_context")
def test_send_refuses_a_bot_message_id_in_the_raw_body(
self, mock_resolve: MagicMock, runner: CliRunner
) -> None:
client = _mock_resolve(mock_resolve)
result = runner.invoke(
cli,
[
"chat",
"send",
"--payload-json",
'{"target_channels":["C0"],"message":"a","bot_message_id":"my-id-1"}',
],
)
assert result.exit_code == 1
assert "update-only" in result.output
assert "chat update" in result.output
client.send_chat_message.assert_not_called()

@patch("dailybot_cli.commands.chat._resolve_agent_context")
def test_update_explains_an_id_dailybot_did_not_create(
self, mock_resolve: MagicMock, runner: CliRunner
) -> None:
client = _mock_resolve(mock_resolve)
client.send_chat_message.side_effect = APIError(
status_code=400,
detail="bot_message_id is only for updating a message Dailybot already sent.",
code="unknown_bot_message_id",
)
result = runner.invoke(cli, ["chat", "update", "my-id-1", "-c", "C0", "-m", "x"])
assert result.exit_code != 0
assert "not created by Dailybot" in result.output

def test_send_help_says_the_id_is_returned_never_invented(self, runner: CliRunner) -> None:
result = runner.invoke(cli, ["chat", "send", "--help"])
assert result.exit_code == 0
assert "Never invent a bot_message_id" in result.output

@patch("dailybot_cli.commands.chat._resolve_agent_context")
def test_send_no_target_fails(self, mock_resolve: MagicMock, runner: CliRunner) -> None:
_mock_resolve(mock_resolve)
Expand Down
Loading