Mattermost インストール手順および API の利用例
[履歴] [最終更新] (2019/08/24 01:52:43)

概要

Mattermost においても Slack のような API が提供されています。動作確認のためのインストール手順および API の利用例を記載します。

インストール手順

CentOS6 の場合は以下のようになります。

Database のインストール

古いバージョンの MySQL では Mattermost が利用する UTF8MB4 に対応していません。例えば MySQL 5.7 をインストールする場合は RPM が利用できます。DB とユーザを作成します。

CREATE USER 'mmuser'@'%' IDENTIFIED BY 'mmuser_password';
CREATE USER 'mmuser'@'localhost' IDENTIFIED BY 'mmuser_password';
CREATE DATABASE mattermost;
GRANT ALL PRIVILEGES ON mattermost.* TO 'mmuser'@'%';
GRANT ALL PRIVILEGES ON mattermost.* TO 'mmuser'@'localhost';

Mattermost ダウンロードおよびインストール

こちらのページからダウンロードして解凍およびインストールします。

wget https://releases.mattermost.com/5.0.0/mattermost-5.0.0-linux-amd64.tar.gz
tar zxvf mattermost-5.0.0-linux-amd64.tar.gz
sudo mv mattermost /opt
sudo mkdir /opt/mattermost/data
sudo useradd --system --user-group mattermost
sudo chown -R mattermost:mattermost /opt/mattermost
sudo chmod -R g+w /opt/mattermost

設定ファイルを更新して MySQL 認証情報を設定します。

sudo vim /opt/mattermost/config/config.json

設定例

"DataSource": "mmuser:mmuser_password@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s",

外部から利用する場合は iptables を停止しておきます。

sudo service iptables stop

動作確認

cd /opt/mattermost/
sudo -u mattermost ./bin/mattermost
curl http://localhost:8065/

Uploaded Image

API の利用

Slack と同様に Incoming Webhooks 等の Integration および API が利用できます。

Incoming Webhooks

Main Menu → Integrations から Incoming Webhooks を選択して、エンドポイントを新規作成します。HTTP POST するとメッセージを投稿できます。

curl -XPOST -H 'Content-Type: application/json' -d '{"text": "Hello, this is some text\nThis is more text. :tada:"}' http://192.168.56.10:8065/hooks/63mt7dzfmtdn7cegjs35mumxkw

Web API

何らかの認証を利用して、各種 API が利用できます。Personal Access Tokens が利用できる設定になっている Mattermost の場合は、以下のようにして自分の情報を取得できます。

$ curl -H 'Authorization: Bearer eib158e47i81xe4dpzg346qqqr' http://192.168.56.10:8065/api/v4/users/me | jq .
{
  "id": "h9n3rrbuzbnt3cmu333o3h1www",
  "create_at": 1529463111662,
  "update_at": 1529463742269,
  "delete_at": 0,
  "username": "myuser",
  "auth_data": "",
  "auth_service": "",
  "email": "myuser@example.com",
  "nickname": "",
  "first_name": "",
  "last_name": "",
  "position": "",
  "roles": "system_admin system_user",
  "allow_marketing": true,
  "notify_props": {
    "channel": "true",
    "comments": "never",
    "desktop": "mention",
    "desktop_sound": "true",
    "email": "true",
    "first_name": "false",
    "mention_keys": "myuser,@myuser",
    "push": "mention",
    "push_status": "away"
  },
  "last_password_update": 1529463111662,
  "locale": "en",
  "timezone": {
    "automaticTimezone": "",
    "manualTimezone": "",
    "useAutomaticTimezone": "true"
  }
}

サーバに HTTP POST するボタン

Interactive Messages を利用します。

Uploaded Image

curl -XPOST -H 'Content-Type: application/json' -d '{
  "attachments": [
    {
      "pretext": "This is the attachment pretext.",
      "text": "This is the attachment text.",
      "actions": [
        {
          "name": "Ephemeral Message",
          "integration": {
            "url": "http://www.example.com/hello",
            "context": {
              "action": "do_something_ephemeral"
            }
          }
        }, {
          "name": "Update",
          "integration": {
            "url": "http://www.example.com/hello2",
            "context": {
              "action": "do_something_update"
            }
          }
        }
      ]
    }
  ]
}' https://mattermost.example.com/hooks/xxxxxxxxxxxxx

サーバには以下のような JSON が HTTP POST されます。レスポンスまたは別途 Incoming Webhooks 等で処理結果を Mattermost に反映させます。

{
  "user_id": "xxxxxxxxxxxxx",
  "channel_id": "xxxxxxxxxxxxx",
  "team_id": "xxxxxxxxxxxxx",
  "post_id": "xxxxxxxxxxxxx",
  "type": "",
  "data_source": "",
  "context": {
    "action": "do_something_ephemeral"
  }
}

サーバに HTTP POST/GET するコマンド

Slack と同様に Slash Commands が利用できます。HTTP GET または POST でサーバにリクエストを送れます。確かに Mattermost からのリクエストであることをサーバ側で確認するために Token が含まれたリクエストとなります。事前に Mattermost から取得してサーバに設定しておきます。以下は HTTP POST の例です。

Host: www.example.com
User-Agent: Go-http-client/1.1
Content-Length: 338
Accept: application/json
Authorization: Token xxxxxxxxxxxxxxxxxxxxxxxxxx
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip

channel_id=yyyyyyyyyyyyyyyyyy
&channel_name=yyyyyyyyyyyyyyyyyy
&command=%2Fyyyyyyyyyyyyyyyyyy
&response_url=https%3A%2F%2Fmattermost.example.com%2Fhooks%2Fcommands%2Fyyyyyyyyyyyyyyyyyy
&team_domain=yyyyyyyyyyyyyyyyyy
&team_id=yyyyyyyyyyyyyyyyyy
&text=
&token=xxxxxxxxxxxxxxxxxxxxxxxxxx
&user_id=yyyyyyyyyyyyyyyyyy
&user_name=yyyyyyyyyyyyyyyyyy
関連ページ