--- /dev/null
+<?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;
+}
+
+?>
--- /dev/null
+#!/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";
+}
--- /dev/null
+<?php
+$msgtype = "m.text";
+$homeserver = "glasgow.social";
+$room = "!BOrDFgeDdZZbUvfjjs:glasgow.social";
+$access_token="put_your_user_access_token_here"
+$url = "https://$homeserver/_matrix/client/r0/rooms/$room/send/m.room.message?access_token=$access_token";
+
+$msg = "Hello world";
+
+$payload = json_encode(array("msgtype"=>$msgtype, "body"=>$msg));
+
+$ch = curl_init($url);
+curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
+$response = curl_exec($ch);
+?>
--- /dev/null
+#!/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"