Send notification to Mattermost

Hi,

I’m trying to send a message from HA to Mattermost. I have a shell script, a perl script and a curl command that can do this from the command line.

The perl script or the shell script would be the preferred method. However, I don’t see how to parameterize the message text. For example I would like to execute this command:

./notify.pl “This is a message” “@user

But all I can ever get to work is:

./notify.pl

The alternative is to use a REST notifier. I’ve tried various variations of this theme:

notify:

  • platform: rest
    name: NotifyMe
    method: POST
    headers:
    Content-Type: application/json
    resource: https://mm.example.com/hooks/nceur8
    data:{text: “This is a test”, message: “this is a message” }
    message_param_name: text

But when I run this notifier, the Mattermost logs indicate that the message body is null.

The curl command that works from the command line looks like this:

curl -i -X POST -H ‘Content-Type: application/json’ -d ‘{“text”: “Hello, this is some text\nThis is more text” }’ https://mm.example.com/hooks/nceur8

So, I hope you can see what I’m trying to do.

What am I missing?

Mike.

I’ve been working on it for my instance as well and it’s fairly simple once we dig into Mattermost’s documentation.
Below is what I’m using it seems to get the job done thus far. I’m sure we’d need to test more to get more robust notifications but at least for now we have text.

notify:
  - name: mattermost
    platform: rest
    resource: https://your-mattermost-server.com/hooks/xxx-generatedkey-xxx
    method: POST_JSON
    message_param_name: text

That worked for me! Thank you!

Is the generated key the personal access token? Also, is this just a service after integrated to add to automation?

Yes, the generated key is just a personal access token. If I understand your question correctly, yes, this creates a service that I can call in an automation to send a message to someone via Mattermost.

Hope that helps.

Mike

Got it. How to you choose which channel to send notification to within HA automation? I have matter showing as notification option to call, but I do not get anything in my Home Assistant channel when triggering a test event.

This is my automation.

 alias: Motion Notify test
 description: ""
 trigger:
   - platform: state
     entity_id:
       - group.motion
     to: "on"
 condition: []
 action:
   - service: notify.mattermost
     data:
       message: test motion
       title: test motion2
 mode: single

This is part of my config file

notify:

  - platform: synology_chat

    name: hass_synchat

    verify_ssl: false

    resource: https://removed

  - platform: rest

    name: mattermost

    resource: https://10.34.21.10/hooks/gfgfg34534fgdsfgdfgg43534534y

    method: POST_JSON

    message_param_name: text

    notify:

Working on this also. I am assuming the -notify is put into the configuration.yaml. Is the synology_chat just a second chat service which if I am only using mattermost I don’t need it?

I did try my install with notify in confuguration.yaml, and a automation as follows. When the automation runs no errors in HA, but also no errors in Mattermost.

alias: Motion Matter Most
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 6b8ebb2b5e0e51c4cfdbba7b8b947e69
    entity_id: c4d66ef79d80c4305c475140a8b6cf6f
    domain: binary_sensor
condition: []
action:
  - service: notify.mattermost
    data:
      message: test motion
      title: test motion2
mode: single

notify:
  - platform: rest
    name: mattermost
    resource: https://mattermost.my-own.url/hooks/-----secret key-----
    method: POST_JSON
    message_param_name: text

guessing i am missing something, just not sure where too look.

Just FYI that in the latest version 9.11, I was able to make this rest_command that is slightly more flexible than rest notifier:

  artpolis_notify:
    url: "https://<URL-of-your-mattermost-server>/api/v4/posts"
    method: POST
    headers:
      Authorization: "Bearer <the token you get when you create a bot in the admin console>"
      Content-Type: "application/json"
    payload: >-
      {
        "channel_id": "{{ channel_id | default('your-default-id-channel') }}",
        "message": "{{ message }}",
        "props": {
          "attachments": [
            {
              "pretext": "{{ pretext }}",
              "text": "{{ text }}"
            }
          ]
        }
      }
    content_type: "application/json"

Notes:

  • ID of the channel can be found in the Channel’s info:

  • You need to add the bot into your team and then into the channel you want it to write.

Then can be called as:

data:
  message: This is a message from a bot
  pretext: Look some text
  text: This is text
action: rest_command.artpolis_notify
1 Like