User Tools

Site Tools


matrix_tools

This is an old revision of the document!


Matrix Tools

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

Bash posting

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=$accesstoken"

PHP posting

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";
 
$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.1590705050.txt.gz · Last modified: 2020/05/28 23:30 by neil