DEV Community

VaiTon
VaiTon

Posted on

Notification service via [matrix] and WebHooks

Questo articolo è disponibile anche in italiano

Have you ever wanted to set up a notification service for your long standing process / monitoring and didn't know how to do it without setting up an MTA?

Don't get me wrong, emails are good, but sometimes you need a faster approach than setting up postfix (or exim4) with a smarthost and bla bla bla when you just need to be able to recieve a notification when something goes wrong!

In this guide we leverage the power of webhooks and the matrix network to do exactly that.

1. Create a webhook

First, create a new Matrix room. Be sure to make it unencrypted, as t2bot.io webhooks bridge still doesn't support encrypted rooms.

Then, follow t2bot.io documentation for creating a webhook:

  1. Invite @_webhook:t2bot.io to your Matrix room.
  2. Send the message !webhook
  3. The bridge will send you a URL to use alongside simple instructions on how to use it.

2. Create the script

Create a file named notifyMatrix.py and paste this snippet:

(I'm using python for simplicity, but you could also do a simple curl!)

#!/bin/env python3
import requests
import sys

args = " ".join(sys.argv[1:])

WEBHOOK = "..."
BOTNAME = "..."

requests.post(WEBHOOK, 
    json={"text": args, "format": "plain", "displayName": BOTNAME})
Enter fullscreen mode Exit fullscreen mode
  • WEBHOOK should be set to the URL that the bot gave you previously.
  • BOTNAME can be set to anything. It will be used as the username that sends the message to the Matrix room.

The script simply takes the last n1n-1 arguments, creates a string by joining them (with a space in between each) and sends it to the specified webhook.

Now try running

./notifyMatrix.py ciao!
Enter fullscreen mode Exit fullscreen mode

and you should get a message in the room you created with the content ciao!.

If everything went well, you should have your notification service up and running, and it took only a couple of minutes!

3. Usage

You can now use the script wherever you want, for example in a system with a software RAID controlled by MDADM:

In /etc/mdadm/mdadm.conf

...
PROGRAM /root/notifyMatrix.py
...
Enter fullscreen mode Exit fullscreen mode

and you're done!

Credits

If you enjoyed this guide and/or found it useful, consider donating to t2bot.io and to the matrix foundation!

Top comments (0)