Skip to content

Let a bot open a direct conversation with a person - #50

Merged
ssenerg merged 2 commits into
mainfrom
bot-direct-conversations
Nov 2, 2026
Merged

ssenerg merged 2 commits into
mainfrom
bot-direct-conversations

Conversation

@ssenerg

@ssenerg ssenerg commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Until now a bot could only write in conversations people opened with it. That rules out reminder, alert and notification bots that message one person. This PR lets a bot open a direct conversation itself, following Slack's model rather than Telegram's "the person writes first".

Rules

  • A bot calls ChannelService/OpenDirectChannel with a team and a person. Both must be members of that team. A bot can't open a group conversation (OpenGroupChannel stays off its allowlist, and the directory refuses it with code 3), and it can't open one with another bot (code 3).
  • If the person blocked the bot, the call is refused with code 7, the same refusal as a blocked post (channels.ErrNotTaking). A deactivated or deleted account gets code 5, as for anyone else.
  • Each bot can open up to services.directory.max_bot_conversations_per_hour new conversations an hour (30 by default). Past that the call returns code 8. Reopening a conversation that already exists is free, whoever opened it. Like the existing team and invite caps, the counter lives in the directory's memory.
  • The bot is written as a member that sees everything (Bot, BotSeesAll), so it gets every message of the conversation, as with any direct conversation it's in. The person sees an ordinary direct conversation with the bot's badge. They can mute it, block the bot (which already stops its posts there) or delete it for themselves.

Wiring

  • Added OpenDirectChannel to the bot allowlist (internal/server/drpc/middleware.go).
  • HTTP Bot API: a new openDirect method.
  • Go SDK: Client.OpenDirect(ctx, teamID, userID).
  • examples/echobot: /remindme 10m stand up sends the text back in a direct message when the time is up.
  • docs/protocol.md: the bots' call table, a new "Writing to a person first" section, and openDirect in the HTTP table. README updated too.
  • Repo: DirectKeyHolder reads a conversation key, so the rate limit can let existing conversations through.

A fix on the web side

The web tab only learned that a conversation was direct after its sidebar reloaded. A bot that opens a conversation and posts straight away could therefore beat that reload, and the person got no desktop notification for the first message. The session now marks a direct conversation as soon as ChannelJoined arrives.

Pushes needed no change. The channel passes the person as a member, and push/notify.go already skips people who blocked the author. A new test covers this.

Deploying

There's no migration and no proto change. The new config key has a default in cmd/serve/default.yaml.

Tests

  • directory: each refusal (group, another bot, outside the team, a team the bot isn't in, deactivated, deleted, blocked), the bot's membership flags, and the rate limit (existing conversations are free, each bot has its own allowance, people have none).
  • channels: in a conversation a bot opened, the bot gets every message and can read the history, its post is pushed to the person, and a block stops its posts.
  • push: a bot's direct message is pushed like a person's, but not to someone who blocked the bot.
  • The allowlist test, a Bot API test for openDirect (success, and 429 past the limit), an SDK test, and a web session test for the notification fix.
  • e2e (test/e2e/bots_test.go): the bot opens a conversation with the admin and posts. The admin's sidebar shows it and a notification arrives. The admin's reply reaches the bot. OpenGroupChannel returns code 12. While the admin blocks the bot, openDirect returns 403 and the bot's post returns code 7. After unblocking, openDirect returns the same conversation.

Ran locally: go test -race ./... and golangci-lint v2.15.0 (0 issues).

Not run locally: e2e (left to CI) and the live Scylla test for DirectKeyHolder, which skips without GOCHAT_TEST_SCYLLA. Docker already runs three Scylla nodes here, so I didn't start a throwaway one.

@ssenerg
ssenerg merged commit 7f34773 into main Nov 2, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant