From ef3c9087501f0eb4349c0f27740854502bf9e169 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 27 Oct 2017 13:07:14 +0300 Subject: api: telegram replies --- .../java/com/juick/api/TelegramBotManager.java | 16 ++++------- .../com/juick/api/controllers/TelegramWebhook.java | 32 ++++++++++++++++++---- .../java/com/juick/api/tests/MessagesTests.java | 8 ++++++ 3 files changed, 41 insertions(+), 15 deletions(-) (limited to 'juick-api/src') 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 users = telegramService.getTelegramIdentifiers(subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid())); List 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() { @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); + } } } } diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java index 906e8713..63fac6a1 100644 --- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java +++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java @@ -44,6 +44,8 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.util.UriComponents; +import org.springframework.web.util.UriComponentsBuilder; import javax.inject.Inject; import java.util.Arrays; @@ -262,4 +264,10 @@ public class MessagesTests extends AbstractJUnit4SpringContextTests { }); assertThat(user.get(0).getTokens().get(0).getToken(), equalTo(token)); } + @Test + public void tg2juickLinks() { + UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://juick.com/123456#23").build(); + assertThat(uriComponents.getPath().substring(1), is("123456")); + assertThat(uriComponents.getFragment(), is("23")); + } } -- cgit v1.2.3