User Tools

Site Tools


matrix_tools

This is an old revision of the document!


Matrix

Getting your access token

  • Using Riot (that's whats running on https://matrix.glasgow.social) - Click on your profile name at the top left corner and select Settings
  • Click Help & About from the list then scroll down to the 'Advanced' section.
  • You should see the bottom line that reads Access Token <click to reveal>
  • Copy and paste that into the example below

Getting a room ID

  • Click on the three dots at the edge of a channel name and click Settings
  • Choose Advanced to see the Internal Room ID
  • #glasgow on the server is !BOrDFgeDdZZbUvfjjs:glasgow.social

Posting messages to a channel

It can be very simple to post a message to a channel on Matrix.

Via bash/curl

This script requires jq, the command line JSON processor.

sudo apt install jq
post_to_matrix.sh
#!/bin/bash
# Usage:
# echo "Hello world" | ./post_to_matrix.sh
msgtype=m.text
homeserver=glasgow.social
room=!BOrDFgeDdZZbUvfjjs:glasgow.social
access_token=put_your_user_access_token_here
 
curl -XPOST -d "$( jq -Rsc --arg msgtype "$msgtype" '{$msgtype, body:.}')" "https://$homeserver/_matrix/client/r0/rooms/$room/send/m.room.message?access_token=$access_token"

PHP example using Curl

post_to_matrix.php
<?php
$msgtype = "m.text";
$homeserver = "glasgow.social";
$room = "!BOrDFgeDdZZbUvfjjs:glasgow.social";
$access_token="put_your_user_access_token_here"
$url = "https://$homeserver/_matrix/client/r0/rooms/$room/send/m.room.message?access_token=$access_token";
 
$msg = "Hello world";
 
$payload = json_encode(array("msgtype"=>$msgtype, "body"=>$msg));
 
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
 
?>
matrix_tools.1590819837.txt.gz · Last modified: 2020/05/30 07:23 by admin