Skip to main content

A Python client for Mattermost incoming webhooks.

Project description

Mattermost Client for Incoming Webhooks

A simple Python client for sending messages to Mattermost using incoming webhooks.

Installation

pip install mattermost-client-py

Usage

First, you need to create an incoming webhook in your Mattermost instance. You can find instructions on how to do that here.

Synchronous Client

The synchronous client is useful for simple applications that don't require asynchronous operations.

from mattermost_client import SyncMattermostClient

# Initialize the client with your Mattermost webhook URL
client = SyncMattermostClient("https://your-mattermost-instance.com/hooks/your_webhook_token")

# Send a simple message
response = client.send_message(text='Hello, Mattermost!')
print(response)

# Send a more complex message with attachments
attachments = [
    {
        "fallback": "Upgrade your client to see this attachment.",
        "color": "#FF8000",
        "pretext": "This is optional pretext that shows above the attachment.",
        "author_name": "Mattermost",
        "author_link": "https://mattermost.com/",
        "author_icon": "http://www.mattermost.org/wp-content/uploads/2016/04/icon.png",
        "title": "Example Attachment",
        "title_link": "https://docs.mattermost.com/developer/message-attachments.html",
        "text": "This is the text of the attachment. It can be formatted with Markdown.",
        "fields": [
            {
                "short": False,
                "title": "Long Field",
                "value": "This is a long field, so it is not short."
            },
            {
                "short": True,
                "title": "Short Field",
                "value": "This is a short field."
            }
        ],
        "image_url": "http://www.mattermost.org/wp-content/uploads/2016/04/icon.png"
    }
]


response = client.send_message(
    text='A message with an attachment.',
    attachments=attachments,
    channel='town-square',
    username='My Bot',
    icon_url='https://www.mattermost.org/wp-content/uploads/2016/04/icon.png'
)
print(response)

Asynchronous Usage (with aiohttp)

The asynchronous client is ideal for applications using asyncio.

import asyncio
from mattermost_client import AsyncMattermostClient

async def main():
    # Initialize the client with your Mattermost webhook URL
    mm = AsyncMattermostClient("https://your-mattermost-instance.com/hooks/your_webhook_token")

    # Send a message asynchronously
    response = await mm.send_message("Hello, Mattermost! (async)")
    print(response)

if __name__ == "__main__":
    asyncio.run(main())

Bot Token & Direct Messaging (Personal Messages)

You can also send personal direct messages to individual users using a bot account token.

Synchronous Client

from mattermost_client import SyncMattermostClient

# Initialize with the Mattermost server base URL and your Bot personal access token
client = SyncMattermostClient(
    server_url="https://your-mattermost-instance.com",
    bot_token="your_bot_account_token"
)

# Send a personal message to a user by their username (with or without '@') or user ID
response = client.send_direct_message(recipient="john.doe", text="Hello from bot!")
print(response)

Asynchronous Client

import asyncio
from mattermost_client import AsyncMattermostClient

async def main():
    # Initialize with the Mattermost server base URL and your Bot personal access token
    client = AsyncMattermostClient(
        server_url="https://your-mattermost-instance.com",
        bot_token="your_bot_account_token"
    )

    # Send a personal message asynchronously
    response = await client.send_direct_message(recipient="john.doe", text="Hello from bot! (async)")
    print(response)

if __name__ == "__main__":
    asyncio.run(main())

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mattermost_client_py-0.1.3.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mattermost_client_py-0.1.3-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file mattermost_client_py-0.1.3.tar.gz.

File metadata

  • Download URL: mattermost_client_py-0.1.3.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for mattermost_client_py-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ba19504c6701d9ce48be57e0a0441db45d0694af281950a40caf9a36d73a2342
MD5 361bb34a41a74576da7917553a1c88a7
BLAKE2b-256 af1fe828ef2bcc95a516bdb20e0eae0ed2800799214daa7bccc5218510d364d1

See more details on using hashes here.

File details

Details for the file mattermost_client_py-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for mattermost_client_py-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 64a235a7c8f3a6868bebdb2ac745e0cb993e59a1b255e595fe9e562858d8a03d
MD5 5afe905c64391f4f751772dbfc7c6536
BLAKE2b-256 21e29b4f0a16f913aba641a75e007aac4a3e7778c99485124c7fe595ba8cbe10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page