longpolling
This commit is contained in:
parent
6bc04e719a
commit
a8011462f3
|
@ -44,6 +44,15 @@ public class TelegramAPI implements Runnable {
|
||||||
private String apikey;
|
private String apikey;
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
private boolean run = true;
|
private boolean run = true;
|
||||||
|
private boolean longpolling = false;
|
||||||
|
public boolean isLongpolling() {
|
||||||
|
return longpolling;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLongpolling(boolean longpolling) {
|
||||||
|
this.longpolling = longpolling;
|
||||||
|
}
|
||||||
|
|
||||||
private String helpmessage = "generic helppage\nuse TelegramAPI.setHelpText(java.lang.String) to change this.";
|
private String helpmessage = "generic helppage\nuse TelegramAPI.setHelpText(java.lang.String) to change this.";
|
||||||
|
|
||||||
//stats
|
//stats
|
||||||
|
@ -51,7 +60,7 @@ public class TelegramAPI implements Runnable {
|
||||||
protected long start = 0;
|
protected long start = 0;
|
||||||
|
|
||||||
private LinkedList<TUser> users = new LinkedList<>();
|
private LinkedList<TUser> users = new LinkedList<>();
|
||||||
private CommandManager cmdmgr = new CommandManager();
|
private CommandManager cmdmgr = new CommandManager(this);
|
||||||
private EventManager evntmgr = new EventManager();
|
private EventManager evntmgr = new EventManager();
|
||||||
|
|
||||||
//async
|
//async
|
||||||
|
@ -239,7 +248,7 @@ public class TelegramAPI implements Runnable {
|
||||||
|
|
||||||
private void fetchUpdates() {
|
private void fetchUpdates() {
|
||||||
try {
|
try {
|
||||||
processUpdates(request("getUpdates", "offset=" + msg_offset, false));
|
processUpdates(request("getUpdates", "offset=" + msg_offset + "&timeout=" + (longpolling ? updateInterval : 1), true));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.log("error getting updates.", e);
|
log.log("error getting updates.", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,24 @@
|
||||||
package de.mrbesen.telegram.commands;
|
package de.mrbesen.telegram.commands;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.message.TimestampMessage;
|
||||||
|
|
||||||
import com.google.common.collect.ArrayListMultimap;
|
import com.google.common.collect.ArrayListMultimap;
|
||||||
import com.google.common.collect.Multimap;
|
import com.google.common.collect.Multimap;
|
||||||
|
|
||||||
import de.mrbesen.telegram.objects.TMessage;
|
import de.mrbesen.telegram.objects.TMessage;
|
||||||
import de.mrbesen.telegram.objects.TUser;
|
import de.mrbesen.telegram.objects.TUser;
|
||||||
|
import de.mrbesen.telegram.*;
|
||||||
|
|
||||||
public class CommandManager {
|
public class CommandManager {
|
||||||
|
|
||||||
private final static String CMDPATTERN = "^[\\w-]+$";
|
private final static String CMDPATTERN = "^[\\w-]+$";
|
||||||
|
private final TelegramAPI api;
|
||||||
|
|
||||||
private Multimap<String, ICommandHandler> handlerlist = ArrayListMultimap.create();//list of all registered CommandHandler
|
private Multimap<String, ICommandHandler> handlerlist = ArrayListMultimap.create();//list of all registered CommandHandler
|
||||||
|
|
||||||
public CommandManager() { }
|
public CommandManager(TelegramAPI a) {
|
||||||
|
api = a;
|
||||||
|
}
|
||||||
|
|
||||||
public void onCommand(String line, TUser sender, TMessage json) {//called by the api (/-prefix already removed)
|
public void onCommand(String line, TUser sender, TMessage json) {//called by the api (/-prefix already removed)
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
|
@ -46,7 +52,7 @@ public class CommandManager {
|
||||||
}
|
}
|
||||||
//do smth. with result
|
//do smth. with result
|
||||||
if(!result) {
|
if(!result) {
|
||||||
sender.sendHelpPage();
|
TUser.sendMessage(api, json.getChatID(), api.getHelpMessage(), null, 0, true, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,10 +67,6 @@ public class TUser {
|
||||||
return uname;
|
return uname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendHelpPage() {
|
|
||||||
sendMessage(api.getHelpMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean sendMessage(String text) {
|
public boolean sendMessage(String text) {
|
||||||
return sendMessage(text, null);
|
return sendMessage(text, null);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue