From 94d52e4e4022678bce895c4a567d1c9c9d01560a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Apr 2022 18:49:56 +0300 Subject: Telegram: send top messages to the channel --- src/main/java/com/juick/TelegramBotManager.java | 47 +++++++++++++++++-------- 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'src/main/java/com') diff --git a/src/main/java/com/juick/TelegramBotManager.java b/src/main/java/com/juick/TelegramBotManager.java index abed77fc..eb8c2b83 100644 --- a/src/main/java/com/juick/TelegramBotManager.java +++ b/src/main/java/com/juick/TelegramBotManager.java @@ -82,6 +82,8 @@ public class TelegramBotManager implements NotificationListener { private String telegramToken; @Value("${telegram_debug:false}") private boolean telegramDebug; + @Value("${telegram_top_channel:@JuickTop}") + private String topChannelName; @Inject private TelegramService telegramService; @Inject @@ -291,11 +293,11 @@ public class TelegramBotManager implements NotificationListener { return e.offset() == 0 && e.type().equals(MessageEntity.Type.text_link) && e.length() == 2; } - public void telegramNotify(Long chatId, String msg, @Nonnull com.juick.model.Message source) { + public void telegramNotify(Object chatId, String msg, @Nonnull com.juick.model.Message source) { telegramNotify(chatId, msg, 0, source); } - public void telegramNotify(Long chatId, String msg, Integer replyTo, @Nonnull com.juick.model.Message source) { + public void telegramNotify(Object chatId, String msg, Integer replyTo, @Nonnull com.juick.model.Message source) { String attachment = MessageUtils.attachmentUrl(source); boolean isSendTxt = true; if (!StringUtils.isEmpty(attachment)) { @@ -311,7 +313,11 @@ public class TelegramBotManager implements NotificationListener { bot.execute(telegramPhoto, new Callback<>() { @Override public void onResponse(SendPhoto request, SendResponse response) { - processTelegramResponse(chatId, response, source); + if (chatId instanceof Long) { + processTelegramResponse((Long)chatId, response, source); + } else { + processTelegramChannelResponse((String)chatId, response, source); + } } @Override @@ -329,7 +335,11 @@ public class TelegramBotManager implements NotificationListener { bot.execute(telegramMessage, new Callback<>() { @Override public void onResponse(SendMessage request, SendResponse response) { - processTelegramResponse(chatId, response, source); + if (chatId instanceof Long) { + processTelegramResponse((Long)chatId, response, source); + } else { + processTelegramChannelResponse((String)chatId, response, source); + } } @Override @@ -373,6 +383,13 @@ public class TelegramBotManager implements NotificationListener { } } + private void processTelegramChannelResponse(String channelId, SendResponse response, com.juick.model.Message source) { + if (!response.isOk()) { + logger.warn("error response from channel, isOk: {}, errorCode: {}, description: {}, Juick message: {}", + response.isOk(), response.errorCode(), response.description(), source.getMid()); + } + } + public void telegramSignupNotify(Long telegramId, String hash) { bot.execute(new SendMessage(telegramId, String.format("You are subscribed to all Juick messages. " + @@ -407,6 +424,9 @@ public class TelegramBotManager implements NotificationListener { processFollow(activity.getFrom(), activity.getTo()); } } + private String formatTelegramMessage(com.juick.model.Message jmsg) { + return String.format("[%s](%s) %s", MSG_LINK, formatUrl(jmsg), formatPost(jmsg, true)); + } private void processMessage(com.juick.model.Message jmsg, List subscribedUsers) { if (jmsg.isService()) { return; @@ -421,7 +441,7 @@ public class TelegramBotManager implements NotificationListener { subscribedUsers ).forEach(c -> telegramNotify(c, fmsg, jmsg)); } else { - String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true)); + String msg = formatTelegramMessage(jmsg); List users = telegramService.getTelegramIdentifiers(subscribedUsers); List chats = telegramService.getAnonymous(); @@ -434,17 +454,15 @@ public class TelegramBotManager implements NotificationListener { } private void processLike(User liker, com.juick.model.Message message, List subscribers) { - if (!liker.equals(serviceUser)) { - logger.info("Like received in tg listener"); - if (!userService.isInBLAny(message.getUser().getUid(), liker.getUid())) { - telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) - .forEach(c -> telegramNotify(c, String.format("%s recommends your [post](%s)", - MessageUtils.getMarkdownUser(liker), formatUrl(message)), new com.juick.model.Message())); - } - telegramService.getTelegramIdentifiers(subscribers) - .forEach(c -> telegramNotify(c, String.format("%s recommends you someone's [post](%s)", + logger.info("Like received in tg listener"); + if (!userService.isInBLAny(message.getUser().getUid(), liker.getUid())) { + telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) + .forEach(c -> telegramNotify(c, String.format("%s recommends your [post](%s)", MessageUtils.getMarkdownUser(liker), formatUrl(message)), new com.juick.model.Message())); } + telegramService.getTelegramIdentifiers(subscribers) + .forEach(c -> telegramNotify(c, String.format("%s recommends you someone's [post](%s)", + MessageUtils.getMarkdownUser(liker), formatUrl(message)), new com.juick.model.Message())); } @Override @@ -453,6 +471,7 @@ public class TelegramBotManager implements NotificationListener { } private void processTop(com.juick.model.Message message) { + telegramNotify(topChannelName, formatTelegramMessage(message), message); telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) .forEach(c -> telegramNotify(c, String.format("Your [post](%s) became popular!", formatUrl(message)), new com.juick.model.Message())); -- cgit v1.2.3