From 1eabe2af0aa198e723b83a937783242571c8dc65 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 6 Sep 2016 17:21:09 +0300 Subject: telegram: single WebSocket connection, fix reply photos --- .../main/java/com/juick/api/TelegramBotHook.java | 92 ++++++++++------------ 1 file changed, 43 insertions(+), 49 deletions(-) (limited to 'juick-api') diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java b/juick-api/src/main/java/com/juick/api/TelegramBotHook.java index ef78485e..d0a5b177 100644 --- a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java +++ b/juick-api/src/main/java/com/juick/api/TelegramBotHook.java @@ -38,7 +38,7 @@ public class TelegramBotHook { private static final Logger logger = Logger.getLogger(TelegramBotHook.class.getName()); TelegramBot bot; - WebSocket ws, wsReply; + WebSocket ws; MessageSerializer ms = new MessageSerializer(); JdbcTemplate jdbc; JuickProtocol protocol; @@ -50,9 +50,7 @@ public class TelegramBotHook { protocol = new JuickProtocol(jdbc, "https://juick.com/"); try { bot.setWebhook("https://api.juick.com/tlgmbtwbhk"); - ws = new WebSocketFactory().createSocket("wss://ws.juick.com/_all"); - ws.addHeader("Origin", "ws.juick.com"); - ws.addHeader("Host", "ws.juick.com"); //TODO: remove from server + ws = new WebSocketFactory().createSocket("wss://ws.juick.com/"); ws.setPingInterval(60 * 1000); ws.addListener(new WebSocketAdapter() { @Override @@ -71,61 +69,57 @@ public class TelegramBotHook { super.onTextMessage(websocket, text); com.juick.Message jmsg = ms.deserialize(new JSONObject(text)); logger.info("got jmsg: " + ms.serialize(jmsg).toString()); - StringBuilder sb = new StringBuilder(); - sb.append("@").append(jmsg.getUser().getUName()).append(":\n") - .append(jmsg.getTagsString()).append("\n").append(jmsg.getText()).append("\n"); - if (jmsg.Photo != null) { - sb.append(jmsg.Photo); - } - String msg = sb.toString(); - List users = getSubscribers(jmsg.getUser().getUID()); - List chats = getChats(); - // registered subscribed users - String msgUrl = "https://juick.com/" + jmsg.getMID(); - users.stream().forEach(c -> telegramNotify(c, msg, msgUrl)); - // anonymous - chats.stream().filter(u -> getUser(u) == 0).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"); - ws.connect(); - } + String msgUrl = formatUrl(jmsg); + if (jmsg.getRID() == 0) { + String msg = formatPost(jmsg); - @Override - public void onConnected(WebSocket websocket, Map> headers) throws Exception { - logger.info("ws replies connected"); - } + List users = getSubscribers(jmsg.getUser().getUID()); + List chats = getChats(); + // registered subscribed users - @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 by @").append(jmsg.getUser().getUName()).append(":\n") - .append(getReplyQuote(jmsg.getMID(), jmsg.ReplyTo)).append("\n").append(jmsg.getText()); - if (jmsg.getAttachmentURL() != null) { - sb.append("\n").append(jmsg.getAttachmentURL()); + users.forEach(c -> telegramNotify(c, msg, msgUrl)); + // anonymous + chats.stream().filter(u -> getUser(u) == 0).forEach(c -> telegramNotify(c, msg, msgUrl)); + } else { + String msg = formatReply(jmsg); + getSubscribersToComments(jmsg.getMID(), jmsg.getUser().getUID()).stream() + .forEach(c -> telegramNotify(c, msg, msgUrl)); } - 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(); + ws.connect(); } catch (Exception e) { logger.log(Level.WARNING, "couldn't initialize telegram bot", e); } } + private String formatPost(com.juick.Message jmsg) { + StringBuilder sb = new StringBuilder(); + sb.append("@").append(jmsg.getUser().getUName()).append(":\n") + .append(jmsg.getTagsString()).append("\n").append(jmsg.getText()).append("\n"); + if (jmsg.Photo != null) { + sb.append(jmsg.Photo); + } + return sb.toString(); + } + private String formatReply(com.juick.Message jmsg) { + StringBuilder sb = new StringBuilder(); + sb.append("Reply by @").append(jmsg.getUser().getUName()).append(":\n") + .append(getReplyQuote(jmsg.getMID(), jmsg.ReplyTo)).append("\n") + .append(jmsg.getText()).append("\n"); + if (jmsg.Photo != null) { + sb.append(jmsg.Photo); + } + return sb.toString(); + } + + private String formatUrl(com.juick.Message jmsg) { + if (jmsg.getRID() > 0) { + return String.format("https://juick.com/%d#%d", jmsg.getMID(), jmsg.getRID()); + } + return "https://juick.com/" + jmsg.getMID(); + } + private void telegramNotify(Long c, String msg, String msgUrl) { SendMessage telegramMessage = new SendMessage(c, msg); if (msgUrl != null) { -- cgit v1.2.3