From 03667020ae95ba855e21cc12a501962095899449 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 3 Nov 2017 13:45:37 +0300 Subject: api: better telegram messages --- .../com/juick/api/controllers/TelegramWebhook.java | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'juick-api/src/main/java/com/juick/api/controllers') 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 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"); } } } -- cgit v1.2.3