LoginSignup
4
4

More than 3 years have passed since last update.

mattermostでBOTから複数のチームに通知を一斉送信する

Posted at

はじめに

どうも!生産技術部のエンジニアです。Mattermostサーバを立ち上げ、現在、50人以上のユーザが利用しており、それぞれのチームを作り活動しています。管理者からすべてのチームに、メンテナンス等の通知を送るため、BOTアカウントとシェルスクリプトを利用しました。スクリプトから通知を一斉送信する方法を紹介します。
notification.PNG

環境

$ mattermost-cli version
Version: 5.14.0
Build Number: 5.14.2
Build Date: Fri Aug 30 20:20:48 UTC 2019
Build Hash: 817ee89711bf26d33f840ce7f59fba14da1ed168
Build Enterprise Ready: false
DB Version: 5.14.0

jqコマンドによりjsonファイルからデータ抽出を実施するため、jqコマンドを導入する必要があります。

実施方法

  • 送信用のスクリプト(mattermost_admin_notification.sh)
  • 通知設定ファイル(notification_setting.json)
  • 送信メッセージ設定ファイル(message.json)

上記3つのファイルを同じ階層に配置し、スクリプトを実行する事で送信できます。

# 初回のみchmodを実施し、ファイルのアクセス権を変更する
$ chmod 755 mattermost_admin_notification.sh
# 通知を一斉送信する
$ ./mattermost_admin_notification.sh

送信用のスクリプト

jsonファイルをjqコマンドで展開し、curlコマンドで送信します。

mattermost_admin_notification.sh
#!/bin/bash

bot_access_token=$(cat notification_setting.json | jq -r '.["bot-access-token"]')
mattermost_url=$(cat notification_setting.json | jq -r '.["mattermost-url"]')
teams=$(cat notification_setting.json | jq '.teams')
# team数を取得
len=$(echo $teams | jq length)
# team毎のchannel-idを取得し、curlで送信
for i in $( seq 0 $(($len - 1)) ); do
    channel=$(echo $teams | jq .[$i])
    channel_id=$(echo $channel | jq '.["channel-id"]')
    message=$(cat message.json | jq '.channel_id|='$channel_id)
    curl --noproxy 10.75.94.188 -i -X POST -H 'Content-Type: application/json' -d "$message" -H 'Authorization: Bearer '"$bot_access_token" http://$mattermost_url/api/v4/posts
done

通知設定ファイル

<bot-access-token>にはボット生成時のトークンを記入します。<channel-id>は、MattermostのQ&Aを参考に取得しました。

notification_setting.json
{
    "bot-access-token":"<bot-access-token>",
    "mattermost-url":"10.75.94.188:8001",
    "teams":[
        {
            "name":"Tomoyuki",
            "channel":"off-topic",
            "channel-id":"<channel-id>"
        },
        {
            "name":"Elec",
            "channel":"off-topic",
            "channel-id":"<channel-id>"
        }

    ]    
}

送信メッセージ設定ファイル

Mattermost#message-attachmentsを参考に作成しました。

message.json
{
    "channel_id":"",
    "message":"This is a message from a admin_notification_bot",
    "props":
    {
        "attachments": 
        [
            {
                "color": "#FF8000",
                "pretext": "管理者からのお知らせです。",
                "text": "@here 今週末にサーバを一時的に停止します。停止中はサービスをご利用できません。",
                "author_name": "Tomoyuki Sugiyama",
                "fields": [
                    {
                      "short":false,
                      "title":"目的",
                      "value":"サーバが正常な状態を維持できるように、定期的にシステムアップデート・セキュリティアップデートを実施します。"
                    },
                    {
                      "short":true,
                      "title":"停止日",
                      "value":"5/1 17:00"
                    },
                    {
                      "short":true,
                      "title":"再稼働日",
                      "value":"5/11 9:00"
                    },
                    {
                    "short":false,
                    "title":"その他",
                    "value":"分からないことがあれば@tomoyukiまでダイレクトメッセージを送ってください。"
                    }
                  ]

            }
        ]
    }
}
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4