diff --git a/src/main/java/de/mrbesen/telegram/TelegramAPI.java b/src/main/java/de/mrbesen/telegram/TelegramAPI.java index ca96745..cdf3b88 100644 --- a/src/main/java/de/mrbesen/telegram/TelegramAPI.java +++ b/src/main/java/de/mrbesen/telegram/TelegramAPI.java @@ -44,6 +44,7 @@ public class TelegramAPI implements Runnable { private int msg_offset = 0; private int updateInterval = 1500; private String apikey; + private String botname; private Thread thread; private boolean run = true; private boolean longpolling = false; @@ -80,7 +81,21 @@ public class TelegramAPI implements Runnable { Log log = new Log4JLog(); + /** + * please only use if bot is not used in groups + * @param apikey + */ public TelegramAPI(String apikey) { + botname = ""; + if (!apikey.matches(TOKENREGEX) ) { + throw new IllegalArgumentException("Invalid API key: " + apikey); + } + this.apikey = apikey; + } + + public TelegramAPI(String apikey, String botname) { + this.botname = botname; + if (!apikey.matches(TOKENREGEX) ) { throw new IllegalArgumentException("Invalid API key: " + apikey); } @@ -409,10 +424,22 @@ public class TelegramAPI implements Runnable { String text = msg.getText(); if(text != null) { - if(text.startsWith("/" )) { + if(text.matches("^\\/(\\w*)(@(\\w*))?")) { text = text.substring(1);//remove '/' isCommand = true; - cmdmgr.onCommand(text, msg.getFrom(), msg); + if(text.contains("@")) {//check name + int at = text.indexOf('@'); + int end = text.indexOf(' ', at+1); + if(end == -1) + end = text.length(); + String botname = text.substring(at+1, end); + if(botname.equalsIgnoreCase(api.botname)) { + cmdmgr.onCommand(text, msg.getFrom(), msg); + } else + api.log.log("other botname found: " + botname); + } else { + cmdmgr.onCommand(text, msg.getFrom(), msg); + } } else { Event e = null; if(getMsg().has(Member.audio)) {