error handling
This commit is contained in:
parent
0c6bfe6491
commit
fc1bf1339e
2 changed files with 22 additions and 4 deletions
|
@ -16,6 +16,7 @@ import java.nio.file.Path;
|
|||
@Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!", authors="unnamed")
|
||||
public class VTools {
|
||||
private final ProxyServer server;
|
||||
public final Logger logger;
|
||||
public final Path dataDirectory;
|
||||
|
||||
public static final TextColor COLOR_RED = TextColor.fromCSSHexString("#FF5555");
|
||||
|
@ -25,6 +26,7 @@ public class VTools {
|
|||
@Inject
|
||||
public VTools(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
|
||||
this.server = server;
|
||||
this.logger = logger;
|
||||
this.dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ public class TGBridge {
|
|||
private String TOKEN = "";
|
||||
private long CHAT_ID = 0L;
|
||||
|
||||
private long backoffSec = 1L;
|
||||
|
||||
public TGBridge(VTools plugin) {
|
||||
INSTANCE = this;
|
||||
this.plugin = plugin;
|
||||
|
@ -61,7 +63,7 @@ public class TGBridge {
|
|||
this.TOKEN = config.getOrDefault("token", "");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
plugin.logger.error("parsing config", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,6 +72,7 @@ public class TGBridge {
|
|||
if (TOKEN.isEmpty() || CHAT_ID == 0L) return;
|
||||
bot = new TelegramBot(TOKEN);
|
||||
bot.setUpdatesListener(updates -> {
|
||||
backoffSec = 1L;
|
||||
for (Update update : updates) {
|
||||
try {
|
||||
if (update != null &&
|
||||
|
@ -84,11 +87,19 @@ public class TGBridge {
|
|||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
plugin.logger.error("handling update", e);
|
||||
}
|
||||
}
|
||||
return UpdatesListener.CONFIRMED_UPDATES_ALL;
|
||||
}, Throwable::printStackTrace);
|
||||
}, (e) -> {
|
||||
plugin.logger.error("getting update", e);
|
||||
plugin.logger.error(String.format("waiting %ds before getting another update", backoffSec));
|
||||
try { Thread.sleep(backoffSec * 1000); } catch (InterruptedException ignored) {}
|
||||
backoffSec *= 2L;
|
||||
if (backoffSec > 3600) {
|
||||
backoffSec = 3600;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void inbound(String content) {
|
||||
|
@ -100,23 +111,28 @@ public class TGBridge {
|
|||
}
|
||||
|
||||
protected void outbound(String content) {
|
||||
if (TOKEN.isEmpty() || CHAT_ID == 0L) return;
|
||||
if (bot == null) return;
|
||||
if (content.length() > 4000) {
|
||||
content = content.substring(0, 4000);
|
||||
}
|
||||
bot.execute(new SendMessage(CHAT_ID, content), new Callback<SendMessage, SendResponse>() {
|
||||
@Override
|
||||
public void onResponse(SendMessage sendMessage, SendResponse sendResponse) {
|
||||
if (!sendResponse.isOk()) {
|
||||
plugin.logger.error(String.format("sendMessage error %d: %s", sendResponse.errorCode(), sendResponse.description()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(SendMessage sendMessage, IOException e) {
|
||||
plugin.logger.error("sending message", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onProxyShutdown(ProxyShutdownEvent event) {
|
||||
if (bot == null) return;
|
||||
bot.removeGetUpdatesListener();
|
||||
bot.shutdown();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue