1
1
mirror of https://github.com/ahabhyde/miguelbridge synced 2025-01-09 22:24:18 +01:00

Aggiunto richiesta post file, da aggiornare guida file json

This commit is contained in:
Ahab Hyde 2018-04-20 01:11:05 +02:00
parent b26f00736a
commit c4bce1ac74
5 changed files with 111 additions and 18 deletions

BIN
prova.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

View File

@ -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"

View File

@ -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);

View File

@ -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;
}
}

55
src/test/UploadTest.java Normal file
View File

@ -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());
}
}