diff --git a/lib/GSON.jar b/lib/GSON.jar new file mode 100644 index 0000000..9770b7d Binary files /dev/null and b/lib/GSON.jar differ diff --git a/nbproject/project.properties b/nbproject/project.properties index f0bca55..2ffcd0c 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -29,6 +29,7 @@ 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 @@ -37,7 +38,8 @@ jar.compress=false javac.classpath=\ ${file.reference.json-simple-1.1.1.jar}:\ ${file.reference.telegrambots-3.0-jar-with-dependencies.jar}:\ - ${file.reference.httpclient-4.5.5.jar} + ${file.reference.httpclient-4.5.5.jar}:\ + ${file.reference.GSON.jar} # Space-separated list of extra javac options javac.compilerargs= javac.deprecation=false diff --git a/sample_botsettings.json b/sample_botsettings.json index fd3dfcc..ea520fd 100644 --- a/sample_botsettings.json +++ b/sample_botsettings.json @@ -1,14 +1,15 @@ { - "matrixuser": "Matrix user name", - "tgtoken": "Token key given from BotFather", - "matrixpswd": "Matrix user password", - "matrixhomeserver": "https://homeserver.com/_matrix/client/r0", - - "rooms": [{ - "tgname": "Name of the room in Telegram", - "matrixname": "Name of the room in Matrix", - "tgid": "Chat id of the room in Telegram", - "matrixid": "Chat id of the room in Matrix", - "lastmessageid": "event id of the last message sent in this room. DO NOT EDIT" - ]} + "rooms": [ + { + "matrixname": "Name of the room in Matrix", + "tgid": "Chat id of the room in Telegram", + "lastmessageid": "event id of the last message sent in this room. DO NOT EDIT", + "matrixid": "Chat id of the room in Matrix", + "tgname": "Name of the room in Telegram" + } + ], + "matrixhomeserver": "https://homeserver.com/_matrix/client/r0", + "tgtoken": "Token key given from BotFather", + "matrixuser": "Matrix user name", + "matrixpswd": "Matrix user password" } \ No newline at end of file diff --git a/src/com/em/miguelbridge/Launcher.java b/src/com/em/miguelbridge/Launcher.java index c1f88eb..74ec5a7 100644 --- a/src/com/em/miguelbridge/Launcher.java +++ b/src/com/em/miguelbridge/Launcher.java @@ -2,6 +2,10 @@ package com.em.miguelbridge; import com.em.miguelbridge.matrixbot.MatrixBot; import com.em.miguelbridge.telegrambot.TGBot; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; @@ -45,7 +49,14 @@ public class Launcher { matrixBot.setAccessToken(matrixBot.login()); System.out.println("Bot Matrix avviato! " + matrixBot.readBotUserName()); - String roomAddress = "!mPkXwqjuGdhEVSopiG:maxwell.ydns.eu"; + //Joina tutte le room presenti nel json del collegamento + JSONArray rooms = getRooms(); + String roomid = ""; + for (int k=0; k 0) - postParam = postParam.substring(0, postParam.length()-1); - System.out.println(postParam); - - DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); - wr.writeBytes(postParam); - wr.flush(); - wr.close(); - - int status = connection.getResponseCode(); - BufferedReader in = new BufferedReader( - new InputStreamReader(connection.getInputStream())); - String inputLine; - String risp = ""; - while ((inputLine = in.readLine()) != null) { - risp += inputLine; - } - in.close(); - - risposta[0] = "" + status; - risposta[1] = risp; - - return risposta; - } -} diff --git a/src/com/em/miguelbridge/telegrambot/TGBot.java b/src/com/em/miguelbridge/telegrambot/TGBot.java index 7181be8..58c72e2 100644 --- a/src/com/em/miguelbridge/telegrambot/TGBot.java +++ b/src/com/em/miguelbridge/telegrambot/TGBot.java @@ -3,12 +3,15 @@ package com.em.miguelbridge.telegrambot; 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 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.send.*; import org.telegram.telegrambots.api.objects.Update; @@ -33,19 +36,30 @@ public class TGBot extends TelegramLongPollingBot { */ //Controllo per vedere se l'update è un messaggio testuale e che esso non sia vuoto - if (update.hasMessage() && update.getMessage().hasText()) { - //Testo e mittente - String testoMessaggio = update.getMessage().getText(); - String chat_id = "" + update.getMessage().getChatId(); - String sender = update.getMessage().getFrom().getFirstName() + " " - + update.getMessage().getFrom().getLastName(); - - //Per capire qual'è l'id della chat di telegram - //System.out.println(chat_id); - - echoToMatrix(testoMessaggio, sender); + if (update.hasMessage()) { + if (update.getMessage().getText().equalsIgnoreCase("/chatid") || + update.getMessage().getText().equalsIgnoreCase("/chatid@" + getBotUsername())) { + String chat_id = "" + update.getMessage().getChatId(); + cEcho(chat_id, chat_id); + } + else { + //Testo e mittente + String testoMessaggio = update.getMessage().getText(); + String chat_id = "" + update.getMessage().getChatId(); + String sender = update.getMessage().getFrom().getFirstName() + " " + + update.getMessage().getFrom().getLastName(); + String destination; + try { + destination = getDestinationRoom(chat_id); + if (destination == null) + throw new Exception(); + echoToMatrix(testoMessaggio, sender, destination); + } catch (Exception ex) { + cEcho(chat_id, "Errore: questa chat non è collegata a matrix."); + } } } + } @Override public String getBotUsername() { @@ -71,6 +85,17 @@ public class TGBot extends TelegramLongPollingBot { return ""; } + private String getDestinationRoom(String sender_id) throws IOException, FileNotFoundException, ParseException { + //Dalla chat mittente in telegram ritorna l'id della chat di matrix relativa + JSONArray rooms = Launcher.getRooms(); + for (int k=0; k