diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/juick/api/TelegramBotHook.java | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/src/main/java/com/juick/api/TelegramBotHook.java b/src/main/java/com/juick/api/TelegramBotHook.java index 907898f2..40ff5411 100644 --- a/src/main/java/com/juick/api/TelegramBotHook.java +++ b/src/main/java/com/juick/api/TelegramBotHook.java @@ -34,7 +34,7 @@ public class TelegramBotHook { private static final Logger logger = Logger.getLogger(TelegramBotHook.class.getName()); TelegramBot bot; - WebSocket ws; + WebSocket ws, wsReply; MessageSerializer ms = new MessageSerializer(); JdbcTemplate jdbc; @@ -72,22 +72,54 @@ public class TelegramBotHook { List<Long> users = getSubscribers(jmsg.getUser().getUID()); List<Long> chats = getChats(); // registered subscribed users - users.stream().forEach(c -> telegramNotify(c, msg, jmsg.getMID())); + String msgUrl = "https://juick.com/" + jmsg.getMID(); + users.stream().forEach(c -> telegramNotify(c, msg, msgUrl)); // anonymous - chats.stream().filter(u -> !getUser(u).isPresent()).forEach(c -> telegramNotify(c, msg, jmsg.getMID())); + chats.stream().filter(u -> !getUser(u).isPresent()).forEach(c -> telegramNotify(c, msg, msgUrl)); } }); ws.connect(); + wsReply = new WebSocketFactory().createSocket("wss://ws.juick.com/_replies"); + wsReply.addHeader("Origin", "ws.juick.com"); + wsReply.addHeader("Host", "ws.juick.com"); + wsReply.setPingInterval(60 * 1000); + wsReply.addListener(new WebSocketAdapter() { + @Override + public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception { + logger.info("ws replies disconnected"); + } + + @Override + public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { + logger.info("ws replies connected"); + } + + @Override + public void onTextMessage(WebSocket websocket, String text) throws Exception { + com.juick.Message jmsg = ms.deserialize(new JSONObject(text)); + logger.info(String.format("got jmsg: %s", ms.serialize(jmsg).toString())); + StringBuilder sb = new StringBuilder(); + sb.append("Reply from @").append(jmsg.getUser().getUName()).append(":\n").append(jmsg.getText()).append("\n"); + if (jmsg.getAttachmentURL() != null) { + sb.append(jmsg.getAttachmentURL()); + } + String msg = sb.toString(); + String msgUrl = String.format("https://juick.com/%d#%d", jmsg.getMID(), jmsg.getRID()); + getSubscribersToComments(jmsg.getMID(), jmsg.getUser().getUID()).stream() + .forEach(c -> telegramNotify(c, msg, msgUrl)); + } + }); + wsReply.connect(); } catch (IOException | WebSocketException e) { logger.log(Level.SEVERE, "couldn't create ws connection", e); } } - private void telegramNotify(Long c, String msg, int mid) { + private void telegramNotify(Long c, String msg, String msgUrl) { bot.execute(new SendMessage(c, msg).replyMarkup( new InlineKeyboardMarkup( new InlineKeyboardButton[] { - new InlineKeyboardButton("See on Juick").url("https://juick.com/" + mid) + new InlineKeyboardButton("See on Juick").url(msgUrl) } )), new Callback<SendMessage, SendResponse>() { @Override @@ -156,4 +188,8 @@ public class TelegramBotHook { return jdbc.queryForList("SELECT tg_id FROM telegram INNER JOIN subscr_users " + "ON (subscr_users.user_id=? AND telegram.tg_id=subscr_users.suser_id)", Long.class, uid, uid); } + private List<Long> getSubscribersToComments(int mid, int ignore_uid) { + 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); + } } |