From 238bda37cf25e99e260672e6e235547153fc2d1a Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 27 Apr 2023 20:09:29 +0100 Subject: [PATCH 1/1] Init commit of three CLI utils to post/upload and listen to Matrix server --- listen_to_matrix.php | 39 +++++++++++++++++++++++++++++ matrix_cp.php | 58 ++++++++++++++++++++++++++++++++++++++++++++ post_to_matrix.php | 16 ++++++++++++ post_to_matrix.sh | 9 +++++++ 4 files changed, 122 insertions(+) create mode 100644 listen_to_matrix.php create mode 100644 matrix_cp.php create mode 100644 post_to_matrix.php create mode 100644 post_to_matrix.sh diff --git a/listen_to_matrix.php b/listen_to_matrix.php new file mode 100644 index 0000000..2ea0038 --- /dev/null +++ b/listen_to_matrix.php @@ -0,0 +1,39 @@ +$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; +} + +?> diff --git a/matrix_cp.php b/matrix_cp.php new file mode 100644 index 0000000..6bf002c --- /dev/null +++ b/matrix_cp.php @@ -0,0 +1,58 @@ +#!/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 +$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"; +} diff --git a/post_to_matrix.php b/post_to_matrix.php new file mode 100644 index 0000000..642405f --- /dev/null +++ b/post_to_matrix.php @@ -0,0 +1,16 @@ +$msgtype, "body"=>$msg)); + +$ch = curl_init($url); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); +$response = curl_exec($ch); +?> diff --git a/post_to_matrix.sh b/post_to_matrix.sh new file mode 100644 index 0000000..fbf9912 --- /dev/null +++ b/post_to_matrix.sh @@ -0,0 +1,9 @@ +#!/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" -- 2.25.1