From 55e1ceb895053024bd169d8356e9ba82be9be78c Mon Sep 17 00:00:00 2001 From: Mauricio Morales <99351019+mauriciomdb@users.noreply.github.com> Date: Thu, 3 Dec 2026 18:07:12 +0000 Subject: [PATCH] fix(chat): bot_message_id is update-only; never invent one `chat send` help and the README now say every send posts a new message, Dailybot creates and returns its bot_message_id, and that id is kept only for `chat update` (72h). A `--payload-json` body carrying bot_message_id is refused locally with a pointer to `chat update`, and the API's new `400 unknown_bot_message_id` gets a plain explanation. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AY6kxNxkCZG2pPJSh1Bxer --- README.md | 9 +++++++- dailybot_cli/commands/chat.py | 39 ++++++++++++++++++++++++++++++++++- tests/chat_commands_test.py | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 335d4cc..65d586f 100644 --- a/README.md +++ b/README.md @@ -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:` / `:`), and/or teams (`-t`) | -| `dailybot chat update ` | Edit a previously sent message (parent **or** a thread reply) | +| `dailybot chat update ` | 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 @@ -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 ` (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 ` edits the parent or any diff --git a/dailybot_cli/commands/chat.py b/dailybot_cli/commands/chat.py index 69c1949..e6b1b6a 100644 --- a/dailybot_cli/commands/chat.py +++ b/dailybot_cli/commands/chat.py @@ -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)." @@ -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 `." +) + + +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, @@ -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 ` + (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 🚀" @@ -758,6 +788,9 @@ def chat_send( "--send-as-user 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, @@ -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 -c C0123 -m "Status: DONE ✅" diff --git a/tests/chat_commands_test.py b/tests/chat_commands_test.py index 951b176..fa27d22 100644 --- a/tests/chat_commands_test.py +++ b/tests/chat_commands_test.py @@ -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)