diff --git a/.idea/artifacts/MiguelBridge_jar.xml b/.idea/artifacts/MiguelBridge_jar.xml new file mode 100644 index 0000000..67d6156 --- /dev/null +++ b/.idea/artifacts/MiguelBridge_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/MiguelBridge_jar + + + + + \ No newline at end of file diff --git a/image.jpg b/image.jpg new file mode 100644 index 0000000..297a3f9 Binary files /dev/null and b/image.jpg differ diff --git a/lib/telegrambots-3.0-jar-with-dependencies.jar b/lib/OLDtelegrambots-3.0-jar-with-dependencies.jar similarity index 100% rename from lib/telegrambots-3.0-jar-with-dependencies.jar rename to lib/OLDtelegrambots-3.0-jar-with-dependencies.jar diff --git a/lib/maybe/commons-codec-1.10.jar b/lib/maybe/commons-codec-1.10.jar deleted file mode 100644 index 1d7417c..0000000 Binary files a/lib/maybe/commons-codec-1.10.jar and /dev/null differ diff --git a/lib/maybe/commons-logging-1.2.jar b/lib/maybe/commons-logging-1.2.jar deleted file mode 100644 index 93a3b9f..0000000 Binary files a/lib/maybe/commons-logging-1.2.jar and /dev/null differ diff --git a/lib/maybe/fluent-hc-4.5.5.jar b/lib/maybe/fluent-hc-4.5.5.jar deleted file mode 100644 index f1495c7..0000000 Binary files a/lib/maybe/fluent-hc-4.5.5.jar and /dev/null differ diff --git a/lib/maybe/httpclient-4.5.5.jar b/lib/maybe/httpclient-4.5.5.jar deleted file mode 100644 index 7796b0e..0000000 Binary files a/lib/maybe/httpclient-4.5.5.jar and /dev/null differ diff --git a/lib/maybe/httpclient-cache-4.5.5.jar b/lib/maybe/httpclient-cache-4.5.5.jar deleted file mode 100644 index 264dc1c..0000000 Binary files a/lib/maybe/httpclient-cache-4.5.5.jar and /dev/null differ diff --git a/lib/maybe/httpclient-win-4.5.5.jar b/lib/maybe/httpclient-win-4.5.5.jar deleted file mode 100644 index 25574c9..0000000 Binary files a/lib/maybe/httpclient-win-4.5.5.jar and /dev/null differ diff --git a/lib/maybe/httpcore-4.4.9.jar b/lib/maybe/httpcore-4.4.9.jar deleted file mode 100644 index e2d3cbf..0000000 Binary files a/lib/maybe/httpcore-4.4.9.jar and /dev/null differ diff --git a/lib/maybe/httpmime-4.5.5.jar b/lib/maybe/httpmime-4.5.5.jar deleted file mode 100644 index 4d3ee66..0000000 Binary files a/lib/maybe/httpmime-4.5.5.jar and /dev/null differ diff --git a/lib/maybe/jna-4.4.0.jar b/lib/maybe/jna-4.4.0.jar deleted file mode 100644 index 521bd92..0000000 Binary files a/lib/maybe/jna-4.4.0.jar and /dev/null differ diff --git a/lib/maybe/jna-platform-4.4.0.jar b/lib/maybe/jna-platform-4.4.0.jar deleted file mode 100644 index ce54d8f..0000000 Binary files a/lib/maybe/jna-platform-4.4.0.jar and /dev/null differ diff --git a/lib/telegrambots-3.6.1-jar-with-dependencies.jar b/lib/telegrambots-3.6.1-jar-with-dependencies.jar new file mode 100644 index 0000000..4f34196 Binary files /dev/null and b/lib/telegrambots-3.6.1-jar-with-dependencies.jar differ diff --git a/nbproject/project.properties b/nbproject/project.properties index 27040c1..f11b0a8 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -29,17 +29,17 @@ dist.jar=${dist.dir}/MiguelBridge.jar dist.javadoc.dir=${dist.dir}/javadoc endorsed.classpath= excludes= -file.reference.GSON.jar=lib\\GSON.jar -file.reference.httpclient-4.5.5.jar=lib\\httpclient-4.5.5.jar -file.reference.json-simple-1.1.1.jar=lib\\json-simple-1.1.1.jar -file.reference.telegrambots-3.0-jar-with-dependencies.jar=lib\\telegrambots-3.0-jar-with-dependencies.jar +file.reference.GSON.jar=lib/GSON.jar +file.reference.httpclient-4.5.5.jar=lib/httpclient-4.5.5.jar +file.reference.json-simple-1.1.1.jar=lib/json-simple-1.1.1.jar +file.reference.telegrambots-3.6.1-jar-with-dependencies.jar=lib/telegrambots-3.6.1-jar-with-dependencies.jar includes=** jar.compress=false javac.classpath=\ - ${file.reference.json-simple-1.1.1.jar}:\ - ${file.reference.telegrambots-3.0-jar-with-dependencies.jar}:\ + ${file.reference.GSON.jar}:\ ${file.reference.httpclient-4.5.5.jar}:\ - ${file.reference.GSON.jar} + ${file.reference.json-simple-1.1.1.jar}:\ + ${file.reference.telegrambots-3.6.1-jar-with-dependencies.jar} # Space-separated list of extra javac options javac.compilerargs= javac.deprecation=false diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..14d31b0 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.em.miguelbridge.Launcher + diff --git a/src/com/em/miguelbridge/Launcher.java b/src/com/em/miguelbridge/Launcher.java index b3836c3..e10bc26 100644 --- a/src/com/em/miguelbridge/Launcher.java +++ b/src/com/em/miguelbridge/Launcher.java @@ -1,7 +1,7 @@ package com.em.miguelbridge; -import com.em.miguelbridge.matrixbot.MatrixBot; -import com.em.miguelbridge.telegrambot.TGBot; +import com.em.miguelbridge.botmatrix.MatrixBot; +import com.em.miguelbridge.bottelegram.TGBot; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; diff --git a/src/com/em/miguelbridge/matrixbot/MatrixBot.java b/src/com/em/miguelbridge/botmatrix/MatrixBot.java similarity index 93% rename from src/com/em/miguelbridge/matrixbot/MatrixBot.java rename to src/com/em/miguelbridge/botmatrix/MatrixBot.java index e86a1e2..bd10008 100644 --- a/src/com/em/miguelbridge/matrixbot/MatrixBot.java +++ b/src/com/em/miguelbridge/botmatrix/MatrixBot.java @@ -1,12 +1,11 @@ -package com.em.miguelbridge.matrixbot; +package com.em.miguelbridge.botmatrix; import com.em.miguelbridge.Launcher; import java.io.*; import java.net.URISyntaxException; import java.net.URLEncoder; -import java.util.logging.Level; -import java.util.logging.Logger; + import org.json.simple.*; import org.json.simple.parser.*; @@ -128,7 +127,7 @@ public class MatrixBot { public synchronized String sendFile(String roomAddress, File file) throws IOException, URISyntaxException, ParseException { String requestUrl = homeUrl + String.format("media/r0/upload?filename=%s&access_token=%s", - file.getName(), accessToken); + file.getName()+".jpg", accessToken); String[] risposta = RequestHandler.postRequestFile(requestUrl, file); JSONObject uriFileObj = (JSONObject) new JSONParser().parse(risposta[1]); @@ -138,23 +137,15 @@ public class MatrixBot { requestUrl = homeUrl + String.format("client/r0/rooms/%s/send/m.room.message?access_token=%s", roomAddress, accessToken); - /* - String[][] reqParams = new String[][] { - {"msgtype", "m.image"}, - {"body", "image.png"}, - {"url", uriFile} - }; - */ - JSONObject reqParams = new JSONObject(); JSONObject objInfo = new JSONObject(); - objInfo.put("mimetype", "image/png"); + objInfo.put("mimetype", "image/jpg"); objInfo.put("size", file.length()); reqParams.put("info", objInfo); reqParams.put("msgtype", "m.file"); - reqParams.put("body", "image.png"); + reqParams.put("body", file.getName()+".jpg"); reqParams.put("url", uriFile); risposta = RequestHandler.postRequestJSON(requestUrl, reqParams); diff --git a/src/com/em/miguelbridge/matrixbot/RequestHandler.java b/src/com/em/miguelbridge/botmatrix/RequestHandler.java similarity index 89% rename from src/com/em/miguelbridge/matrixbot/RequestHandler.java rename to src/com/em/miguelbridge/botmatrix/RequestHandler.java index 810db5b..977461a 100644 --- a/src/com/em/miguelbridge/matrixbot/RequestHandler.java +++ b/src/com/em/miguelbridge/botmatrix/RequestHandler.java @@ -1,24 +1,16 @@ -package com.em.miguelbridge.matrixbot; +package com.em.miguelbridge.botmatrix; import java.io.*; -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; import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ByteArrayEntity; 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.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; diff --git a/src/com/em/miguelbridge/telegrambot/TGBot.java b/src/com/em/miguelbridge/bottelegram/TGBot.java similarity index 60% rename from src/com/em/miguelbridge/telegrambot/TGBot.java rename to src/com/em/miguelbridge/bottelegram/TGBot.java index 8f4493a..0df7859 100644 --- a/src/com/em/miguelbridge/telegrambot/TGBot.java +++ b/src/com/em/miguelbridge/bottelegram/TGBot.java @@ -1,21 +1,23 @@ -package com.em.miguelbridge.telegrambot; +package com.em.miguelbridge.bottelegram; import com.em.miguelbridge.Launcher; -import com.em.miguelbridge.matrixbot.MatrixBot; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.logging.Level; -import java.util.logging.Logger; +import com.em.miguelbridge.botmatrix.MatrixBot; + +import java.io.*; +import java.util.Comparator; +import java.util.List; + import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import org.telegram.telegrambots.api.methods.GetFile; import org.telegram.telegrambots.api.methods.send.*; +import org.telegram.telegrambots.api.objects.PhotoSize; import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.bots.TelegramLongPollingBot; +import org.telegram.telegrambots.exceptions.TelegramApiException; /* * @author Emanuele Magon @@ -36,7 +38,7 @@ public class TGBot extends TelegramLongPollingBot { */ //Controllo per vedere se l'update è un messaggio testuale e che esso non sia vuoto - if (update.hasMessage()) { + if (update.hasMessage() && update.getMessage().hasText()) { if (update.getMessage().getText().equalsIgnoreCase("/chatid") || update.getMessage().getText().equalsIgnoreCase("/chatid@" + getBotUsername())) { String chat_id = "" + update.getMessage().getChatId(); @@ -59,12 +61,62 @@ public class TGBot extends TelegramLongPollingBot { destination = getDestinationRoom(chat_id); if (destination == null) throw new Exception(); - echoToMatrix(testoMessaggio, sender, destination); + matrixBot.sendMessage(sender + ":\n" + testoMessaggio, destination); } catch (Exception ex) { cEcho(chat_id, "Errore: questa chat non è collegata a matrix."); } } } + + else if (update.hasMessage() && update.getMessage().hasPhoto()) { + String chat_id = "" + update.getMessage().getChatId(); + String sender = update.getMessage().getFrom().getFirstName() + " " + + update.getMessage().getFrom().getLastName(); + String destination; + java.io.File downloadedFile = null; + + //TODO Scarica la foto + // When receiving a photo, you usually get different sizes of it + List photos = update.getMessage().getPhoto(); + + // We fetch the bigger photo + PhotoSize foto = photos.stream() + .sorted(Comparator.comparing(PhotoSize::getFileSize).reversed()) + .findFirst() + .orElse(null); + + String filePath; + if (foto.hasFilePath()) { // If the file_path is already present, we are done! + filePath = foto.getFilePath(); + } else { // If not, let find it + // We create a GetFile method and set the file_id from the photo + GetFile getFileMethod = new GetFile(); + getFileMethod.setFileId(foto.getFileId()); + + try { + // We execute the method using AbsSender::execute method. + final org.telegram.telegrambots.api.objects.File file = execute(getFileMethod); + // We now have the file_path + filePath = file.getFilePath(); + // Download the file calling AbsSender::downloadFile method + downloadedFile = downloadFile(filePath); + } catch (TelegramApiException e) { + e.printStackTrace(System.err); + } + } + + + try { + destination = getDestinationRoom(chat_id); + if (destination == null) + throw new Exception(); + matrixBot.sendMessage(sender + " ha inviato una foto:", destination); + matrixBot.sendFile(destination, downloadedFile); + } catch (Exception ex) { + cEcho(chat_id, "Errore: questa chat non è collegata a matrix."); + ex.printStackTrace(System.err); + } + } } @Override @@ -113,7 +165,8 @@ public class TGBot extends TelegramLongPollingBot { try { //Invia il messaggio all'utente - sendMessage(messaggio); + //sendMessage(messaggio); + execute(messaggio); } catch (Exception e) { System.err.println("Errore: " + e); } @@ -124,12 +177,4 @@ public class TGBot extends TelegramLongPollingBot { "Per informazioni: https://github.com/AhabHyde/MiguelBridge"; cEcho(chat_id, stringa); } - - private void echoToMatrix(String testoMessaggio, String sender, String destination) { - try { - matrixBot.sendMessage(sender + ":\n" + testoMessaggio, destination); - } catch (Exception ex) { - Logger.getLogger(TGBot.class.getName()).log(Level.SEVERE, null, ex); - } - } } \ No newline at end of file diff --git a/src/test/MatrixTest.java b/src/test/MatrixTest.java index 70f8640..c60a9c5 100644 --- a/src/test/MatrixTest.java +++ b/src/test/MatrixTest.java @@ -1,6 +1,6 @@ package test; -import com.em.miguelbridge.matrixbot.MatrixBot; +import com.em.miguelbridge.botmatrix.MatrixBot; import java.util.logging.Level; import java.util.logging.Logger; diff --git a/src/test/UploadTest.java b/src/test/UploadTest.java index a94ce04..783e365 100644 --- a/src/test/UploadTest.java +++ b/src/test/UploadTest.java @@ -1,6 +1,6 @@ package test; -import com.em.miguelbridge.matrixbot.MatrixBot; +import com.em.miguelbridge.botmatrix.MatrixBot; import org.json.simple.parser.ParseException; import java.io.File;