diff options
author | Vitaly Takmazov | 2017-11-03 13:45:37 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-11-03 13:45:37 +0300 |
commit | 03667020ae95ba855e21cc12a501962095899449 (patch) | |
tree | 47229939312737d6bb79d2f784d97831df837f14 | |
parent | 80f6a6eb734fa8f07dbc1e89d4bde8b1db11d07d (diff) |
api: better telegram messages
-rw-r--r-- | juick-api/src/main/java/com/juick/api/TelegramBotManager.java | 11 | ||||
-rw-r--r-- | juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java | 43 |
2 files changed, 35 insertions, 19 deletions
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java index bdf8bc83..73b8c3a8 100644 --- a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java +++ b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java @@ -106,8 +106,15 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> { } } - public void telegramNotify(Long c, String msg) { - SendMessage telegramMessage = new SendMessage(c, msg); + public void telegramNotify(Long chatId, String msg) { + telegramNotify(chatId, msg, 0); + } + + public void telegramNotify(Long chatId, String msg, Integer replyTo) { + SendMessage telegramMessage = new SendMessage(chatId, msg); + if (replyTo > 0) { + telegramMessage.replyToMessageId(replyTo); + } telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true); bot.execute(telegramMessage, new Callback<SendMessage, SendResponse>() { @Override diff --git a/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java b/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java index 137e1a59..94cb81cc 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java +++ b/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java @@ -52,6 +52,7 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Comparator; import java.util.List; +import java.util.Optional; /** * Created by vt on 24/11/2016. @@ -128,28 +129,36 @@ public class TelegramWebhook { } else { Message replyMessage = message.replyToMessage(); if (replyMessage != null) { - String finalText = text; - String finalAttachment = attachment; - Message finalMessage = message; - Arrays.stream(replyMessage.entities()) + Optional<MessageEntity> juickLink = Arrays.stream(replyMessage.entities()) .filter(this::isJuickLink) - .findFirst().ifPresent(e -> { - if (StringUtils.isNotEmpty(e.url())) { - UriComponents uriComponents = UriComponentsBuilder.fromUriString(e.url()).build(); - int mid = Integer.valueOf(uriComponents.getPath().substring(1)); - String prefix = String.format("#%d ", mid); - int rid = 0; - if (StringUtils.isNotEmpty(uriComponents.getFragment())) { - rid = Integer.valueOf(uriComponents.getFragment()); - prefix = String.format("#%d/%d ", mid, rid); + .findFirst(); + if (juickLink.isPresent()) { + if (StringUtils.isNotEmpty(juickLink.get().url())) { + UriComponents uriComponents = UriComponentsBuilder.fromUriString( + juickLink.get().url()).build(); + String path = uriComponents.getPath(); + if (StringUtils.isNotEmpty(path) && path.length() > 1) { + int mid = Integer.valueOf(path.substring(1)); + String prefix = String.format("#%d ", mid); + if (StringUtils.isNotEmpty(uriComponents.getFragment())) { + int rid = Integer.valueOf(uriComponents.getFragment()); + prefix = String.format("#%d/%d ", mid, rid); + } + apiServer.processMessage(user_from, prefix + text, attachment); + telegramBotManager.telegramNotify(message.from().id().longValue(), "Reply sent"); + } else { + logger.warn("invalid path: {}", path); } - apiServer.processMessage(user_from, prefix + finalText, finalAttachment); - telegramBotManager.telegramNotify(finalMessage.from().id().longValue(), "reply sent"); + } else { + logger.warn("invalid entity: {}", juickLink); } - }); + } else { + telegramBotManager.telegramNotify(message.from().id().longValue(), + "Can not reply to this message", message.messageId()); + } } else { apiServer.processMessage(user_from, text, attachment); - telegramBotManager.telegramNotify(message.from().id().longValue(), "message sent"); + telegramBotManager.telegramNotify(message.from().id().longValue(), "Message sent"); } } } |