From 53586f7dc92c47bf45e33c2cd086a32a8da1a250 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 7 Apr 2018 17:33:16 +0300 Subject: server: handle telegram webhook exceptions --- .../java/com/juick/server/TelegramBotManager.java | 36 +++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'juick-server/src/main/java/com/juick') 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 b0803265..e1bdad1f 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -104,7 +104,7 @@ public class TelegramBotManager implements NotificationListener { } } - public void processUpdate(String data) throws Exception { + public void processUpdate(String data) { Update update = BotUtils.parseUpdate(data); Message message = update.message(); if (update.message() == null) { @@ -139,8 +139,12 @@ public class TelegramBotManager implements NotificationListener { GetFile request = new GetFile(fileId); GetFileResponse response = bot.execute(request); logger.info("got file {}", response.file()); - URL fileURL = new URL(bot.getFullFilePath(response.file())); - attachment = HttpUtils.downloadImage(fileURL, tmpDir); + try { + URL fileURL = new URL(bot.getFullFilePath(response.file())); + attachment = HttpUtils.downloadImage(fileURL, tmpDir); + } catch (Exception e) { + logger.warn("attachment exception", e); + } logger.info("received {}", attachment); } } @@ -185,10 +189,15 @@ public class TelegramBotManager implements NotificationListener { int rid = Integer.valueOf(uriComponents.getFragment()); prefix = String.format("#%d/%d ", mid, rid); } - CommandResult result = commandsManager.processCommand(user_from, prefix + text, attachment); - String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() - : "Unknown error or unsupported command"; - telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); + CommandResult result = null; + try { + result = commandsManager.processCommand(user_from, prefix + text, attachment); + String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() + : "Unknown error or unsupported command"; + telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); + } catch (Exception e) { + logger.warn("telegram exception", e); + } } else { logger.warn("invalid path: {}", path); } @@ -204,10 +213,15 @@ public class TelegramBotManager implements NotificationListener { "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); } } else { - CommandResult result = commandsManager.processCommand(user_from, text, attachment); - String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() - : "Unknown error or unsupported command"; - telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); + CommandResult result = null; + try { + result = commandsManager.processCommand(user_from, text, attachment); + String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() + : "Unknown error or unsupported command"; + telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); + } catch (Exception e) { + logger.warn("telegram reply exception", e); + } } } } -- cgit v1.2.3