Mattermost API Wrapper
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
-
mattermostwrapper.exceptions.MattermostAPIError: Login Failed
cause Incorrect Mattermost URL, username, password, or the specified team name does not exist or is inaccessible.fixVerify the `MATTERMOST_URL` (ensure it includes `/api/v4`), `MATTERMOST_LOGIN`, `MATTERMOST_PASSWORD`, and `MATTERMOST_TEAM_NAME` variables are correct and the user has permission to access the team. Test credentials manually via web interface. -
No module named 'mattermostwrapper'
cause The `mattermostwrapper` package is not installed in the current Python environment.fixRun `pip install mattermostwrapper` to install the library. -
requests.exceptions.ConnectionError: HTTPSConnectionPool(...) Failed to establish a new connection
cause The Python environment cannot reach the Mattermost server URL, possibly due to network issues, incorrect URL, or firewall blocking.fixCheck network connectivity from where the script is run to the Mattermost server. Verify the `MATTERMOST_URL` is correct and accessible (e.g., try `ping` or `curl` from the command line). Check firewall rules.
Warnings
- gotcha The library's last update was in February 2020 (v2.2). Newer Mattermost server versions (beyond 5.x) may introduce API changes or new features that are not supported or correctly handled by this wrapper, potentially leading to unexpected behavior or missing functionality.
- breaking Authentication methods for Mattermost servers have evolved (e.g., Personal Access Tokens, OAuth2). This library primarily focuses on username/password login. While `login` method is available, ensure your Mattermost server allows basic username/password authentication for bots/integrations.
- gotcha Errors like 'channel not found' or 'team not found' are common if the provided `team_name` or `channel_id` in the API calls do not exactly match those on the Mattermost server. Case sensitivity and exact names are crucial.
Install
-
pip install mattermostwrapper
Imports
- MattermostAPI
from mattermostwrapper import MattermostAPI
Quickstart
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()