aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--juick-api/src/main/java/com/juick/api/Main.java7
-rw-r--r--juick-api/src/main/java/com/juick/api/TelegramBotHook.java26
2 files changed, 25 insertions, 8 deletions
diff --git a/juick-api/src/main/java/com/juick/api/Main.java b/juick-api/src/main/java/com/juick/api/Main.java
index e56ca8c2..5a495ba2 100644
--- a/juick-api/src/main/java/com/juick/api/Main.java
+++ b/juick-api/src/main/java/com/juick/api/Main.java
@@ -41,6 +41,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
+import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
@@ -193,7 +194,11 @@ public class Main extends HttpServlet implements Stream.StreamListener {
}
String uri = request.getRequestURI();
if (uri.equals("/tlgmbtwbhk")) {
- tgb.doPost(request);
+ try {
+ tgb.doPost(request);
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+ log("telegram error", e);
+ }
return;
}
if (uri.equals("/skypebotendpoint")) {
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java b/juick-api/src/main/java/com/juick/api/TelegramBotHook.java
index f440f8bc..e09c4021 100644
--- a/juick-api/src/main/java/com/juick/api/TelegramBotHook.java
+++ b/juick-api/src/main/java/com/juick/api/TelegramBotHook.java
@@ -4,6 +4,8 @@ import com.juick.User;
import com.juick.json.MessageSerializer;
import com.juick.server.MessagesQueries;
import com.juick.server.UserQueries;
+import com.juick.server.protocol.JuickProtocol;
+import com.juick.server.protocol.ProtocolReply;
import com.neovisionaries.ws.client.*;
import com.pengrad.telegrambot.BotUtils;
import com.pengrad.telegrambot.Callback;
@@ -22,6 +24,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -38,11 +41,13 @@ public class TelegramBotHook {
WebSocket ws, wsReply;
MessageSerializer ms = new MessageSerializer();
JdbcTemplate jdbc;
+ JuickProtocol protocol;
public TelegramBotHook(JdbcTemplate jdbc, String token) {
this.jdbc = jdbc;
bot = TelegramBotAdapter.build(token);
+ protocol = new JuickProtocol(jdbc, "https://juick.com/");
try {
bot.setWebhook("https://api.juick.com/tlgmbtwbhk");
ws = new WebSocketFactory().createSocket("wss://ws.juick.com/_all");
@@ -122,12 +127,16 @@ public class TelegramBotHook {
}
private void telegramNotify(Long c, String msg, String msgUrl) {
- bot.execute(new SendMessage(c, msg).replyMarkup(
- new InlineKeyboardMarkup(
- new InlineKeyboardButton[]{
- new InlineKeyboardButton("See on Juick").url(msgUrl)
- }
- )), new Callback<SendMessage, SendResponse>() {
+ SendMessage telegramMessage = new SendMessage(c, msg);
+ if (msgUrl != null) {
+ telegramMessage.replyMarkup(
+ new InlineKeyboardMarkup(
+ new InlineKeyboardButton[]{
+ new InlineKeyboardButton("See on Juick").url(msgUrl)
+ }
+ ));
+ }
+ bot.execute(new SendMessage(c, msg), new Callback<SendMessage, SendResponse>() {
@Override
public void onResponse(SendMessage request, SendResponse response) {
logger.info("got response: " + response.message().toString());
@@ -148,7 +157,7 @@ public class TelegramBotHook {
jdbc.update("INSERT IGNORE INTO telegram_chats(chat_id) VALUES(?)", id);
}
- public void doPost(HttpServletRequest request) throws IOException {
+ public void doPost(HttpServletRequest request) throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
try (BufferedReader reader = request.getReader()) {
Update update = BotUtils.parseUpdate(reader);
Message message = update.message();
@@ -183,6 +192,9 @@ public class TelegramBotHook {
String msg = String.format("Hi, %s!\nTap to log in", user_from.getUName());
String msgUrl = "http://juick.com/login?" + UserQueries.getHashByUID(jdbc, user_from.getUID());
telegramNotify(message.from().id().longValue(), msg, msgUrl);
+ } else {
+ ProtocolReply reply = protocol.getReply(user_from, text);
+ telegramNotify(message.from().id().longValue(), reply.getDescription(), null);
}
}
}