In my project, instead of a daily report, I post YWT (what I did, what I learned, what I do next) to Mattermost as a retrospective and share it among the members. It's been more than half a year since I started writing daily reports, and I wanted to put together my own posts, so I tried to output a file using the Mattermost API.
Operation check environment | version |
---|---|
Windows10 Home Edition | Version 2004 |
Docker for Windows | 2.4.0.0 |
GitLab | 13.4.3 |
GitLab Mattermost | 5.26.2 |
Python | 3.8 |
MattermostDriver | 6.3 |
GitLab Mattermost uses the one built last time (https://qiita.com/shimi58/items/0e9b81f2eac8993be71b).
I am registered on GitHub.
Install mattermost driver --Run with powerShell or command prompt
```cmd
pip install mattermostdriver
```
Open config.ini and specify the connection destination, login information, and search conditions.
[CONNECTION]
#Specify the URL of Mattermost
scheme = http
url = localhost
port = 9081
#Specify the user to sign in with the API
login_id = test
password = Password1!
[SEARCH]
#Specify the team name to be searched
team_name = sample
#Specify search conditions
tearms = #XXXXXXX
[OUTPUT]
path = ./mattermostPost.txt
Run mattermost.py
python mattermost.py
It is a sauce coat.
from mattermostdriver import Driver
import configparser
#Read definition
config_ini = configparser.ConfigParser()
config_ini.read('config.ini', encoding='utf-8')
#Mattermost login
connection = Driver({
'url': config_ini['CONNECTION']['url'],
'login_id': config_ini['CONNECTION']['login_id'],
'password': config_ini['CONNECTION']['password'],
'scheme': config_ini['CONNECTION']['scheme'],
'basepath': '/api/v4',
'verify': True,
'port': int(config_ini['CONNECTION']['port']),
})
connection.login()
#Get Team ID from Team Name
teamId = connection.api['teams'].get_team_by_name(config_ini['SEARCH']['team_name'])['id']
#Get posts that match your search criteria
postMessage = connection.api['posts'].search_for_team_posts(teamId,options={
'terms': config_ini['SEARCH']['tearms']
})
#File open
with open(config_ini['OUTPUT']['path'], 'w') as f:
#Output search results
for post in postMessage['posts'].values():
print(post['message'], file=f)
#Delimiter
print('====================', file=f)
-Official Mattermost Driver documentation The connection method is written here, and I was able to connect almost exactly as it was. In addition, information on the arguments of each API is also written in End Points. -Official documentation for the Mattermost API It describes what kind of operations can be performed with the API. (Of course, of course ...)
The Mattermost API has abundant APIs that can be used, and the documentation is thick, so I could make it without any clogging. Since the search for Mattermost itself is not good, I wondered if it would be easier to operate if the post extracted later was made a rule with a hashtag. Next time, I would like to make a bot.
Recommended Posts