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
Next revision Both sides next revision
matrix_tools [2020/05/28 23:54]
admin
matrix_tools [2020/05/30 08:59]
admin
Line 1: Line 1:
-====== Matrix ​Tools ======+====== 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. It can be very simple to post a message to a channel on Matrix.
-==== Bash posting ​====+==== Via bash/​curl ​====
 This script requires ''​jq'',​ the command line JSON processor. This script requires ''​jq'',​ the command line JSON processor.
 <code bash> <code bash>
Line 19: Line 31:
 </​file>​ </​file>​
  
-==== PHP posting with Curl ====+==== PHP example using Curl ====
 <file php post_to_matrix.php>​ <file php post_to_matrix.php>​
 <?php <?php
Line 39: Line 51:
 ?> ?>
 </​file>​ </​file>​
 +
 +===== Listening to channels =====
 +This is pretty straightforward. ​ This is just how to see messages that are posted, but if you explore what is returned then you can see all sorts of things too (like joins, images, likes, invites etc).  Very cool.
 +
 +<code php ListenToMatrix.php>​
 +<?php
 +$homeserver = "​glasgow.social";​
 +$access_token = "​access_token_goes_here";​
 +
 +// this is so you can start and stop the program, and it'll continue where it left off
 +$tracking_file "/​tmp/​listen_matrix.json";​
 +
 +while(true) {
 +$since = file_get_contents($tracking_file);​
 +$messages = get_new_events($since);​
 +if(!empty($messages['​rooms'​]['​join'​] and is_array($messages['​rooms'​]['​join'​]))) {
 +   ​foreach($messages['​rooms'​]['​join'​] as $room_id=>​$data) {
 +      foreach($data['​timeline'​]['​events'​] as $event_id=>​$event) {
 +         ​if($event['​type'​] == "​m.room.message"​) {
 +            $sender = $event['​sender'​];​
 +            $content = "not text";
 +            if($event['​content'​]['​msgtype'​] == "​m.text"​)
 +               ​$content = $event['​content'​]['​body'​];​
 +            echo "​$sender $room_id $content\n";​
 +         }
 +      }
 +   }
 +}
 +$new_tracking_data = $messages['​next_batch'​];​
 +file_put_contents($tracking_file,​ $new_tracking_data);​
 +}
 +
 +function get_new_events($since) {
 +   ​global $homeserver,​$access_token;​
 +   // timeout means the server will wait and only respond after 30 seconds, ​
 +   // however if a message is returned before that time, it will return immediately
 +   $url = "​https://​$homeserver/​_matrix/​client/​r0/​sync?​access_token=$access_token&​timeout=30000";​
 +   ​if(!empty($since))
 +      $url .= "&​since=$since";​
 +   $data = json_decode(file_get_contents($url),​ true);
 +   ​return $data;
 +}
 +
 +
 +?>
 +</​code>​
 +
 +===== Matrix Bot =====
 +  * See [[Matrix Bot]]
matrix_tools.txt · Last modified: 2023/12/27 13:45 by admin