aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/api')
-rw-r--r--src/main/java/com/juick/api/Main.java2
-rw-r--r--src/main/java/com/juick/api/TelegramBotHook.java57
2 files changed, 50 insertions, 9 deletions
diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java
index 7a571fe0..6adef22a 100644
--- a/src/main/java/com/juick/api/Main.java
+++ b/src/main/java/com/juick/api/Main.java
@@ -191,7 +191,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
}
String uri = request.getRequestURI();
if (uri.equals("/tlgmbtwbhk")) {
- tgb.doPost(request, response);
+ tgb.doPost(request);
return;
}
diff --git a/src/main/java/com/juick/api/TelegramBotHook.java b/src/main/java/com/juick/api/TelegramBotHook.java
index a502e846..b6cd16ab 100644
--- a/src/main/java/com/juick/api/TelegramBotHook.java
+++ b/src/main/java/com/juick/api/TelegramBotHook.java
@@ -1,14 +1,25 @@
package com.juick.api;
+import com.juick.json.MessageSerializer;
+import com.neovisionaries.ws.client.WebSocket;
+import com.neovisionaries.ws.client.WebSocketAdapter;
+import com.neovisionaries.ws.client.WebSocketFactory;
+import com.neovisionaries.ws.client.WebSocketListener;
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.Update;
+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 javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -17,21 +28,51 @@ import java.util.logging.Logger;
public class TelegramBotHook {
private static final Logger logger = Logger.getLogger(TelegramBotHook.class.getName());
- private String token;
TelegramBot bot;
+ List<Long> chats = new ArrayList<>();
+ WebSocket ws;
+ MessageSerializer ms = new MessageSerializer();
public TelegramBotHook(String token) {
- this.token = token;
bot = TelegramBotAdapter.build(token);
bot.setWebhook("https://api.juick.com/tlgmbtwbhk");
+ try {
+ ws = new WebSocketFactory().createSocket("wss://ws.juick.com/_all");
+ ws.addListener(new WebSocketAdapter() {
+ @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);
+ }
+ });
+ });
+ }
+ });
+ } catch (IOException e) {
+ logger.log(Level.SEVERE, "couldn't create ws connection", e);
+ }
}
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
- StringBuilder sb = new StringBuilder();
+ public void doPost(HttpServletRequest request) throws IOException {
try (BufferedReader reader = request.getReader()) {
- Update update = BotUtils.parseUpdate(reader);
- logger.info("got telegram msg: " + update.message().toString());
+ 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());
+ }
}
}
}