User Tools

Site Tools


matrix_tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
matrix_tools [2020/06/01 13:59]
admin
matrix_tools [2023/04/27 20:24]
neil
Line 1: Line 1:
-====== Matrix ​ ======+====== Matrix ​Command Line Tools ====== 
 ===== Documentation ===== ===== Documentation =====
   * https://​matrix.org/​docs/​api/​client-server/#/​   * https://​matrix.org/​docs/​api/​client-server/#/​
Line 114: Line 115:
  
 ===== Matrix Bot ===== ===== Matrix Bot =====
-The basis of my bot's interaction with Matrix is bascially ​the above code.  Replace the echo line with with whatever you want to do (in my case, I look up a list of rules [i.e. pairs of regexes and their response functions] I use in IRC from a database and act accordingly). ​+The basis of my bot's interaction with Matrix is basically ​the above code.  Replace the echo line with with whatever you want to do (in my case, I look up a list of rules [i.e. pairs of regexes and their response functions] I use in IRC from a database and act accordingly). ​
  
 ===== Posting images to a room ===== ===== Posting images to a room =====
 Media must first be uploaded to your homeserver, then you can send a new message, as above, using the ''​m.image''​ event type.  Here is an example in PHP that works as a '​copy'​ command. Media must first be uploaded to your homeserver, then you can send a new message, as above, using the ''​m.image''​ event type.  Here is an example in PHP that works as a '​copy'​ command.
 <code php matrix_cp.php>​ <code php matrix_cp.php>​
 +#​!/​usr/​bin/​php
 +# Usage example: matrix_cp /​tmp/​test.jpg glasgow
 +# this will send the test.jpg to the glasgow room
 +# this also supports remote URLs
 +<?php
 +$usage = "​Usage:​ "​.$argv[0]."​ filename room_name alt_text[optional]\n";​
 +$filename = "";​
 +$room_name = "#​pythia";​
 +$alt_text = "Image attachment";​
 +$homeserver = "​glasgow.social";​
 +$access_token = "​access_token_goes_here"​
 +// I have a lookup list of friendly room names to full IDs here
 +$channels["​glasgow"​] = "​!BOrDFgeDdZZbUvfjjs:​glasgow.social";​
  
 +if(!empty($argv[2]))
 +   ​$room_name = $argv[2];
 +if(!empty($argv[3]))
 +   ​$alt_text = $argv[3];
 +
 +$room = $channels[$room_name];​
 +if(empty($room))
 +   ​die($room_name."​ not in room list"​);​
 +
 +if(empty($argv[1]))
 +   ​die("​Filename is required. $usage"​);​
 +
 +$filename = $argv[1];
 +$image_data = file_get_contents($filename);​
 +
 +// this whole temporary file saving is all done to support mime_type lookups for remote URLs
 +$tmp_filename = "/​tmp/"​.md5($argv[0].time());​
 +
 +if(empty($image_data))
 +   ​die("​Unable to read file location"​);​
 +file_put_contents($tmp_filename,​ $image_data);​
 +$mime_type = mime_content_type($tmp_filename);​
 +
 +$url = "​https://​$homeserver/​_matrix/​media/​r0/​upload?​access_token=$access_token";​
 +$ch = curl_init($url);​
 +curl_setopt($ch,​ CURLOPT_RETURNTRANSFER,​ true);
 +curl_setopt($ch,​ CURLOPT_HTTPHEADER,​ array("​Content-Type:​ $mime_type"​));​
 +curl_setopt($ch,​ CURLOPT_POSTFIELDS,​ $image_data);​
 +$response = curl_exec($ch);​
 +
 +if(!empty($response_json['​content_uri'​])) {
 +      $url = "​https://​$homeserver/​_matrix/​client/​r0/​rooms/​$room/​send/​m.room.message?​access_token=$access_token";​
 +      $msgtype = "​m.image";​
 +      $mxc_url = $response_json['​content_uri'​];​
 +      $ch = curl_init($url);​
 +      $payload = json_encode(array("​msgtype"​=>​$msgtype,​ "​body"​=>​$alt_text,​ "​url"​=>​$mxc_url));​
 +      curl_setopt($ch,​ CURLOPT_RETURNTRANSFER,​ true);
 +      curl_setopt($ch,​ CURLOPT_POSTFIELDS,​ $payload);
 +      $response = curl_exec($ch);​
 +      // SUCCESS. ​ No output.
 +   } else {
 +      echo "Error uploading $filename. ";
 +      echo implode("​ : ", $response_json)."​\n";​
 +   }
 </​code>​ </​code>​
  
 +You can copy this file to your bin directory and use it on the command line with:
  
-in bash that first uploads the file, then posts it to the room.  I've named it matrix_cp as it basically lets you copy a image to the room: +<code bash> 
-<code bash matrix_cp.sh+matrix_cp ​/tmp/example.jpg glasgow ​"This is an example image"
-#!/bin/bash +
-# Usage: +
-./matrix_cp filename room_id alt_text +
-msgtype=m.text +
-homeserver=glasgow.social +
-room=!BOrDFgeDdZZbUvfjjs:​glasgow.social +
-access_token=put_your_user_access_token_here+
 </​code>​ </​code>​
 +
 +===== Git =====
 +You can [[https://​coding.openguide.co.uk/​gitweb/?​p=matrix-cli;​a=summary|view the repository]] or clone the code directory from my repository:
 +<code bash>
 +git clone https://​coding.openguide.co.uk/​git/​matrix-cli/​
 +</​code>​
 +
matrix_tools.txt · Last modified: 2023/12/27 13:45 by admin