Mattermost

People talk to kōjō in Mattermost by direct message or by mentioning the bot in a channel. kōjō answers in the thread and asks for approvals with buttons. It signs in as a bot account of your Mattermost server and uses the server’s API v4.

See Surfaces for how Mattermost compares with the other chat platforms, and Who may trigger what for what a request may do, depending on who sent it.

How it connects

Direction How Needs
Messages to kōjō kōjō opens a WebSocket to the server (<url>/api/v4/websocket) with the bot’s token and reads the posted events. kōjō reaches the Mattermost server. No inbound route.
Replies and approvals from kōjō Calls to the server’s REST API with the bot’s token. The same.
Clicks on approval buttons The Mattermost server posts to <public_url>/mattermost/actions. The Mattermost server reaches kōjō’s server.public_url.

When the WebSocket drops, kōjō connects again after a pause that grows from 1 second to 1 minute, and asks the server to resume the same connection. The server then sends the events of the gap, for as long as it keeps the connection’s queue. After a longer outage, or a restart of the Mattermost server, a new connection starts and the messages sent in between are not delivered: send them again. A connection that stays silent for 90 seconds is taken for dead (the server pings every 60).

kojo serve stops with an error when the server refuses the token (HTTP 401): no retry fixes that. While the server is merely unreachable it keeps trying.

Set up

  1. Turn bot accounts on: System Console → Integrations → Bot Accounts → Enable Bot Account Creation.

  2. Create the bot account and its token. In the product menu choose Integrations → Bot Accounts → Add Bot Account, give it a username (kojo) and the Member role, and copy the token that is shown once. Or with mmctl:

    mmctl bot create kojo --display-name "kōjō"
    mmctl token generate kojo kojo-token

    People mention the bot by its username, so @kojo here.

  3. Add the bot to each team, and to each channel where people should be able to mention it (/invite @kojo in the channel), or:

    mmctl team users add engineering kojo
    mmctl channel users add engineering:builds kojo
  4. Make a secret for the approval buttons:

    openssl rand -hex 32
  5. Add Mattermost to kojo.yaml:

    server:
      public_url: https://kojo.example.com
    
    chat:
      mattermost:
        url: https://chat.example.com
        token: ${MATTERMOST_BOT_TOKEN}
        action_secret: ${MATTERMOST_ACTION_SECRET}
    Key Required Meaning
    url yes The Mattermost server’s URL, with its path when Mattermost is served under one. http or https.
    token yes The bot account’s access token.
    action_secret no Signs the approval buttons. At least 16 characters. Without it kōjō makes one up at every start: buttons posted before a restart then fail, and so do buttons posted by another replica. Set it.
  6. Let the Mattermost server call kōjō. When server.public_url resolves to a private address (the same cluster, the same host, an internal network), Mattermost refuses to call it until the host is listed in System Console → Environment → Developer → Allow untrusted internal connections to (ServiceSettings.AllowedUntrustedInternalConnections), for example kojo.example.com. A click that cannot reach kōjō shows “Action failed to execute” under the buttons.

  7. In the dashboard under Settings → Access, give the operator role to the people who may trigger code changes and decide approvals from Mattermost. kōjō matches a Mattermost user to a kōjō user by the email address of the Mattermost account.

  8. Run kojo doctor. Its chat mattermost check calls GET /api/v4/users/me with the token. It fails when the server refuses the token or when the token is not a bot account’s, and kōjō logs the username and user ID of the bot.

What the bot needs

The bot needs For How
To be a member of the team and the channel Mattermost only sends a bot the posts of channels it is in, and only lets it post there. Step 3 above. Direct messages need no channel.
To read the email address of users Mapping the sender, and whoever clicks a button, onto a kōjō role. Emails are visible to every account when System Console → Site Configuration → Users and Teams → Show Email Address is on (PrivacySettings.ShowEmailAddress, on by default). When it is off, only a bot with the System Admin role reads them.
To post, and to edit its own posts Replies, approvals, and replacing the buttons of an approval that was decided outside a click. The Member role has both.
To open direct message channels Only for a schedule that notifies a person (notify: mattermost:@ana). The Member role has it.

