aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/TelegramBotManager.java')
-rw-r--r--juick-api/src/main/java/com/juick/api/TelegramBotManager.java38
1 files changed, 5 insertions, 33 deletions
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
index 0516af5f..1611c23c 100644
--- a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
+++ b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
@@ -19,6 +19,7 @@ package com.juick.api;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.server.component.DisconnectedEvent;
+import com.juick.server.component.MessageEvent;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.TelegramService;
@@ -35,6 +36,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
@@ -55,7 +57,7 @@ import static com.juick.formatters.PlainTextFormatter.formatUrl;
* Created by vt on 12/05/16.
*/
@Component
-public class TelegramBotManager extends TextWebSocketHandler {
+public class TelegramBotManager implements ApplicationListener<MessageEvent> {
private static final Logger logger = LoggerFactory.getLogger(TelegramBotManager.class);
private TelegramBot bot;
@@ -68,12 +70,6 @@ public class TelegramBotManager extends TextWebSocketHandler {
private MessagesService messagesService;
@Inject
private SubscriptionService subscriptionService;
- @Inject
- private ObjectMapper jsonMapper;
- @Inject
- private ApplicationEventPublisher applicationEventPublisher;
-
- private WebSocketSession session;
public static final String MSG_LINK = "🔗";
@@ -94,35 +90,11 @@ public class TelegramBotManager extends TextWebSocketHandler {
}
}
- @Override
- public void afterConnectionEstablished(WebSocketSession session) throws Exception {
- logger.info("WebSocket connected");
- this.session = session;
- }
- @Override
- public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
- logger.info("WebSocket disconnected with code {}: {}", status.getCode(), status.getReason());
- applicationEventPublisher.publishEvent(new DisconnectedEvent(this));
- }
-
- @Scheduled(fixedRate = 30000, initialDelay = 30000)
- public void ping() throws IOException {
- if (session != null && session.isOpen()) {
- logger.debug("Sending WebSocket ping");
- session.sendMessage(new PingMessage());
- } else {
- applicationEventPublisher.publishEvent(new DisconnectedEvent(this));
- }
- }
@Override
- protected void handleTextMessage(WebSocketSession session, TextMessage text) throws Exception {
- com.juick.Message jmsg = jsonMapper.readValue(text.asBytes(), com.juick.Message.class);
-
- if (logger.isInfoEnabled()) // prevent writeValueAsString execution if logger disabled
- logger.info("got jmsg: {}", jsonMapper.writeValueAsString(jmsg));
-
+ public void onApplicationEvent(MessageEvent event) {
+ com.juick.Message jmsg = event.getMessage();
String msgUrl = formatUrl(jmsg);
if (jmsg.getRid() == 0) {
String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg));