aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-05-19 20:14:35 +0300
committerGravatar Vitaly Takmazov2016-05-19 20:14:35 +0300
commit78071c4c2310af611b5297ec1098db568df6a884 (patch)
treef032b275bbcacdde8b088526b7fdab1bf371651f /src/main/java
parent15f02eb428584970a95ce5787299f32b432b9a80 (diff)
parent2b49c5d7215aa5bd75b14101ca48c872b232bc79 (diff)
Merge branch 'tgb' of ssh://den.jabber.ru:2205/var/lib/git/com.juick.api into tgb
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/juick/api/TelegramBotHook.java46
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 0cc3aa84..97418418 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);
+ }
}