aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-11-18 09:47:24 +0300
committerGravatar Vitaly Takmazov2016-11-18 09:47:24 +0300
commit5af964e05a79e64f02ebcfb3e1b19f109e32deaf (patch)
treef029f2b0c013e9ebba86a0e93c053e9f7a2f7146 /juick-api/src/main/java/com/juick
parent66a03f1c8550ee97f98afd733355ae147a55c023 (diff)
juick-api: Telegram bot refactoring
Diffstat (limited to 'juick-api/src/main/java/com/juick')
-rw-r--r--juick-api/src/main/java/com/juick/api/Main.java4
-rw-r--r--juick-api/src/main/java/com/juick/api/TGBot.java (renamed from juick-api/src/main/java/com/juick/api/TelegramBotHook.java)22
2 files changed, 18 insertions, 8 deletions
diff --git a/juick-api/src/main/java/com/juick/api/Main.java b/juick-api/src/main/java/com/juick/api/Main.java
index d5851c12..6d7deeac 100644
--- a/juick-api/src/main/java/com/juick/api/Main.java
+++ b/juick-api/src/main/java/com/juick/api/Main.java
@@ -71,7 +71,7 @@ public class Main extends HttpServlet {
Others others;
Subscriptions subscriptions;
Notifications notifications;
- TelegramBotHook tgb;
+ TGBot tgb;
SkypeEndpoint sep;
String tmpDir, imgDir;
@@ -92,7 +92,7 @@ public class Main extends HttpServlet {
others = new Others(jdbc);
subscriptions = new Subscriptions(jdbc);
notifications = new Notifications(jdbc);
- tgb = new TelegramBotHook(jdbc, conf.getProperty("telegram_token", ""));
+ tgb = new TGBot(jdbc, conf.getProperty("telegram_token", ""));
sep = new SkypeEndpoint();
setupXmppComponent(conf.getProperty("xmpp_host", "localhost"), Integer.parseInt(conf.getProperty("xmpp_port", "5347")),
conf.getProperty("xmpp_jid", "api.localhost"), conf.getProperty("xmpp_password"));
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java b/juick-api/src/main/java/com/juick/api/TGBot.java
index e089250b..9f573d34 100644
--- a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java
+++ b/juick-api/src/main/java/com/juick/api/TGBot.java
@@ -36,8 +36,8 @@ import static com.juick.formatters.PlainTextFormatter.formatUrl;
/**
* Created by vt on 12/05/16.
*/
-public class TelegramBotHook {
- private static final Logger logger = LoggerFactory.getLogger(TelegramBotHook.class);
+public class TGBot implements AutoCloseable {
+ private static final Logger logger = LoggerFactory.getLogger(TGBot.class);
TelegramBot bot;
WebSocket ws;
@@ -46,7 +46,7 @@ public class TelegramBotHook {
JuickProtocol protocol;
- public TelegramBotHook(JdbcTemplate jdbc, String token) {
+ public TGBot(JdbcTemplate jdbc, String token) {
this.jdbc = jdbc;
bot = TelegramBotAdapter.build(token);
protocol = new JuickProtocol(jdbc, "https://juick.com/");
@@ -58,7 +58,7 @@ public class TelegramBotHook {
@Override
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
logger.info("ws disconnected");
- ws.connect();
+ ws.recreate();
}
@Override
@@ -86,8 +86,7 @@ public class TelegramBotHook {
// get quote
com.juick.Message msg = MessagesQueries.getReply(jdbc, jmsg.getMid(), jmsg.getRid());
String fmsg = formatPost(msg);
- getSubscribersToComments(jmsg.getMid(), jmsg.getUser().getUid()).stream()
- .forEach(c -> telegramNotify(c, fmsg, msgUrl));
+ getSubscribersToComments(jmsg.getMid(), jmsg.getUser().getUid()).forEach(c -> telegramNotify(c, fmsg, msgUrl));
}
}
});
@@ -218,4 +217,15 @@ public class TelegramBotHook {
return jdbc.queryForList("SELECT tg_id FROM telegram INNER JOIN subscr_messages " +
"ON (telegram.user_id=subscr_messages.suser_id) WHERE message_id=? AND suser_id!=?", Long.class, mid, ignore_uid);
}
+
+ @Override
+ public void close() throws Exception {
+ try {
+ if (ws != null) {
+ ws.disconnect();
+ }
+ } catch (Exception e) {
+ logger.warn("websocket disconnection exception", e);
+ }
+ }
}