From 950177ab7959590cfd9c8678b6dbe563fcc964e0 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 13 May 2016 12:11:52 +0300 Subject: store chats in db --- src/main/java/com/juick/api/Main.java | 2 +- src/main/java/com/juick/api/TelegramBotHook.java | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'src/main/java/com/juick') 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 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 chats = getChats(); chats.stream().forEach(c -> { bot.execute(new SendMessage(c, msg), new Callback() { @Override @@ -83,12 +86,20 @@ public class TelegramBotHook { } } + List 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 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() { @Override -- cgit v1.2.3