aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-04-07 18:49:56 +0300
committerGravatar Vitaly Takmazov2022-04-08 15:17:01 +0300
commit94d52e4e4022678bce895c4a567d1c9c9d01560a (patch)
tree6c53e43db64d60fe4a9c94e32eec01ba6dc65cda /src/main/java/com/juick
parent0001282064ac4b9461bcf6e6fb0f66752e8e9bef (diff)
Telegram: send top messages to the channel
Diffstat (limited to 'src/main/java/com/juick')
-rw-r--r--src/main/java/com/juick/TelegramBotManager.java47
1 files changed, 33 insertions, 14 deletions
diff --git a/src/main/java/com/juick/TelegramBotManager.java b/src/main/java/com/juick/TelegramBotManager.java
index abed77fc3..eb8c2b83a 100644
--- a/src/main/java/com/juick/TelegramBotManager.java
+++ b/src/main/java/com/juick/TelegramBotManager.java
@@ -82,6 +82,8 @@ public class TelegramBotManager implements NotificationListener {
private String telegramToken;
@Value("${telegram_debug:false}")
private boolean telegramDebug;
+ @Value("${telegram_top_channel:@JuickTop}")
+ private String topChannelName;
@Inject
private TelegramService telegramService;
@Inject
@@ -291,11 +293,11 @@ public class TelegramBotManager implements NotificationListener {
return e.offset() == 0 && e.type().equals(MessageEntity.Type.text_link) && e.length() == 2;
}
- public void telegramNotify(Long chatId, String msg, @Nonnull com.juick.model.Message source) {
+ public void telegramNotify(Object chatId, String msg, @Nonnull com.juick.model.Message source) {
telegramNotify(chatId, msg, 0, source);
}
- public void telegramNotify(Long chatId, String msg, Integer replyTo, @Nonnull com.juick.model.Message source) {
+ public void telegramNotify(Object chatId, String msg, Integer replyTo, @Nonnull com.juick.model.Message source) {
String attachment = MessageUtils.attachmentUrl(source);
boolean isSendTxt = true;
if (!StringUtils.isEmpty(attachment)) {
@@ -311,7 +313,11 @@ public class TelegramBotManager implements NotificationListener {
bot.execute(telegramPhoto, new Callback<>() {
@Override
public void onResponse(SendPhoto request, SendResponse response) {
- processTelegramResponse(chatId, response, source);
+ if (chatId instanceof Long) {
+ processTelegramResponse((Long)chatId, response, source);
+ } else {
+ processTelegramChannelResponse((String)chatId, response, source);
+ }
}
@Override
@@ -329,7 +335,11 @@ public class TelegramBotManager implements NotificationListener {
bot.execute(telegramMessage, new Callback<>() {
@Override
public void onResponse(SendMessage request, SendResponse response) {
- processTelegramResponse(chatId, response, source);
+ if (chatId instanceof Long) {
+ processTelegramResponse((Long)chatId, response, source);
+ } else {
+ processTelegramChannelResponse((String)chatId, response, source);
+ }
}
@Override
@@ -373,6 +383,13 @@ public class TelegramBotManager implements NotificationListener {
}
}
+ private void processTelegramChannelResponse(String channelId, SendResponse response, com.juick.model.Message source) {
+ if (!response.isOk()) {
+ logger.warn("error response from channel, isOk: {}, errorCode: {}, description: {}, Juick message: {}",
+ response.isOk(), response.errorCode(), response.description(), source.getMid());
+ }
+ }
+
public void telegramSignupNotify(Long telegramId, String hash) {
bot.execute(new SendMessage(telegramId,
String.format("You are subscribed to all Juick messages. " +
@@ -407,6 +424,9 @@ public class TelegramBotManager implements NotificationListener {
processFollow(activity.getFrom(), activity.getTo());
}
}
+ private String formatTelegramMessage(com.juick.model.Message jmsg) {
+ return String.format("[%s](%s) %s", MSG_LINK, formatUrl(jmsg), formatPost(jmsg, true));
+ }
private void processMessage(com.juick.model.Message jmsg, List<User> subscribedUsers) {
if (jmsg.isService()) {
return;
@@ -421,7 +441,7 @@ public class TelegramBotManager implements NotificationListener {
subscribedUsers
).forEach(c -> telegramNotify(c, fmsg, jmsg));
} else {
- String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true));
+ String msg = formatTelegramMessage(jmsg);
List<Long> users = telegramService.getTelegramIdentifiers(subscribedUsers);
List<Long> chats = telegramService.getAnonymous();
@@ -434,17 +454,15 @@ public class TelegramBotManager implements NotificationListener {
}
private void processLike(User liker, com.juick.model.Message message, List<User> subscribers) {
- if (!liker.equals(serviceUser)) {
- logger.info("Like received in tg listener");
- if (!userService.isInBLAny(message.getUser().getUid(), liker.getUid())) {
- telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser()))
- .forEach(c -> telegramNotify(c, String.format("%s recommends your [post](%s)",
- MessageUtils.getMarkdownUser(liker), formatUrl(message)), new com.juick.model.Message()));
- }
- telegramService.getTelegramIdentifiers(subscribers)
- .forEach(c -> telegramNotify(c, String.format("%s recommends you someone's [post](%s)",
+ logger.info("Like received in tg listener");
+ if (!userService.isInBLAny(message.getUser().getUid(), liker.getUid())) {
+ telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser()))
+ .forEach(c -> telegramNotify(c, String.format("%s recommends your [post](%s)",
MessageUtils.getMarkdownUser(liker), formatUrl(message)), new com.juick.model.Message()));
}
+ telegramService.getTelegramIdentifiers(subscribers)
+ .forEach(c -> telegramNotify(c, String.format("%s recommends you someone's [post](%s)",
+ MessageUtils.getMarkdownUser(liker), formatUrl(message)), new com.juick.model.Message()));
}
@Override
@@ -453,6 +471,7 @@ public class TelegramBotManager implements NotificationListener {
}
private void processTop(com.juick.model.Message message) {
+ telegramNotify(topChannelName, formatTelegramMessage(message), message);
telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser()))
.forEach(c -> telegramNotify(c, String.format("Your [post](%s) became popular!",
formatUrl(message)), new com.juick.model.Message()));