From 80f6a6eb734fa8f07dbc1e89d4bde8b1db11d07d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 3 Nov 2017 13:06:31 +0300 Subject: api: clean up telegram reply parsing --- .../com/juick/api/controllers/TelegramWebhook.java | 29 +++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'juick-api/src/main/java/com/juick/api') 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 236e4064..137e1a59 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 @@ -25,6 +25,7 @@ import com.juick.service.TelegramService; import com.juick.service.UserService; import com.pengrad.telegrambot.BotUtils; import com.pengrad.telegrambot.model.Message; +import com.pengrad.telegrambot.model.MessageEntity; import com.pengrad.telegrambot.model.PhotoSize; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.GetFile; @@ -131,19 +132,20 @@ public class TelegramWebhook { String finalAttachment = attachment; Message finalMessage = message; Arrays.stream(replyMessage.entities()) - .filter(e -> e.offset() == 0).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); - } - apiServer.processMessage(user_from, prefix + finalText, finalAttachment); - telegramBotManager.telegramNotify(finalMessage.from().id().longValue(), "reply sent"); + .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); } + apiServer.processMessage(user_from, prefix + finalText, finalAttachment); + telegramBotManager.telegramNotify(finalMessage.from().id().longValue(), "reply sent"); + } }); } else { apiServer.processMessage(user_from, text, attachment); @@ -154,4 +156,7 @@ public class TelegramWebhook { } } } + boolean isJuickLink(MessageEntity e) { + return e.offset() == 0 && e.type().equals(MessageEntity.Type.text_link) && e.length() == 2; + } } -- cgit v1.2.3