0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonでMattermostにIncoming Webhookを投げる

Last updated at Posted at 2020-05-29

Mattermostのusernameとiconの上書きを許可する設定をし、Pythonを使ってMattermostに投稿しました

セルフホストするMattermostへWebhookを使ってメッセージを投げ込む必要があったので調査しました。
現在携わっているプロジェクトはPythonを多用しているため、Pythonでできないか調べてみました。

Python側の処理

Mattermostへのメッセージのポストは単純にhttp postを投げるだけなので、特段ライブラリなどいらないかなという思いはありましたが
一応調べてみたところ、良さそうなライブラリ mattermostdriverがあったので、そちらを使うことにしました。

使い方は簡単で

  1. installします
% poetry add mattermostdriver
# pipの場合は以下
% pip install mattermostdriver
  1. 以下のような感じで使います
    mattermost_driver = Driver({
        'scheme': 'https',
        'url': 'mattermost.your-domain.com',
        'port': 443,
    })
    webhook_id = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
    mattermost_driver.webhooks.call_webhook(hook_id=webhook_id, options={
        "username": "my-bot",
        "icon_url": "https://www.mattermost.org/wp-content/uploads/2016/04/icon.png",
        "text": "Hello, this is some text\nThis is from python script. :tada:",
    })

これで、pythonから投稿できました

Mattermostの設定

結構つまづきポイントもありました、
なぜか、usernameやicon_urlが指定しても変わってくれなかったのです。

少し調査したところ mattermostのconfigの変更(+リスタート)が必要でした。以下が詳しかったです。

以下の設定を変更して、mattermost serverの再デプロイを実施しました。

/mattermost/config/config.json

        "EnablePostUsernameOverride": true,
        "EnablePostIconOverride": true,

設定変更後、Pythonで作った、scriptで、usernameとiconが変更されていることを確認しました。

同じ内容ですが、こちらからもご覧いただけます。

以上になります。

0
1
1

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?