ControlAltDieliet loves Mattermost

Sending alerts to a channel
with an incoming webhook

Our fridges need to retain a constant temperature. When temperature gets too high, we have a time frame to react. Their can be a problem with the fridge or someone didn’t close the fridge well.

Incoming Webhooks are the easiest way to let other programs send messages to Mattermost

We had a monitoring system that warned the responsibles with an email. But let’s be honest. Email will become like faxes, once great but soon outdated. 🙂
Just kidding, we integrated the monitoring system with Mattermost because we can have push notifications and we see at glance who checked the situation. We have an overview in the channel how often incidents occur.


1 Create the Incoming Webhook in four steps

1 Go to the Menu and choose Integrations

System Menu

2 In the following screen, choose Incoming Webhooks.

Menu

3 In the following screen, you see your existing Incoming Webhooks and can add a new one.

Menu

4 Now we can setup the Incoming Webhook in 5 steps

Menu

This is done in 5 steps.

  1. Give the incoming webhook a title
  2. Give the incoming webhook a description
  3. Select the default channel that receives the messages that the incoming webhook is sending
  4. Choose if you want to lock the incoming webhook to the channel or that it is allowed to send to other channels as well
  5. Click the save button

That wasn’t that hard… You get a confirmation with a very important URL, it listens to the messages we will be sending.


2 Write some code

Ok, you are all set! Let's start sending messages!

Do you want to do a quick check if it’s working?

You can check it with curl

curl -i -X POST -H 'Content-Type: application/json' -d '{"text": "Hello, this is some text\nThis is more text. :tada:"}' http://{your-mattermost-site}/hooks/xxx-generatedkey-xxx
# or
curl -i -X POST --data-urlencode 'payload={"text": "Hello, this is some text\nThis is more text. :tada:"}' http://{your-mattermost-site}/hooks/xxx-generatedkey-xxx

#for windows make sure that the double quotes are escaped with a backslash!
curl -i -X POST -H "Content-Type: application/json" -d "{\"text\":Hello, this is some text\nThis is more text. :tada:\"}" https://{your-mattermost-site}/hooks/xxx-generatedkey-xxx

Or with Python


  import requests
  # the part where we read the actual temperatures is  replaced by the next line
  temperature= 3.14159265358979323846
   
  #actual code for sending
  headers = {'Content-Type': 'application/json',}
  values = '{ "text": "The temperature of the fridge is now '+str(temperature).replace(".",",")+' degrees Celcius"}'
  response = requests.post('https://{your-mattermost-site}/hooks/xxx-generatedkey-xxx', headers=headers, data=values)          
  

If you don't like JSON, you can send a plain webrequest as well


    import requests
 
    # the part where we read the actual temperatures is  replaced by the next line
    temperature= 3.14159265358979323846
    
    #actual code for sending
    headers = {}
    values = '{"text": "The temperature of the fridge is now '+str(temperature).replace(".",",")+' degrees Celcius"}'
    response = requests.post('https://{your-mattermost-site}/hooks/xxx-generatedkey-xxx,headers=headers data=values)
  

Or if you prefer NodeJS


      const https = require('https')
      temperature= 3.14159265358979323846 
      const data = JSON.stringify({
        text: "The temperature of the fridge is now "+temperature+" degrees Celcius"
      })
       
      const options = {
        hostname: '{your-mattermost-site}',
        port: 443,
        path: '/hooks/{xxx-generatedkey-xxx}',
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Content-Length': data.length
        }
      }
      const req = https.request(options, res => {
        console.log(`statusCode: ${res.statusCode}`)
       
        res.on('data', d => {
          process.stdout.write(d)
        })
      })
      req.write(data)
      req.end()
    

3 More options

Sending to other channels

If you didn’t check the ‘Lock to this channel’ checkbox you can send messages to other channels by sending a channel-variable

values = '{ "channel": "the-icrowd","text": "The temperature of the fridge is now 42 degrees Celcius"}'

Changing the username

If you want to use another username appearing as sender, you can add the username variable. This setting is default disabled and must be enabled in the Managment Console!

values = '{"username":"The Coolest Fridge","text": "The temperature of the fridge is now -42 degrees Celcius"}'
  

Changing the profile picture

If you want to change the profile picture, add the icon_url in the request.

values = '{"icon_url":"https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/198/freezing-face_1f976.png","text": "The Fonz says the fridge is cool enough"}'

Adding emoticons

If you want to add an emoticon in the text, add the shortcode like :tada: in the text.

values = '{"text": ""Wow, this works :tada:}'
    

Adding more lay-out

If you are not familiar with Markdown layout you can find an introduction in the Mattermost Documentation

values = '{"text": "| Left-Aligned  | Center Aligned  | Right Aligned |
| :------------ |:---------------:| -----:|
| Left column 1 | this text       |  $100 |
| Left column 2 | is              |   $10 |
| Left column 3 | centered        |    $1 |"}'
        


4 Debugging

Is something not working as expected? Go to the System Console and under Enviroment you find the Logging section.

System Console Enviroment Logging

In the Logging section enable Webhook Debugging and set Log Level to Debug.
You'll find in the logfiles (or in the console if you enabled logging to the console) all the relevant information.
My most common mistake is a typo in the request. It gives me an "Unable to parse incoming data" error.

System Console Enviroment Logging

5 More info

Much more info in a comprehensive way you can find in the official Mattermost Developer Documentation on Incoming Webhooks.
You can always ask for help at the Mattermost Forum