Mattermost API Wrapper

2.2 · maintenance · verified Thu Apr 16

Mattermostwrapper is a Python library that provides a wrapper for the Mattermost API v4, enabling programmatic interaction with Mattermost servers. The current version is 2.2, with its last release in February 2020. Given its last update, the project appears to be in a maintenance state, supporting core API v4 functionalities without frequent new feature additions or updates.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Mattermost API client, logs in with credentials, retrieves available teams and channels, and demonstrates how to post a message to a specific channel. Ensure Mattermost server URL, login, password, and team name are correctly configured via environment variables for a runnable example.

import os
from mattermostwrapper import MattermostAPI

# Replace with your Mattermost instance details
MATTERMOST_URL = os.environ.get('MATTERMOST_URL', 'https://your-mattermost-instance.com/api/v4')
MATTERMOST_LOGIN = os.environ.get('MATTERMOST_LOGIN', 'your_username')
MATTERMOST_PASSWORD = os.environ.get('MATTERMOST_PASSWORD', 'your_password')
MATTERMOST_TEAM_NAME = os.environ.get('MATTERMOST_TEAM_NAME', 'your_team_name')
MATTERMOST_CHANNEL_ID = os.environ.get('MATTERMOST_CHANNEL_ID', 'your_channel_id') # Optional for direct posts


def main():
    try:
        m = MattermostAPI(MATTERMOST_URL, MATTERMOST_TEAM_NAME)
        m.login(MATTERMOST_LOGIN, MATTERMOST_PASSWORD)
        print("Successfully logged into Mattermost.")

        # Example: Get teams
        teams = m.get_teams()
        print(f"Teams: {teams}")

        # Example: Get channel listings for a team
        if teams:
            first_team_id = teams[0]['id']
            # If MATTERMOST_TEAM_NAME is used above, you might need to get team_id dynamically
            # For simplicity, if MATTERMOST_TEAM_NAME is set to a specific team name during MattermostAPI init,
            # the internal methods should work with the associated team.
            # If not, you'd need to find the team ID from 'teams'
            
            print(f"Getting channels for team: {MATTERMOST_TEAM_NAME}")
            channel_list = m.get_channel_listing(MATTERMOST_TEAM_NAME)
            for channel in channel_list:
                print(f"  Channel: {channel['display_name']} (ID: {channel['id']})")
            
            # Example: Post a message to a channel (requires channel_id)
            if MATTERMOST_CHANNEL_ID:
                message = "Hello from mattermostwrapper! This is a test message."
                post_response = m.post_channel(MATTERMOST_CHANNEL_ID, message)
                print(f"Message posted: {post_response['message']}")
            else:
                print("MATTERMOST_CHANNEL_ID not set. Skipping message post.")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == '__main__':
    main()

view raw JSON →