From 2a5753cb32dd184d66ee28d38881c9135f8526c4 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 21 Feb 2018 23:52:11 +0300 Subject: server: send attachments to telegram as photos --- .../java/com/juick/server/TelegramBotManager.java | 114 +++++++++++++-------- 1 file changed, 69 insertions(+), 45 deletions(-) (limited to 'juick-server/src/main/java') 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 564a2255..de54c3ce 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -24,6 +24,7 @@ import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.TelegramService; import com.juick.service.UserService; +import com.juick.util.MessageUtils; import com.pengrad.telegrambot.BotUtils; import com.pengrad.telegrambot.Callback; import com.pengrad.telegrambot.TelegramBot; @@ -34,6 +35,7 @@ import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.GetFile; import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.request.SendPhoto; import com.pengrad.telegrambot.request.SetWebhook; import com.pengrad.telegrambot.response.GetFileResponse; import com.pengrad.telegrambot.response.SendResponse; @@ -159,7 +161,7 @@ public class TelegramBotManager implements ApplicationListener { String msgUrl = "http://juick.com/login?hash=" + userService.getHashByUID(user_from.getUid()); String msg = String.format("Hi, %s!\nYou can post messages and images to Juick there.\n" + "Tap to [log into website](%s) to get more info", user_from.getName(), msgUrl); - telegramNotify(message.from().id().longValue(), msg); + telegramNotify(message.from().id().longValue(), msg, StringUtils.EMPTY); } else { Message replyMessage = message.replyToMessage(); if (replyMessage != null) { @@ -180,7 +182,7 @@ public class TelegramBotManager implements ApplicationListener { prefix = String.format("#%d/%d ", mid, rid); } serverManager.processMessage(user_from, prefix + text, attachment); - telegramNotify(message.from().id().longValue(), "Reply sent"); + telegramNotify(message.from().id().longValue(), "Reply sent", StringUtils.EMPTY); } else { logger.warn("invalid path: {}", path); } @@ -189,15 +191,15 @@ public class TelegramBotManager implements ApplicationListener { } } else { telegramNotify(message.from().id().longValue(), - "Can not reply to this message", replyMessage.messageId()); + "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); } } else { telegramNotify(message.from().id().longValue(), - "Can not reply to this message", replyMessage.messageId()); + "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); } } else { serverManager.processMessage(user_from, text, attachment); - telegramNotify(message.from().id().longValue(), "Message sent"); + telegramNotify(message.from().id().longValue(), "Message sent", StringUtils.EMPTY); } } } @@ -220,62 +222,84 @@ public class TelegramBotManager implements ApplicationListener { List chats = telegramService.getChats(); // registered subscribed users - users.forEach(c -> telegramNotify(c, msg)); + users.forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); // anonymous - chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg)); + chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); } else if (jmsg.getRid() > 0) { // get quote com.juick.Message msg = messagesService.getReply(jmsg.getMid(), jmsg.getRid()); String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(msg, true)); telegramService.getTelegramIdentifiers( subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid()) - ).forEach(c -> telegramNotify(c, fmsg)); + ).forEach(c -> telegramNotify(c, fmsg, MessageUtils.attachmentUrl(jmsg))); } } - public void telegramNotify(Long chatId, String msg) { - telegramNotify(chatId, msg, 0); + public void telegramNotify(Long chatId, String msg, String attachment) { + telegramNotify(chatId, msg, 0, attachment); } - public void telegramNotify(Long chatId, String msg, Integer replyTo) { - SendMessage telegramMessage = new SendMessage(chatId, msg); - if (replyTo > 0) { - telegramMessage.replyToMessageId(replyTo); - } - telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true); - bot.execute(telegramMessage, new Callback() { - @Override - public void onResponse(SendMessage request, SendResponse response) { - if (!response.isOk()) { - if (response.errorCode() == 403) { - // remove from anonymous chat - telegramService.getChats().stream().filter(c -> c.equals(chatId)).findFirst().ifPresent( - d -> { - telegramService.deleteChat(d); - logger.info("deleted {} chat", d); - } - ); - int userId = telegramService.getUser(chatId); - if (userId > 0) { - User userToDelete = userService.getUserByUID(userId) - .orElse(new User()); - boolean status = telegramService.deleteTelegramUser(userToDelete.getUid()); - logger.info("deleting telegram id of @{} : {}", userToDelete.getName(), status); - boolean chatStatus = telegramService.deleteChat(chatId); - logger.info("deleting telegram chat {} : {}", chatId, chatStatus); - } - } else { - logger.warn("error response, isOk: {}, errorCode: {}, description: {}", - response.isOk(), response.errorCode(), response.description()); - } + public void telegramNotify(Long chatId, String msg, Integer replyTo, String attachment) { + if (StringUtils.isEmpty(attachment)) { + SendMessage telegramMessage = new SendMessage(chatId, msg); + if (replyTo > 0) { + telegramMessage.replyToMessageId(replyTo); + } + telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true); + bot.execute(telegramMessage, new Callback() { + @Override + public void onResponse(SendMessage request, SendResponse response) { + processTelegramResponse(chatId, response); + } + @Override + public void onFailure(SendMessage request, IOException e) { + logger.warn("telegram failure", e); } + }); + } else { + SendPhoto telegramPhoto = new SendPhoto(chatId, attachment); + telegramPhoto.caption(msg); + if (replyTo > 0) { + telegramPhoto.replyToMessageId(replyTo); } + telegramPhoto.parseMode(ParseMode.Markdown); + bot.execute(telegramPhoto, new Callback() { + @Override + public void onResponse(SendPhoto request, SendResponse response) { + processTelegramResponse(chatId, response); + } + @Override + public void onFailure(SendPhoto request, IOException e) { + logger.warn("telegram failure", e); + } + }); + } + } - @Override - public void onFailure(SendMessage request, IOException e) { - logger.warn("telegram failure", e); + private void processTelegramResponse(Long chatId, SendResponse response) { + if (!response.isOk()) { + if (response.errorCode() == 403) { + // remove from anonymous chat + telegramService.getChats().stream().filter(c -> c.equals(chatId)).findFirst().ifPresent( + d -> { + telegramService.deleteChat(d); + logger.info("deleted {} chat", d); + } + ); + int userId = telegramService.getUser(chatId); + if (userId > 0) { + User userToDelete = userService.getUserByUID(userId) + .orElse(new User()); + boolean status = telegramService.deleteTelegramUser(userToDelete.getUid()); + logger.info("deleting telegram id of @{} : {}", userToDelete.getName(), status); + boolean chatStatus = telegramService.deleteChat(chatId); + logger.info("deleting telegram chat {} : {}", chatId, chatStatus); + } + } else { + logger.warn("error response, isOk: {}, errorCode: {}, description: {}", + response.isOk(), response.errorCode(), response.description()); } - }); + } } public void telegramSignupNotify(Long telegramId, String hash) { -- cgit v1.2.3