aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-10-27 13:07:14 +0300
committerGravatar Vitaly Takmazov2017-10-28 06:15:17 +0000
commitef3c9087501f0eb4349c0f27740854502bf9e169 (patch)
treeac23f9effde6779b994de4ba9ddf8e039d700a3f /juick-api/src/main/java/com/juick/api
parent51df4a3aa7d76a0457c89efc55c35f0fa8e17896 (diff)
api: telegram replies
Diffstat (limited to 'juick-api/src/main/java/com/juick/api')
-rw-r--r--juick-api/src/main/java/com/juick/api/TelegramBotManager.java16
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java32
2 files changed, 33 insertions, 15 deletions
diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
index 7e6d0b17..33e71fee 100644
--- a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
+++ b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java
@@ -25,6 +25,7 @@ import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
+import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SetWebhook;
import com.pengrad.telegrambot.response.SendResponse;
@@ -70,6 +71,8 @@ public class TelegramBotManager extends TextWebSocketHandler {
private WebSocketSession session;
+ public static final String MSG_LINK = "🔗";
+
@PostConstruct
public void init() {
if (StringUtils.isBlank(telegramToken)) {
@@ -115,7 +118,7 @@ public class TelegramBotManager extends TextWebSocketHandler {
String msgUrl = formatUrl(jmsg);
if (jmsg.getRid() == 0) {
- String msg = formatPost(jmsg);
+ String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg));
List<Long> users = telegramService.getTelegramIdentifiers(subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid()));
List<Long> chats = telegramService.getChats();
@@ -127,7 +130,7 @@ public class TelegramBotManager extends TextWebSocketHandler {
} else {
// get quote
com.juick.Message msg = messagesService.getReply(jmsg.getMid(), jmsg.getRid());
- String fmsg = formatPost(msg);
+ String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(msg));
telegramService.getTelegramIdentifiers(
subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid())
).forEach(c -> telegramNotify(c, fmsg, msgUrl));
@@ -136,14 +139,7 @@ public class TelegramBotManager extends TextWebSocketHandler {
public void telegramNotify(Long c, String msg, String msgUrl) {
SendMessage telegramMessage = new SendMessage(c, msg);
- if (msgUrl != null) {
- telegramMessage.replyMarkup(
- new InlineKeyboardMarkup(
- new InlineKeyboardButton[]{
- new InlineKeyboardButton("See on Juick").url(msgUrl)
- }
- ));
- }
+ telegramMessage.parseMode(ParseMode.Markdown);
bot.execute(telegramMessage, new Callback<SendMessage, SendResponse>() {
@Override
public void onResponse(SendMessage request, SendResponse response) {
diff --git a/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java b/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java
index e14d226d..ef2456b5 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/TelegramWebhook.java
@@ -25,6 +25,7 @@ import com.juick.service.TelegramService;
import com.juick.service.UserService;
import com.pengrad.telegrambot.BotUtils;
import com.pengrad.telegrambot.model.Message;
+import com.pengrad.telegrambot.model.MessageEntity;
import com.pengrad.telegrambot.model.PhotoSize;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.request.GetFile;
@@ -38,6 +39,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.util.UriComponents;
+import org.springframework.web.util.UriComponentsBuilder;
import springfox.documentation.annotations.ApiIgnore;
import javax.inject.Inject;
@@ -122,12 +125,31 @@ public class TelegramWebhook {
String msg = String.format("Hi, %s!\nTap to log in", user_from.getName());
String msgUrl = "http://juick.com/login?" + usersService.getHashByUID(user_from.getUid());
telegramBotManager.telegramNotify(message.from().id().longValue(), msg, msgUrl);
- } else if (text.toLowerCase().startsWith("/post ")) {
- apiServer.processMessage(user_from, text.substring(6), attachment);
- telegramBotManager.telegramNotify(message.from().id().longValue(), "message sent", null);
} else {
- telegramBotManager.telegramNotify(message.from().id().longValue(),
- "You can send messages and images to Juick with /post command", null);
+ Message replyMessage = message.replyToMessage();
+ if (replyMessage != null) {
+ String finalText = text;
+ String finalAttachment = attachment;
+ Message finalMessage = message;
+ Arrays.stream(replyMessage.entities())
+ .filter(e -> e.offset() == 0).findFirst().ifPresent(e -> {
+ if (StringUtils.isNotEmpty(e.url())) {
+ UriComponents uriComponents = UriComponentsBuilder.fromUriString(e.url()).build();
+ int mid = Integer.valueOf(uriComponents.getPath().substring(1));
+ String prefix = String.format("#%d ", mid);
+ int rid = 0;
+ if (StringUtils.isNotEmpty(uriComponents.getFragment())) {
+ rid = Integer.valueOf(uriComponents.getFragment());
+ prefix = String.format("#%d/%d ", mid, rid);
+ }
+ apiServer.processMessage(user_from, prefix + finalText, finalAttachment);
+ telegramBotManager.telegramNotify(finalMessage.from().id().longValue(), "reply sent", null);
+ }
+ });
+ } else {
+ apiServer.processMessage(user_from, text, attachment);
+ telegramBotManager.telegramNotify(message.from().id().longValue(), "message sent", null);
+ }
}
}
}