Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

公開日 : 2017-11-16

Python MattermostからAPIをたたいて投稿する方法


# やりたいこと

- SlackのCloneアプリ、MattermostからPythonで投稿したい
- Pythonでスクリプトを実行した結果を、matterpostの特定のChannelからポスト

# 準備する情報

- mattermostのサーバーURL:例)mattermost.hogehoge.com
- mattermostで投稿したいChannelのID
- 自分のログインID
- 自分のパスワード

# 手順

- python-mattermost-driverをインストールする

$ sudo pip install mattermostdriver


# ソースコード

#1 最初にmattermostのAPIを扱えるLibraryのインポートをする

#2 投稿するtextを引数に、mattermost上にtextを投稿するメソッド

#2-1 ログイン情報をJSON形式で入れる。ここは自分の使う環境によって、URLやらログインIDやらを代入する。

#2-2 投稿するチャンネルを指定して、投稿する


#1 mattermostのライブラリーをimport

from mattermostdriver import Driver
#2 投稿するtextを引数に、Mattermost上に投稿するメソッド

def postMattermost(text):

    foo = Driver({
      # Required options

    #2-1 ログイン情報 

    'url': '',
    'login_id': '',
    'password': '',
    'scheme': 'http',

    #2-2 特定のチャンネルに投稿


      'message': text
      #'file_ids': [file_id]


from mattermostdriver import Driver

foo = Driver({
    # Required options
    'url': 'mattermost.server.com',
    'login_id': 'user.name',
    'password': 'verySecret',
    # Instead of login/password you can also use a personal access token
    'token': 'YourPersonalAccessToken',
    # Optional / defaults to
    'scheme': 'https',
    'port': 8065,
    'basepath': '/api/v4',
    # Use False if self signed/insecure certificate
    'verify': True,
    # The interval the websocket will ping the server to keep the connection alive
    'timeout': 30,
    'mfa_token': 'YourMFAToken'

# Most of the requests need you to be logged in, so calling login()
# should be the first thing you do after you created your Driver instance.
# login() returns the raw response
# If using a personal access token, you still need to run login().
# In this case, does not make a login request, but a `get_user('me')`
# and sets everything up in the client.

# You can make api calls by using api['yourendpointofchoice'].
# Since v4.0.0 you can now also call the endpoint directly.
# So, for example, wherever you see `Driver.api['users'].get_user('me')`,
# you can just do `Driver.users.get_user('me')`.
# The names of the endpoints and requests are almost identical to
# the names on the api.mattermost.com/v4 page.
# API calls always return the json the server send as a response.

# If the api request needs additional parameters
# you can pass them to the function in the following way:
# - Path parameters are always simple parameters you pass to the function

# - Query parameters are always passed by passing a `params` dict to the function

# - Request Bodies are always passed by passing an `options` dict or array to the function

# See the mattermost api documentation to see which parameters you need to pass.
    'team_id': 'some_team_id',
    'name': 'awesome-channel',
    'display_name': 'awesome channel',
    'type': 'O'

# If you want to make a websocket connection to the mattermost server
# you can call the init_websocket method, passing an event_handler.
# Every Websocket event send by mattermost will be send to that event_handler.
# See the API documentation for which events are available.

# To upload a file you will need to pass a `files` dictionary
channel_id = foo.api['channels'].get_channel_by_name_and_team_name('team', 'channel')['id']
file_id = foo.api['files'].upload_file(
            files={'files': (filename, open(filename))})['file_infos'][0]['id']

# track the file id and pass it in `create_post` options, to attach the file
    'channel_id': channel_id,
    'message': 'This is the important file',
    'file_ids': [file_id]})
# If needed, you can make custom requests by calling `make_request`
foo.client.make_request('post', '/endpoint', options=None, params=None, data=None, files=None, basepath=None)
# If you want to call a webhook/execute it use the `call_webhook` method.
# This method does not exist on the mattermost api AFAIK, I added it for ease of use.
foo.api['hooks'].call_webhook('myHookId', options) # Options are optional