aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/TelegramBotHook.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-07-18 16:51:33 +0300
committerGravatar Vitaly Takmazov2016-07-18 16:51:33 +0300
commit7f80c591731625c5c2c7219a267bda1630634d7c (patch)
treee117743ccfa16860ec56daef798b5079f4ba9e52 /juick-api/src/main/java/com/juick/api/TelegramBotHook.java
parent8649c960a68300dc8ebbc02920425a813d16551a (diff)
juick-api: connect protocol to telegram bot
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/TelegramBotHook.java')
-rw-r--r--juick-api/src/main/java/com/juick/api/TelegramBotHook.java26
1 files changed, 19 insertions, 7 deletions
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);
}
}
}