From 3ea4cd1942fa4e763034da11c5fa429407b67829 Mon Sep 17 00:00:00 2001 From: alx Date: Sat, 16 Mar 2019 23:55:55 +0300 Subject: Send large image caption as separated message via telegram --- .../java/com/juick/server/TelegramBotManager.java | 36 ++++++++++++---------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main/java/com/juick/server/TelegramBotManager.java b/src/main/java/com/juick/server/TelegramBotManager.java index 8a0c45dc..56b5b4f9 100644 --- a/src/main/java/com/juick/server/TelegramBotManager.java +++ b/src/main/java/com/juick/server/TelegramBotManager.java @@ -257,39 +257,43 @@ public class TelegramBotManager implements NotificationListener { public void telegramNotify(Long chatId, String msg, Integer replyTo, @Nonnull com.juick.Message source) { String attachment = MessageUtils.attachmentUrl(source); - if (StringUtils.isEmpty(attachment)) { - SendMessage telegramMessage = new SendMessage(chatId, msg); + boolean isSendTxt = true; + if (!StringUtils.isEmpty(attachment)) { + SendPhoto telegramPhoto = new SendPhoto(chatId, attachment); if (replyTo > 0) { - telegramMessage.replyToMessageId(replyTo); + telegramPhoto.replyToMessageId(replyTo); } - telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true); - bot.execute(telegramMessage, new Callback() { + telegramPhoto.parseMode(ParseMode.Markdown); + if(msg.length() < 1024) { + telegramPhoto.caption(msg); + isSendTxt = false; + } + bot.execute(telegramPhoto, new Callback() { @Override - public void onResponse(SendMessage request, SendResponse response) { + public void onResponse(SendPhoto request, SendResponse response) { processTelegramResponse(chatId, response, source); } @Override - public void onFailure(SendMessage request, IOException e) { + public void onFailure(SendPhoto request, IOException e) { logger.warn("telegram failure", e); } }); - } else { - SendPhoto telegramPhoto = new SendPhoto(chatId, attachment); - String trimmedPost = msg.length() > 1024 ? msg.substring(0, 1023) + "..." : msg; - telegramPhoto.caption(trimmedPost); + } + if (isSendTxt){ + SendMessage telegramMessage = new SendMessage(chatId, msg); if (replyTo > 0) { - telegramPhoto.replyToMessageId(replyTo); + telegramMessage.replyToMessageId(replyTo); } - telegramPhoto.parseMode(ParseMode.Markdown); - bot.execute(telegramPhoto, new Callback() { + telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true); + bot.execute(telegramMessage, new Callback() { @Override - public void onResponse(SendPhoto request, SendResponse response) { + public void onResponse(SendMessage request, SendResponse response) { processTelegramResponse(chatId, response, source); } @Override - public void onFailure(SendPhoto request, IOException e) { + public void onFailure(SendMessage request, IOException e) { logger.warn("telegram failure", e); } }); -- cgit v1.2.3