Matrix - synapse

Here’s a much easier way of sending messages to a Matrix room from a rule:

sendHttpPostRequest("https://YOURMATRIXDOMAIN/_matrix/client/r0/rooms/YOURROOMID/send/m.room.message?access_token=YOURACCESSTOKEN", "application/json", "{\"msgtype\":\"m.text\", \"body\":\"YOURMESSAGE\"}")

or a little more readable

val String URL = 'https://YOURMATRIXDOMAIN/_matrix/client/r0/rooms/YOURROOMID/send/m.room.message?access_token=YOURACCESSTOKEN'
var String DATA = '{
    "msgtype": "m.text",
    "body": "YOURMESSAGE"
}'

sendHttpPostRequest(URL, "application/json", DATA)

where:

  • YOURMATRIXDOMAIN your (subdomain) URL, such as matrix.example.org
  • YOURROOMID is the room ID in which you want the messages to appear. If you’re using Element as a client you can click on the room name, go to Advanced and you’ll find the Internal room ID which will look something like !hGbNiOPDjfsJfF:matrix.example.org
  • YOURACCESSTOKEN is the access token for your user. To find out my user token for my Matrix user openhab I executed the following command (substituting the password and URL) in a terminal on the same system that is running openHAB:
curl -XPOST -d '{"type":"m.login.password", "user":"openhab", "password":"PASSWORD"}' "https://matrix.example.org/_matrix/client/r0/login"

This will spit out something like the following, from which you can grab your access token.

{
  "user_id": "@openhab:matrix.example.org",
  "access_token": "YOURACCESSTOKEN",
  "home_server": "matrix.example.org",
  "device_id": "HFNFBSUHSK",
  "well_known": {
    "m.homeserver": {
      "base_url": "https://matrix.example.org/"
    }
  }
}

Notes

  • This assumes you have already setup a specific user (such as openhab) on your Matrix/Synapse homeserver, and that you have created a room with the user and one other user (maybe yourself).
  • In theory there should be a way of sending the access token as a header, rather than part of the URL, but I couldn’t get this to work properly: openHAB’s Jetty would spit out an error about basic authentication and I couldn’t be bothered to investigate further - there’s a chance Matrix/Synapse isn’t quite following the HTTP specification.
  • Using this method your messages from openHAB will be sent un-encrypted. As long as you’re still using HTTPS the message won’t be able to be read as it flies on its way to your Matrix/Synapse homeserver, but it does mean that if someone was to have admin access into your Matrix/Synapse server then they can read your messages. I’m not bothered, but you might be.
  • I’m using Jython (with the helper libraries), so the above code for me actually looks like:
from core.actions import HTTP

matrix_openhab_token = "MYUSERTOKEN"
matrix_homeserver_url = "MYMATRIXDOMAIN"
matrix_openhab_room = "MYROOMID"
matrix_message = "MYMESSAGE"

url = "https://{}/_matrix/client/r0/rooms/{}/send/m.room.message?access_token={}".format(matrix_homeserver_url, matrix_openhab_room,matrix_openhab_token) 

HTTP.sendHttpPostRequest(str(url), 'application/json', '{"msgtype":"m.text", "body":matrix_message}')