diff --git a/prova.png b/prova.png new file mode 100644 index 0000000..fd0bd85 Binary files /dev/null and b/prova.png differ diff --git a/sample_botsettings.json b/sample_botsettings.json index 826ad8c..c3cc996 100644 --- a/sample_botsettings.json +++ b/sample_botsettings.json @@ -15,7 +15,7 @@ "lastmessageid": "event id of the last message sent in this room. DO NOT EDIT" } ], - "matrixhomeserver": "https://homeserver.com/_matrix/client/r0", + "matrixhomeserver": "https://homeserver.com/_matrix/", "tgtoken": "Token key given from BotFather", "matrixuser": "@MatrixUser:homeserver.com", "matrixpswd": "Matrix user password" diff --git a/src/com/em/miguelbridge/matrixbot/MatrixBot.java b/src/com/em/miguelbridge/matrixbot/MatrixBot.java index 4b399ee..fdd3b3c 100644 --- a/src/com/em/miguelbridge/matrixbot/MatrixBot.java +++ b/src/com/em/miguelbridge/matrixbot/MatrixBot.java @@ -1,11 +1,8 @@ package com.em.miguelbridge.matrixbot; import com.em.miguelbridge.Launcher; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.UnsupportedEncodingException; + +import java.io.*; import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.logging.Level; @@ -78,7 +75,7 @@ public class MatrixBot { * @throws java.net.URISyntaxException */ public String login() throws IOException, ParseException, URISyntaxException { - String requestUrl = homeUrl + "/login"; + String requestUrl = homeUrl + "client/r0/login"; String[][] reqParams = new String[][] { {"type", "m.login.password"}, @@ -94,7 +91,7 @@ public class MatrixBot { } public String joinRoom(String roomAddress) throws IOException, ParseException, URISyntaxException { - String requestUrl = homeUrl + String.format("/rooms/%s/join?access_token=%s", + String requestUrl = homeUrl + String.format("client/r0/rooms/%s/join?access_token=%s", roomAddress, accessToken); String[][] reqParams = null; @@ -104,7 +101,7 @@ public class MatrixBot { } public synchronized String sendMessage(String message, String roomAddress) throws IOException, URISyntaxException { - String requestUrl = homeUrl + String.format("/rooms/%s/send/m.room.message?access_token=%s", + String requestUrl = homeUrl + String.format("client/r0/rooms/%s/send/m.room.message?access_token=%s", roomAddress, accessToken); String[][] reqParams = new String[][] { @@ -116,12 +113,19 @@ public class MatrixBot { return risposta[0] + " - " + risposta[1]; } + + public synchronized String sendFile(String roomAddress, File file) throws IOException { + String requestUrl = homeUrl + String.format("media/r0/upload?filename=%s&access_token=%s", + file.getName(), accessToken); + String[] risposta = RequestHandler.postRequestFile(requestUrl, file); + return risposta[0] + " - " + risposta[1]; + } public String[] getLastMessage(String roomAddress) { try { String filtro = URLEncoder.encode("{\"room\":{\"timeline\":{\"limit\":1}}}", "UTF-8"); String requestUrl = homeUrl + - String.format("/sync?filter=%s&access_token=%s", + String.format("client/r0/sync?filter=%s&access_token=%s", filtro, accessToken); String[] risposta = RequestHandler.getRequest(requestUrl); diff --git a/src/com/em/miguelbridge/matrixbot/RequestHandler.java b/src/com/em/miguelbridge/matrixbot/RequestHandler.java index 1ec59ad..8d7ce41 100644 --- a/src/com/em/miguelbridge/matrixbot/RequestHandler.java +++ b/src/com/em/miguelbridge/matrixbot/RequestHandler.java @@ -1,11 +1,14 @@ package com.em.miguelbridge.matrixbot; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; + +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; @@ -13,6 +16,9 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.HttpClientBuilder; import org.json.simple.JSONObject; @@ -47,7 +53,7 @@ public class RequestHandler { HttpPost post = new HttpPost(inUrl); //add header - post.setHeader("User-Agent", "Mozilla/5.0"); + post.setHeader("User-Agent", "Mozilla/5.0"); post.addHeader("Content-Type", "charset=UTF_8"); JSONObject obj = new JSONObject(); @@ -62,18 +68,46 @@ public class RequestHandler { StringEntity requestEntity = new StringEntity(obj.toJSONString(), ContentType.APPLICATION_JSON); post.setEntity(requestEntity); - HttpResponse response = client.execute(post); + HttpResponse response = client.execute(post); - BufferedReader rd = new BufferedReader( - new InputStreamReader(response.getEntity().getContent())); + BufferedReader rd = new BufferedReader( + new InputStreamReader(response.getEntity().getContent())); - StringBuffer result = new StringBuffer(); - String line = ""; - while ((line = rd.readLine()) != null) - result.append(line); + StringBuffer result = new StringBuffer(); + String line = ""; + while ((line = rd.readLine()) != null) + result.append(line); String[] risposta = new String[] {""+response.getStatusLine().getStatusCode(), result.toString()}; return risposta; } + + public static String[] postRequestFile(String inUrl, File file) throws IOException { + FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + builder.addPart("content", fileBody); + HttpEntity entity = builder.build(); + + HttpPost request = new HttpPost(inUrl); + request.setEntity(entity); + + HttpClient client = HttpClientBuilder.create().build(); + HttpResponse response = client.execute(request); + + + BufferedReader rd = new BufferedReader( + new InputStreamReader(response.getEntity().getContent())); + + StringBuffer result = new StringBuffer(); + String line = ""; + while ((line = rd.readLine()) != null) + result.append(line); + + String[] risposta = + new String[] {""+response.getStatusLine().getStatusCode(), result.toString()}; + return risposta; + } } diff --git a/src/test/UploadTest.java b/src/test/UploadTest.java new file mode 100644 index 0000000..098b5ef --- /dev/null +++ b/src/test/UploadTest.java @@ -0,0 +1,55 @@ +package test; + +import com.em.miguelbridge.matrixbot.MatrixBot; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.impl.client.HttpClientBuilder; +import org.json.simple.JSONObject; +import org.json.simple.parser.ParseException; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URISyntaxException; + +public class UploadTest { + public static void main(String[] args) throws IOException, ParseException, URISyntaxException { + MatrixBot bot = new MatrixBot(); + String token = bot.login(); + String url = "https://maxwell.ydns.eu/_matrix/media/r0/" + + "upload?filename=prova.png&access_token=" + token; + + File file = new File("prova.png"); + FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + builder.addPart("file", fileBody); + HttpEntity entity = builder.build(); + + HttpPost request = new HttpPost(url); + request.setEntity(entity); + + HttpClient client = HttpClientBuilder.create().build(); + HttpResponse response = client.execute(request); + + + BufferedReader rd = new BufferedReader( + new InputStreamReader(response.getEntity().getContent())); + + StringBuffer result = new StringBuffer(); + String line = ""; + while ((line = rd.readLine()) != null) + result.append(line); + + System.out.println(response.getStatusLine() + " -e- " + result.toString()); + } +}