aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src/main/java/com/juick/server/TelegramBotManager.java')
-rw-r--r--juick-server/src/main/java/com/juick/server/TelegramBotManager.java71
1 files changed, 43 insertions, 28 deletions
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 2793f906..9e056476 100644
--- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
+++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
@@ -18,7 +18,9 @@
package com.juick.server;
import com.juick.User;
+import com.juick.server.component.LikeEvent;
import com.juick.server.component.MessageEvent;
+import com.juick.server.component.SubscribeEvent;
import com.juick.server.util.HttpUtils;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
@@ -53,10 +55,7 @@ import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.IOException;
import java.net.URL;
-import java.util.Arrays;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Optional;
+import java.util.*;
import static com.juick.formatters.PlainTextFormatter.formatPost;
import static com.juick.formatters.PlainTextFormatter.formatUrl;
@@ -65,7 +64,7 @@ import static com.juick.formatters.PlainTextFormatter.formatUrl;
* Created by vt on 12/05/16.
*/
@Component
-public class TelegramBotManager implements ApplicationListener<MessageEvent> {
+public class TelegramBotManager {
private static final Logger logger = LoggerFactory.getLogger(TelegramBotManager.class);
private TelegramBot bot;
@@ -87,6 +86,10 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> {
private static final String MSG_LINK = "🔗";
+ private ApplicationListener<LikeEvent> onLike;
+ private ApplicationListener<SubscribeEvent> onSubscribe;
+ private ApplicationListener<MessageEvent> onMessage;
+
@PostConstruct
public void init() {
if (StringUtils.isBlank(telegramToken)) {
@@ -102,6 +105,41 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> {
} catch (Exception e) {
logger.warn("couldn't initialize telegram bot", e);
}
+ onMessage = event -> {
+ com.juick.Message jmsg = event.getMessage();
+ String msgUrl = formatUrl(jmsg);
+ if (jmsg.getMid() > 0 && jmsg.getRid() == 0) {
+ String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true));
+
+ List<Long> users = telegramService.getTelegramIdentifiers(subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid()));
+ List<Long> chats = telegramService.getChats();
+ // registered subscribed users
+
+ users.forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg)));
+ // anonymous
+ chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg)));
+ } else if (jmsg.getRid() > 0) {
+ com.juick.Message op = messagesService.getMessage(jmsg.getMid());
+ String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true));
+ telegramService.getTelegramIdentifiers(
+ subscriptionService.getUsersSubscribedToComments(op, jmsg)
+ ).forEach(c -> telegramNotify(c, fmsg, MessageUtils.attachmentUrl(jmsg)));
+ }
+ };
+ onLike = event -> {
+ User liker = event.getUser();
+ com.juick.Message message = event.getMessage();
+ telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser()))
+ .forEach(c -> telegramNotify(c, String.format("%s recommends your post",
+ MessageUtils.getMarkdownUser(liker)),null));
+ };
+ onSubscribe = event -> {
+ User subscriber = event.getUser();
+ User target = event.getToUser();
+ telegramService.getTelegramIdentifiers(Collections.singletonList(target))
+ .forEach(c -> telegramNotify(c, String.format("%s subscribed to your blog",
+ MessageUtils.getMarkdownUser(subscriber)), null));
+ };
}
public void processUpdate(String data) throws Exception {
@@ -215,29 +253,6 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> {
return e.offset() == 0 && e.type().equals(MessageEntity.Type.text_link) && e.length() == 2;
}
- @Override
- public void onApplicationEvent(@Nonnull MessageEvent event) {
- com.juick.Message jmsg = event.getMessage();
- String msgUrl = formatUrl(jmsg);
- if (jmsg.getMid() > 0 && jmsg.getRid() == 0) {
- String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true));
-
- List<Long> users = telegramService.getTelegramIdentifiers(subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid()));
- List<Long> chats = telegramService.getChats();
- // registered subscribed users
-
- users.forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg)));
- // anonymous
- chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg)));
- } else if (jmsg.getRid() > 0) {
- com.juick.Message op = messagesService.getMessage(jmsg.getMid());
- String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true));
- telegramService.getTelegramIdentifiers(
- subscriptionService.getUsersSubscribedToComments(op, jmsg)
- ).forEach(c -> telegramNotify(c, fmsg, MessageUtils.attachmentUrl(jmsg)));
- }
- }
-
public void telegramNotify(Long chatId, String msg, String attachment) {
telegramNotify(chatId, msg, 0, attachment);
}