From 966af8fdd48fc0ea17d8893ec711d6d7419c48de Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 3 Nov 2017 14:29:39 +0300 Subject: api: fix NPE in telegram reply processing --- .../com/juick/api/controllers/TelegramWebhook.java | 41 ++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java') 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 94cb81cc..5e253e3c 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 @@ -129,28 +129,33 @@ public class TelegramWebhook { } else { Message replyMessage = message.replyToMessage(); if (replyMessage != null) { - Optional juickLink = Arrays.stream(replyMessage.entities()) - .filter(this::isJuickLink) - .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); + if (replyMessage.entities() != null) { + Optional juickLink = Arrays.stream(replyMessage.entities()) + .filter(this::isJuickLink) + .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 + text, attachment); - telegramBotManager.telegramNotify(message.from().id().longValue(), "Reply sent"); } else { - logger.warn("invalid path: {}", path); + logger.warn("invalid entity: {}", juickLink); } } else { - logger.warn("invalid entity: {}", juickLink); + telegramBotManager.telegramNotify(message.from().id().longValue(), + "Can not reply to this message", message.messageId()); } } else { telegramBotManager.telegramNotify(message.from().id().longValue(), -- cgit v1.2.3