From: Neil Date: Thu, 27 Apr 2023 19:09:29 +0000 (+0100) Subject: Init commit of three CLI utils to post/upload and listen to Matrix server X-Git-Url: https://git.mckillop.org/gitweb/?a=commitdiff_plain;h=HEAD;p=matrix-cli Init commit of three CLI utils to post/upload and listen to Matrix server --- 238bda37cf25e99e260672e6e235547153fc2d1a 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"