aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/api/TelegramBotHook.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-05-12 22:53:12 +0300
committerGravatar Vitaly Takmazov2016-05-12 22:53:12 +0300
commita24822ad17cc4be9ccaa01badfd17f193867e457 (patch)
treec6715362f9e900c7876f2974f1519948e01422b1 /src/main/java/com/juick/api/TelegramBotHook.java
parent01cf5ebb4e9db6458bdda762454b699593f796f5 (diff)
forward all juick messages
Diffstat (limited to 'src/main/java/com/juick/api/TelegramBotHook.java')
-rw-r--r--src/main/java/com/juick/api/TelegramBotHook.java57
1 files changed, 49 insertions, 8 deletions
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());
+ }
}
}
}