diff options
author | 2016-05-13 12:11:52 +0300 | |
---|---|---|
committer | 2016-05-13 12:11:52 +0300 | |
commit | 950177ab7959590cfd9c8678b6dbe563fcc964e0 (patch) | |
tree | 987479a0fe030b4f2f4004cb5767660c4ea12c23 | |
parent | 324391a3b775f6508b316f72268d776e50aff2cc (diff) |
store chats in db
-rw-r--r-- | src/main/java/com/juick/api/Main.java | 2 | ||||
-rw-r--r-- | src/main/java/com/juick/api/TelegramBotHook.java | 17 |
2 files changed, 15 insertions, 4 deletions
diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java index 9b327e83..c632d8fc 100644 --- a/src/main/java/com/juick/api/Main.java +++ b/src/main/java/com/juick/api/Main.java @@ -81,7 +81,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { users = new Users(jdbc); pm = new PM(jdbc); others = new Others(jdbc); - tgb = new TelegramBotHook(conf.getProperty("telegram_token", "")); + tgb = new TelegramBotHook(jdbc, conf.getProperty("telegram_token", "")); 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/src/main/java/com/juick/api/TelegramBotHook.java b/src/main/java/com/juick/api/TelegramBotHook.java index ed3e8942..d06473a3 100644 --- a/src/main/java/com/juick/api/TelegramBotHook.java +++ b/src/main/java/com/juick/api/TelegramBotHook.java @@ -10,6 +10,7 @@ import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.response.SendResponse; import org.json.JSONObject; +import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; @@ -27,12 +28,13 @@ public class TelegramBotHook { private static final Logger logger = Logger.getLogger(TelegramBotHook.class.getName()); TelegramBot bot; - List<Long> chats = new ArrayList<>(); WebSocket ws; MessageSerializer ms = new MessageSerializer(); + JdbcTemplate jdbc; - public TelegramBotHook(String token) { + public TelegramBotHook(JdbcTemplate jdbc, String token) { + this.jdbc = jdbc; bot = TelegramBotAdapter.build(token); bot.setWebhook("https://api.juick.com/tlgmbtwbhk"); try { @@ -62,6 +64,7 @@ public class TelegramBotHook { } sb.append("https://juick.com/").append(jmsg.getMID()); String msg = sb.toString(); + List<Long> chats = getChats(); chats.stream().forEach(c -> { bot.execute(new SendMessage(c, msg), new Callback<SendMessage, SendResponse>() { @Override @@ -83,12 +86,20 @@ public class TelegramBotHook { } } + List<Long> getChats() { + return jdbc.queryForList("SELECT chat_id FROM telegram_chats", Long.class); + } + void addChat(Long id) { + jdbc.update("INSERT IGNORE TO telegram_chats(chat_id) VALUES(?)", id); + } + public void doPost(HttpServletRequest request) throws IOException { try (BufferedReader reader = request.getReader()) { Message message = BotUtils.parseUpdate(reader).message(); logger.info("got telegram msg: " + message.toString()); + List<Long> chats = getChats(); if (!chats.contains(message.chat().id())) { - chats.add(message.chat().id()); + addChat(message.chat().id()); logger.info("added chat with " + message.from().firstName()); bot.execute(new SendMessage(message.chat().id(), "Subscribed!"), new Callback<SendMessage, SendResponse>() { @Override |