From cc0a29650d3ba9b848a86cce73fbe6e3ff5d8693 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 19 May 2016 19:46:31 +0300 Subject: telegram: notify subcribed users --- src/main/java/com/juick/api/TelegramBotHook.java | 57 ++++++++++++++++-------- 1 file changed, 39 insertions(+), 18 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/api/TelegramBotHook.java b/src/main/java/com/juick/api/TelegramBotHook.java index c68d829a..907898f2 100644 --- a/src/main/java/com/juick/api/TelegramBotHook.java +++ b/src/main/java/com/juick/api/TelegramBotHook.java @@ -1,5 +1,6 @@ package com.juick.api; +import com.juick.User; import com.juick.json.MessageSerializer; import com.juick.server.UserQueries; import com.neovisionaries.ws.client.*; @@ -13,6 +14,7 @@ import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.response.SendResponse; import org.json.JSONObject; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.http.HttpServletRequest; @@ -20,6 +22,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -66,25 +69,12 @@ public class TelegramBotHook { sb.append(jmsg.getAttachmentURL()); } String msg = sb.toString(); + List users = getSubscribers(jmsg.getUser().getUID()); List chats = getChats(); - chats.stream().forEach(c -> { - bot.execute(new SendMessage(c, msg).replyMarkup( - new InlineKeyboardMarkup( - new InlineKeyboardButton[] { - new InlineKeyboardButton("See on Juick").url("https://juick.com/" + jmsg.getMID()) - } - )), new Callback() { - @Override - public void onResponse(SendMessage request, SendResponse response) { - logger.info("got response: " + response.message().toString()); - } - - @Override - public void onFailure(SendMessage request, IOException e) { - logger.log(Level.WARNING, "telegram failure", e); - } - }); - }); + // registered subscribed users + users.stream().forEach(c -> telegramNotify(c, msg, jmsg.getMID())); + // anonymous + chats.stream().filter(u -> !getUser(u).isPresent()).forEach(c -> telegramNotify(c, msg, jmsg.getMID())); } }); ws.connect(); @@ -93,6 +83,25 @@ public class TelegramBotHook { } } + private void telegramNotify(Long c, String msg, int mid) { + bot.execute(new SendMessage(c, msg).replyMarkup( + new InlineKeyboardMarkup( + new InlineKeyboardButton[] { + new InlineKeyboardButton("See on Juick").url("https://juick.com/" + mid) + } + )), new Callback() { + @Override + public void onResponse(SendMessage request, SendResponse response) { + logger.info("got response: " + response.message().toString()); + } + + @Override + public void onFailure(SendMessage request, IOException e) { + logger.log(Level.WARNING, "telegram failure", e); + } + }); + } + List getChats() { return jdbc.queryForList("SELECT chat_id FROM telegram_chats", Long.class); } @@ -135,4 +144,16 @@ public class TelegramBotHook { return jdbc.update("INSERT INTO telegram(tg_id, tg_name, loginhash) VALUES(?,?,?)", tgID, tgName, UUID.randomUUID().toString()) > 0; } + private Optional getUser(long tgId) { + try { + return Optional.of(jdbc.queryForObject("SELECT id, name, banned FROM users INNER JOIN telegram" + + "ON telegram.user_id = users.id WHERE telegram.tg_id=?", new UserQueries.UserMapper(), tgId)); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); + } + } + private List getSubscribers(int uid) { + 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); + } } -- cgit v1.2.3