aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src/main/java/com/juick/server/TelegramBotManager.java')
-rw-r--r--juick-server/src/main/java/com/juick/server/TelegramBotManager.java114
1 files changed, 69 insertions, 45 deletions
diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
index 564a2255..de54c3ce 100644
--- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
+++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java
@@ -24,6 +24,7 @@ import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.TelegramService;
import com.juick.service.UserService;
+import com.juick.util.MessageUtils;
import com.pengrad.telegrambot.BotUtils;
import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot;
@@ -34,6 +35,7 @@ import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.GetFile;
import com.pengrad.telegrambot.request.SendMessage;
+import com.pengrad.telegrambot.request.SendPhoto;
import com.pengrad.telegrambot.request.SetWebhook;
import com.pengrad.telegrambot.response.GetFileResponse;
import com.pengrad.telegrambot.response.SendResponse;
@@ -159,7 +161,7 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> {
String msgUrl = "http://juick.com/login?hash=" + userService.getHashByUID(user_from.getUid());
String msg = String.format("Hi, %s!\nYou can post messages and images to Juick there.\n" +
"Tap to [log into website](%s) to get more info", user_from.getName(), msgUrl);
- telegramNotify(message.from().id().longValue(), msg);
+ telegramNotify(message.from().id().longValue(), msg, StringUtils.EMPTY);
} else {
Message replyMessage = message.replyToMessage();
if (replyMessage != null) {
@@ -180,7 +182,7 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> {
prefix = String.format("#%d/%d ", mid, rid);
}
serverManager.processMessage(user_from, prefix + text, attachment);
- telegramNotify(message.from().id().longValue(), "Reply sent");
+ telegramNotify(message.from().id().longValue(), "Reply sent", StringUtils.EMPTY);
} else {
logger.warn("invalid path: {}", path);
}
@@ -189,15 +191,15 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> {
}
} else {
telegramNotify(message.from().id().longValue(),
- "Can not reply to this message", replyMessage.messageId());
+ "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY);
}
} else {
telegramNotify(message.from().id().longValue(),
- "Can not reply to this message", replyMessage.messageId());
+ "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY);
}
} else {
serverManager.processMessage(user_from, text, attachment);
- telegramNotify(message.from().id().longValue(), "Message sent");
+ telegramNotify(message.from().id().longValue(), "Message sent", StringUtils.EMPTY);
}
}
}
@@ -220,62 +222,84 @@ public class TelegramBotManager implements ApplicationListener<MessageEvent> {
List<Long> chats = telegramService.getChats();
// registered subscribed users
- users.forEach(c -> telegramNotify(c, msg));
+ users.forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg)));
// anonymous
- chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg));
+ chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg)));
} else if (jmsg.getRid() > 0) {
// get quote
com.juick.Message msg = messagesService.getReply(jmsg.getMid(), jmsg.getRid());
String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(msg, true));
telegramService.getTelegramIdentifiers(
subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid())
- ).forEach(c -> telegramNotify(c, fmsg));
+ ).forEach(c -> telegramNotify(c, fmsg, MessageUtils.attachmentUrl(jmsg)));
}
}
- public void telegramNotify(Long chatId, String msg) {
- telegramNotify(chatId, msg, 0);
+ public void telegramNotify(Long chatId, String msg, String attachment) {
+ telegramNotify(chatId, msg, 0, attachment);
}
- public void telegramNotify(Long chatId, String msg, Integer replyTo) {
- SendMessage telegramMessage = new SendMessage(chatId, msg);
- if (replyTo > 0) {
- telegramMessage.replyToMessageId(replyTo);
- }
- telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true);
- bot.execute(telegramMessage, new Callback<SendMessage, SendResponse>() {
- @Override
- public void onResponse(SendMessage request, SendResponse response) {
- if (!response.isOk()) {
- if (response.errorCode() == 403) {
- // remove from anonymous chat
- telegramService.getChats().stream().filter(c -> c.equals(chatId)).findFirst().ifPresent(
- d -> {
- telegramService.deleteChat(d);
- logger.info("deleted {} chat", d);
- }
- );
- int userId = telegramService.getUser(chatId);
- if (userId > 0) {
- User userToDelete = userService.getUserByUID(userId)
- .orElse(new User());
- boolean status = telegramService.deleteTelegramUser(userToDelete.getUid());
- logger.info("deleting telegram id of @{} : {}", userToDelete.getName(), status);
- boolean chatStatus = telegramService.deleteChat(chatId);
- logger.info("deleting telegram chat {} : {}", chatId, chatStatus);
- }
- } else {
- logger.warn("error response, isOk: {}, errorCode: {}, description: {}",
- response.isOk(), response.errorCode(), response.description());
- }
+ public void telegramNotify(Long chatId, String msg, Integer replyTo, String attachment) {
+ if (StringUtils.isEmpty(attachment)) {
+ SendMessage telegramMessage = new SendMessage(chatId, msg);
+ if (replyTo > 0) {
+ telegramMessage.replyToMessageId(replyTo);
+ }
+ telegramMessage.parseMode(ParseMode.Markdown).disableWebPagePreview(true);
+ bot.execute(telegramMessage, new Callback<SendMessage, SendResponse>() {
+ @Override
+ public void onResponse(SendMessage request, SendResponse response) {
+ processTelegramResponse(chatId, response);
+ }
+ @Override
+ public void onFailure(SendMessage request, IOException e) {
+ logger.warn("telegram failure", e);
}
+ });
+ } else {
+ SendPhoto telegramPhoto = new SendPhoto(chatId, attachment);
+ telegramPhoto.caption(msg);
+ if (replyTo > 0) {
+ telegramPhoto.replyToMessageId(replyTo);
}
+ telegramPhoto.parseMode(ParseMode.Markdown);
+ bot.execute(telegramPhoto, new Callback<SendPhoto, SendResponse>() {
+ @Override
+ public void onResponse(SendPhoto request, SendResponse response) {
+ processTelegramResponse(chatId, response);
+ }
+ @Override
+ public void onFailure(SendPhoto request, IOException e) {
+ logger.warn("telegram failure", e);
+ }
+ });
+ }
+ }
- @Override
- public void onFailure(SendMessage request, IOException e) {
- logger.warn("telegram failure", e);
+ private void processTelegramResponse(Long chatId, SendResponse response) {
+ if (!response.isOk()) {
+ if (response.errorCode() == 403) {
+ // remove from anonymous chat
+ telegramService.getChats().stream().filter(c -> c.equals(chatId)).findFirst().ifPresent(
+ d -> {
+ telegramService.deleteChat(d);
+ logger.info("deleted {} chat", d);
+ }
+ );
+ int userId = telegramService.getUser(chatId);
+ if (userId > 0) {
+ User userToDelete = userService.getUserByUID(userId)
+ .orElse(new User());
+ boolean status = telegramService.deleteTelegramUser(userToDelete.getUid());
+ logger.info("deleting telegram id of @{} : {}", userToDelete.getName(), status);
+ boolean chatStatus = telegramService.deleteChat(chatId);
+ logger.info("deleting telegram chat {} : {}", chatId, chatStatus);
+ }
+ } else {
+ logger.warn("error response, isOk: {}, errorCode: {}, description: {}",
+ response.isOk(), response.errorCode(), response.description());
}
- });
+ }
}
public void telegramSignupNotify(Long telegramId, String hash) {