1
1
mirror of https://github.com/ahabhyde/miguelbridge synced 2025-01-10 06:24:20 +01:00

Prima versione di inoltro immagini tg > matrix

Immagine mandata come file, se viene mandata la stessa immagine più volte non viene inviata
This commit is contained in:
Ahab Hyde 2018-04-20 16:59:24 +02:00
parent 2efb16cdfa
commit c7b81d01c3
22 changed files with 92 additions and 53 deletions

8
.idea/artifacts/MiguelBridge_jar.xml generated Normal file
View File

@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" build-on-make="true" name="MiguelBridge:jar">
<output-path>$PROJECT_DIR$/out/artifacts/MiguelBridge_jar</output-path>
<root id="archive" name="MiguelBridge.jar">
<element id="module-output" name="MiguelBridge" />
</root>
</artifact>
</component>

BIN
image.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

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

3
src/META-INF/MANIFEST.MF Normal file
View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.em.miguelbridge.Launcher

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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