diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/juick/api/Main.java | 13 | ||||
-rw-r--r-- | src/main/java/com/juick/api/TelegramBotHook.java | 100 |
2 files changed, 109 insertions, 4 deletions
diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java index 09fb51ec..9b327e83 100644 --- a/src/main/java/com/juick/api/Main.java +++ b/src/main/java/com/juick/api/Main.java @@ -64,6 +64,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { Users users; PM pm; Others others; + TelegramBotHook tgb; @Override public void init() throws ServletException { @@ -80,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", "")); setupXmppComponent(conf.getProperty("xmpp_host", "localhost"), Integer.parseInt(conf.getProperty("xmpp_port", "5347")), conf.getProperty("xmpp_jid", "api.localhost"), conf.getProperty("xmpp_password")); @@ -126,12 +127,13 @@ public class Main extends HttpServlet implements Stream.StreamListener { request.setCharacterEncoding("UTF-8"); } + String uri = request.getRequestURI(); + int vuid = Utils.getHttpAuthUID(jdbc, request); if (vuid == 0) { vuid = Utils.getVisitorQueryStringUID(jdbc, request); } - String uri = request.getRequestURI(); if (uri.equals("/home")) { if (vuid > 0) { messages.doGetHome(request, response, vuid); @@ -187,6 +189,11 @@ public class Main extends HttpServlet implements Stream.StreamListener { if (request.getCharacterEncoding() == null) { request.setCharacterEncoding("UTF-8"); } + String uri = request.getRequestURI(); + if (uri.equals("/tlgmbtwbhk")) { + tgb.doPost(request); + return; + } int vuid = Utils.getHttpAuthUID(jdbc, request); if (vuid == 0) { @@ -196,8 +203,6 @@ public class Main extends HttpServlet implements Stream.StreamListener { response.sendError(401); return; } - - String uri = request.getRequestURI(); switch (uri) { case "/post": int mid = Utils.parseInt(request.getParameter("mid"), 0); diff --git a/src/main/java/com/juick/api/TelegramBotHook.java b/src/main/java/com/juick/api/TelegramBotHook.java new file mode 100644 index 00000000..7ee759eb --- /dev/null +++ b/src/main/java/com/juick/api/TelegramBotHook.java @@ -0,0 +1,100 @@ +package com.juick.api; + +import com.juick.json.MessageSerializer; +import com.neovisionaries.ws.client.*; +import com.pengrad.telegrambot.BotUtils; +import com.pengrad.telegrambot.Callback; +import com.pengrad.telegrambot.TelegramBot; +import com.pengrad.telegrambot.TelegramBotAdapter; +import com.pengrad.telegrambot.model.Message; +import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.response.SendResponse; +import org.json.JSONObject; + +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Created by vt on 12/05/16. + */ +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(); + + + public TelegramBotHook(String token) { + bot = TelegramBotAdapter.build(token); + bot.setWebhook("https://api.juick.com/tlgmbtwbhk"); + try { + 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.setPingInterval(60 * 1000); + ws.addListener(new WebSocketAdapter() { + @Override + public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception { + logger.info("ws disconnected"); + } + + @Override + public void onConnected(WebSocket websocket, Map<String, List<String>> headers) { + logger.info("ws connected"); + } + @Override + public void onTextMessage(WebSocket websocket, String text) throws Exception { + super.onTextMessage(websocket, text); + com.juick.Message jmsg = ms.deserialize(new JSONObject(text)); + logger.info("got jmsg: " + jmsg.toString()); + chats.stream().forEach(c -> { + bot.execute(new SendMessage(c, jmsg.getText()), new Callback<SendMessage, SendResponse>() { + @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); + } + }); + }); + } + }); + ws.connect(); + } catch (IOException | WebSocketException e) { + logger.log(Level.SEVERE, "couldn't create ws connection", e); + } + } + + 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()); + if (!chats.contains(message.chat().id())) { + chats.add(message.chat().id()); + logger.info("added chat with " + message.from().firstName()); + bot.execute(new SendMessage(message.chat().id(), "Subscribed!"), new Callback<SendMessage, SendResponse>() { + @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); + } + }); + } + } + } +} |