Output search results of posts to a file using Mattermost API

Output search results of posts to a file using Mattermost API

Introduction

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.

Tools to use

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).

Things that move

I am registered on GitHub.

How to use

  1. Install mattermost driver --Run with powerShell or command prompt

    ```cmd
    pip install mattermostdriver
    ```
    
  2. 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
    
  3. Run mattermost.py

    python mattermost.py
    

What I made

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)

Referenced

-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 ...)

at the end

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

Output search results of posts to a file using Mattermost API
A story about a Python beginner trying to get Google search results using the API
Output product information to csv using Rakuten product search API [Python]
Process Splunk execution results using Python and save to a file
How to output the output result of the Linux man command to a file
Save an array of numpy to a wav file using the wave module
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
Output the output result of sklearn.metrics.classification_report as a CSV file
Created a tool to output a sequence diagram from a packet capture file of multiple nodes
Output a binary dump in binary and revert to a binary file
I tried to automate the construction of a hands-on environment using IBM Cloud's SoftLayer API
I tried to search videos using Youtube Data API (beginner)
Change the standard output destination to a file in Python
Collect large numbers of images using Bing's image search API
[Python] How to output a pandas table to an excel file
The story of creating a database using the Google Analytics API
[End of 2020] A memo to start using AWS CLI (Version 2)
Upload a file to Dropbox
A memorandum of using eigen3
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
How to paste a CSV file into an Excel file using Pandas
I tried to get a database of horse racing using Pandas
I tried to make a regular expression of "amount" using Python
Conditional branch due to the existence of a shell script file
I tried to make a regular expression of "time" using Python
Correspondence analysis of sentences with COTOHA API and save to file
Convert a large number of PDF files to text files using pdfminer
I tried to get a list of AMI Names using Boto3
How to save only a part of a long video using OpenCV
[Python] Conversation using OpenJTalk and Talk API (up to voice output)
How to create a CSV dummy file containing Japanese using Faker
DataFrame of pandas From creating a DataFrame from two lists to writing a file
Bad post for using "animeface-2009" in Python & Implementation of function to output to PASCAL VOC format XML file
Output to csv file with Python
Output cell to file with Colaboratory
How to use bing search api
Create a CRUD API using FastAPI
How to create a config file
[Django 2.2] Add a New badge to new posts with a date using a template filter
Things to be aware of when building a recommender system using Item2Vec
How to put a line number at the beginning of a CSV file
[Python] How to read a csv file (read_csv method of pandas module)
How to get a sample report from a hash value using VirusTotal's API
Character code conversion of CSV file using Loop (Shift JIS to UTF8)
I made a program to check the size of a file in Python
I tried to perform a cluster analysis of customers using purchasing data
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx