Message-Attachments in Mattermost while using Incoming Webhook

423 Views Asked by At

Can we add message-attachments as an HTTP request in Mattermost with using incoming webhook? Attaching my Python code, which I am trying to achieve this functionality.

def mattermost():
    mattermost_webhook_url = "https://matter.com/hooks/some-hook"

    message = "Hello, Mattermost"  # Replace with your desired message

    payload = {
        'props': {
            'attachments': [
                {
                    'color': '#FF0000',
                    'text': message# Set a default color for the message
                }
            ]
        }
    }

    response = requests.post(mattermost_webhook_url, json=payload, headers={'Content-Type': 'application/json'})
    print(response)

    if response.status_code == 200:
        return "Message sent successfully to Mattermost channel."
    else:
        return "Failed to send message to Mattermost channel. Status code: " + str(response.status_code)

I try to achieve something like the imageenter image description here

1

There are 1 best solutions below

0
Apurva On

Got the answer to it. You can just remove the props and directly pass attachment in the payload

def mattermost():
    mattermost_webhook_url = "https://matter.com/hooks/some-hook"

    message = "Hello, Mattermost"  # Replace with your desired message

    payload = {
            'attachments': [
                {
                    'color': '#FF0000',
                    'text': message# Set a default color for the message
                }
            ]
    }

    response = requests.post(mattermost_webhook_url, json=payload, headers={'Content-Type': 'application/json'})
    print(response)

    if response.status_code == 200:
        return "Message sent successfully to Mattermost channel."
    else:
        return "Failed to send message to Mattermost channel. Status code: " + str(response.status_code)

This will work!!