A bot that may not read emails still receives every message and click, without an email. Such a request has no kōjō role: it runs without write access, and a click on an approval is refused.

The token has to be a bot account’s. With a person’s access token every direct message to that person would be a request to kōjō, so kojo doctor fails on one. On a server that limits the lifetime of tokens, replace the token before it expires.

Mentions, messages and replies

Write to the bot in plain words, in a direct message or with @kojo in a channel. Each message becomes one session, unless it goes to a session already at work in its thread (below), and kōjō replies in the thread of that message. A mention inside a thread keeps the conversation in that thread.

@kojo why does the nightly build of github/acme/api fail?

The repository is chosen as in Slack, and what the request may do depends on the sender’s role in kōjō: see who may trigger what.

While a session is at work, an operator’s message in its thread (a mention in a channel, any reply in a direct message) goes to that session instead of starting another one: its next step reads it. See Talking to a session at work.

Message What happens
A direct message to the bot Always a request. No mention needed.
A post in a channel or group message with @kojo A request. The mention is removed from the text.
A post with @channel, @all or @here only Ignored, although Mattermost counts it as a mention of everyone.
@kojo inside a code span or code block Ignored, as in Mattermost itself.
A post by another bot or by a webhook Ignored, so that two bots do not answer each other.
Edits, deletions, system messages (joins, header changes) Ignored.

Messages kōjō writes

kōjō writes Markdown, which Mattermost renders. An answer longer than 16,383 characters continues in further posts of the same thread, cut between paragraphs, with a code block that is cut closed and opened again. Mentions in what kōjō posts do not notify anyone: a @name outside code is written with an invisible character after the @. Its text is largely written by a model that has read issue bodies and files, and neither @channel nor the name of a person should be able to ping people from there.

Approvals

When the merge policy asks a human, kōjō posts a message with Approve and Deny buttons in the thread and parks the session until someone clicks or the question’s deadline passes. The questions, schedule creation included, and what happens without an answer are the ones listed for Slack.

Only a user with the operator or admin role decides. A click from anyone else changes nothing: the buttons stay, kōjō says in the thread that the operator role is needed, and the attempt is written to the audit log. The first valid click stands, and the buttons are replaced by the outcome and the name of who decided.

Mattermost does not sign the request it sends for a click, and /mattermost/actions can be called by anyone who can reach kōjō. So each button carries a context that kōjō signs: an HMAC-SHA256, keyed with action_secret, over the channel, the thread, the approval and the button’s value. Mattermost keeps a button’s URL and context on the server. It removes them from the message before any client sees it, and sends the context back only with a click. kōjō accepts a click only when the signature matches the context and the channel of the request, and answers a click that fails the check with 401.

sequenceDiagram
    participant U as Operator
    participant M as Mattermost server
    participant K as kōjō
    K->>M: post a message with Approve and Deny, each with a signed context
    M-->>U: the message with the buttons, without their URL and context
    U->>M: click Approve
    M->>K: POST /mattermost/actions with the user, the post, the channel and the context
    Note over K: The signature of the context is checked. A request that fails gets 401
    K->>M: GET the user who clicked, for the email
    Note over K: The email has the operator role, so the decision is recorded
    K-->>M: 200 with the message as it is now, the outcome in place of the buttons
    M-->>U: the message without buttons, Approved by Ana
    Note over K: The session continues

The answer to the click carries the resolved message, and the Mattermost server applies it itself. That also works on a server that limits how long a post may be edited (ServiceSettings.PostEditTimeLimit).

What the signature proves is that the request came through the Mattermost server, for a button kōjō made, in the channel kōjō put it in. Who clicked is what the Mattermost server says. Keep action_secret as secret as the token, and serve kōjō’s public URL over https.

Schedules

A schedule can report to Mattermost through its notify:

notify The result goes to
mattermost:9g4zq1irrapnnmjdsiyz9gyode The channel with that ID.
mattermost:engineering/builds The channel builds of the team engineering, by their names.
mattermost:builds The channel builds, when exactly one of the bot’s teams has a channel of that name.
mattermost:@ana A direct message to the user ana.

The mattermost: prefix can be left out when Mattermost is the only chat surface. The bot has to be a member of the channel.

Scroll to move. + - zoom, 0 fit, Esc close.100%