aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src/main/java')
-rw-r--r--juick-server/src/main/java/com/juick/server/TelegramBotManager.java26
-rw-r--r--juick-server/src/main/java/com/juick/server/api/webhooks/TelegramWebhook.java11
2 files changed, 26 insertions, 11 deletions
diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
index 3c3fbc9f..012a3e38 100644
--- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
+++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
@@ -29,6 +29,7 @@ import com.juick.util.MessageUtils;
import com.pengrad.telegrambot.BotUtils;
import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot;
+import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.MessageEntity;
import com.pengrad.telegrambot.model.PhotoSize;
@@ -69,6 +70,8 @@ public class TelegramBotManager implements NotificationListener {
@Value("${telegram_token:12345678}")
private String telegramToken;
+ @Value("${telegram_debug:false}")
+ private boolean telegramDebug;
@Inject
private TelegramService telegramService;
@Inject
@@ -91,19 +94,24 @@ public class TelegramBotManager implements NotificationListener {
return;
}
bot = new TelegramBot(telegramToken);
- try {
- SetWebhook webhook = new SetWebhook().url("https://api.juick.com/tlgmbtwbhk");
- if (!bot.execute(webhook).isOk()) {
- logger.error("error setting webhook");
+ if (!telegramDebug) {
+ try {
+ SetWebhook webhook = new SetWebhook().url("https://api.juick.com/tlgmbtwbhk");
+ if (!bot.execute(webhook).isOk()) {
+ logger.error("error setting webhook");
+ }
+ } catch (Exception e) {
+ logger.warn("couldn't initialize telegram bot", e);
}
- } catch (Exception e) {
- logger.warn("couldn't initialize telegram bot", e);
+ } else {
+ bot.setUpdatesListener(updates -> {
+ updates.forEach(this::processUpdate);
+ return UpdatesListener.CONFIRMED_UPDATES_ALL;
+ });
}
}
- public void processUpdate(String data) {
- logger.info("Telegram update: {}", data);
- Update update = BotUtils.parseUpdate(data);
+ public void processUpdate(Update update) {
Message message = update.message();
if (update.message() == null) {
message = update.editedMessage();
diff --git a/juick-server/src/main/java/com/juick/server/api/webhooks/TelegramWebhook.java b/juick-server/src/main/java/com/juick/server/api/webhooks/TelegramWebhook.java
index 10fd1ab3..8497cff1 100644
--- a/juick-server/src/main/java/com/juick/server/api/webhooks/TelegramWebhook.java
+++ b/juick-server/src/main/java/com/juick/server/api/webhooks/TelegramWebhook.java
@@ -18,7 +18,11 @@
package com.juick.server.api.webhooks;
import com.juick.server.TelegramBotManager;
+import com.pengrad.telegrambot.BotUtils;
+import com.pengrad.telegrambot.model.Update;
import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -36,8 +40,9 @@ import java.nio.charset.StandardCharsets;
*/
@ApiIgnore
@RestController
-@ConditionalOnProperty(name = "telegram_token")
+@ConditionalOnProperty({"telegram_token", "telegram_debug"})
public class TelegramWebhook {
+ private static final Logger logger = LoggerFactory.getLogger(TelegramWebhook.class);
@Inject
private TelegramBotManager telegramBotManager;
@@ -45,6 +50,8 @@ public class TelegramWebhook {
@ResponseStatus(value = HttpStatus.OK)
public void processUpdate(InputStream body) throws Exception {
String data = IOUtils.toString(body, StandardCharsets.UTF_8);
- telegramBotManager.processUpdate(data);
+ logger.info("Telegram update: {}", data);
+ Update update = BotUtils.parseUpdate(data);
+ telegramBotManager.processUpdate(update);
}
}