User Tools

Site Tools


matrix_bot

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
matrix_bot [2020/05/30 11:21]
admin
— (current)
Line 1: Line 1:
-====== Matrix Bot ====== 
-My first attempt at a Matrix box.  Fist off, I wanted to get a list of events (messages). ​ There was an event stream in the docs, but that's been deprecated. ​ 
- 
-You can get a list of events from a particular channel by sending a request to ''/​messages''​ (see the documentation here: https://​matrix.org/​docs/​api/​client-server/#​!/​Room32participation/​getRoomEvents). So, a simple way to get the events is something like this: 
-<code php> 
-function get_new_events($room) { 
-   ​$homeserver = "​glasgow.social";​ 
-   ​$access_token = "​access_token_goes_here"​ 
-   $url = "​https://​$homeserver/​_matrix/​client/​r0/​rooms/​$room/​messages?​access_token=$access_token&​from=$tracking_id";​ 
-   $data = json_decode(file_get_contents($url),​ true); 
-   ​return $data; 
-} 
-</​code>​ 
- 
-The way to get all events (i.e. all messages on channels you are a member of is to use the ''/​sync''​ endpoint. ​ The documentation for that is at: https://​matrix.org/​docs/​api/​client-server/#​!/​Room32participation/​sync 
- 
- 
-===== Command line event listener ===== 
-I run this in a terminal and it shows me everything happening on the rooms I'm in, in a simple way. 
-<code php ListToMatrix.php>​ 
-$homeserver = "​glasgow.social";​ 
-$access_token = "​access_token_goes_here";​ 
- 
-$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 = "​Image/​Video/​Audio";​ 
-            if($event['​content'​]['​msgtype'​] == "​m.text"​) 
-               ​$content = $event['​content'​]['​body'​];​ 
-            echo "​$sender ("​.$rooms[$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;​ 
-   $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>​ 
- 
-Now, to make an actual functioning bot, replace the echo line 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). ​ See the [[Matrix tools]] page for notes on how to post your reply to a room. 
  
matrix_bot.1590834076.txt.gz ยท Last modified: 2020/05/30 11:21 by admin