Skip to main content

Mattermost Integration Library

This library provides a straightforward and asynchronous way to interact with the Mattermost API. It allows sending messages, uploading files, and seamlessly handling API requests, complete with robust error handling. By using this library, you can integrate your Python-based services with your Mattermost workspace efficiently and reliably.

Key Features

  • Asynchronous Communication: Leverages httpx.AsyncClient for performing non-blocking I/O operations.
  • Message Sending: Post messages directly into a specified Mattermost channel.
  • File Uploads: Upload files (images, documents, and more) to Mattermost and include them in posted messages.
  • Robust Error Handling: Raises custom exceptions (ReqError and NotFoundError) to make troubleshooting and handling HTTP errors simpler.

Requirements

  • Python 3.9+ (recommended)
  • A Mattermost server endpoint that you have access to.
  • A valid Mattermost user/bot token.
  • A specific channel ID from your Mattermost server where messages and files will be posted.

Installation

You can install this library with standard Python tools. For example:

pip install -r requirements.txt

Make sure httpx and other dependencies are listed in your requirements.txt and installed.

Usage Example

Before using the Mattermost class, ensure that:

  • You have a Mattermost URL (e.g., https://mattermost.example.com).
  • You have a valid Mattermost bot token (e.g., generated from the Mattermost system console or developer tools).
  • You know the channel ID where you want to post messages and upload files.

Important: Do not use placeholders. Provide real, working values for url, token, and channel_id when creating an instance of Mattermost. For instance, if your Mattermost instance runs at https://mattermost.example.com, your token is xoxb-abc1234exampletoken, and your target channel ID is 1234567890abcde, then use those exact values directly.

Sending a Simple Message

import asyncio
from mattermost_integration import Mattermost  # adjust the import path as needed

async def main():
    # Replace values below with your real Mattermost details
    url = "https://mattermost.example.com"
    token = "xoxb-abc1234exampletoken"
    channel_id = "1234567890abcde"

    mm = Mattermost(url, token, channel_id)

    # Send a simple text message
    await mm.send_message("Hello from the Mattermost integration library!")

asyncio.run(main())

Uploading a File and Sending It with a Message

If you want to send an image, document, or any other file type, you can upload it first and then attach it to a message:

import asyncio
from mattermost_integration import Mattermost  # adjust the import path as needed

async def main():
    url = "https://mattermost.example.com"
    token = "xoxb-abc1234exampletoken"
    channel_id = "1234567890abcde"
    
    mm = Mattermost(url, token, channel_id)

    # Assume we have some file content in memory. For example, read a local file:
    with open("path/to/yourfile.png", "rb") as f:
        file_content = f.read()

    # Upload the file
    file_id = await mm.upload_file("yourfile.png", file_content)

    # Send a message with the uploaded file attached
    await mm.send_message("Here's the file you requested:", file_ids=[file_id])

asyncio.run(main())

Sending Multiple Files at Once

import asyncio
from mattermost_integration import Mattermost  # adjust the import path as needed

async def main():
    url = "https://mattermost.example.com"
    token = "xoxb-abc1234exampletoken"
    channel_id = "1234567890abcde"

    mm = Mattermost(url, token, channel_id)

    additional_files = []
    for file_path in ["path/to/file1.pdf", "path/to/file2.jpg"]:
        with open(file_path, "rb") as f:
            file_content = f.read()
        additional_files.append((file_path.split("/")[-1], file_content))

    await mm.send_message_with_files("Here are the documents you wanted.", additional_files)

asyncio.run(main())

Error Handling

This library defines custom exceptions to help you handle errors gracefully:

  • ReqError: Raised if a request fails due to non-200/201 status codes, excluding 404.
  • NotFoundError: Raised specifically if a requested resource (e.g., a channel or file) is not found (404).

You can wrap your API calls in try-except blocks to manage these conditions:

try:
    await mm.send_message("Testing error handling.")
except ReqError as e:
    print(f"Request failed: {e}")
except NotFoundError:
    print("The requested resource was not found!")

Logging

The library uses Python’s built-in logging for informational messages. Configure the logging as desired in your application to display or store logs, for example:

import logging

logging.basicConfig(level=logging.INFO)

With the logging level set to INFO, you’ll see details about requests made to the Mattermost API.

Contributing

  • Fork this repository.
  • Create a new branch for your feature or bug fix.
  • Submit a pull request describing your changes.

We welcome improvements and fixes to make this library more powerful and user-friendly!

License

This project is licensed under the MIT License. Refer to LICENSE file for more details.


By following these instructions, you’ll have full control over sending messages and files to your Mattermost channels asynchronously. The focus on no placeholders ensures that all provided values must be actual, working credentials and URLs, resulting in a clean and direct integration experience.

Metadata

Release files for asyncmost-vasiaplaton 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncmost-vasiaplaton 0.1.2
File Size Uploaded Provenance
asyncmost_vasiaplaton-0.1.2.tar.gz 6.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncmost-vasiaplaton 0.1.2
File Interpreter ABI Platform
asyncmost_vasiaplaton-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.1 kB

Release files / asyncmost_vasiaplaton-0.1.2.tar.gz

Download URL asyncmost_vasiaplaton-0.1.2.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
64836568f7184248820f16d0d3d6862af050e97fdc559d3491732f27f0abb7c1
BLAKE2b-256 checksum
How to use checksums
8476efe009bd29b963f82885e40c2a3072d68ae13b29e62ce623d99c0712efc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.6

Release files / asyncmost_vasiaplaton-0.1.2-py3-none-any.whl

Download URL asyncmost_vasiaplaton-0.1.2-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2cd372cee596919b3131392fc8ec4a1bd991dbe306897c45d90e42a9aee9d547
BLAKE2b-256 checksum
How to use checksums
c6e2083d4718512f36e9b59ae0281f21c44dcca64a779bc890cab3255f4b08c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.6

Security for asyncmost-vasiaplaton 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page