Skip to content

Commit

Permalink
* JRTB-4: added ability to send notifications about new articles
Browse files Browse the repository at this point in the history
*   JRTB-8: added ability to set inactive telegram user
*   JRTB-9: added ability to set active user and/or start using it.
  • Loading branch information
IvanLiVa committed Dec 22, 2023
1 parent d218909 commit 529a785
Show file tree
Hide file tree
Showing 30 changed files with 447 additions and 15 deletions.
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ JRTB-0: added SpringBoot skeleton project
* ## 0.6.0-SNAPSHOT

* JRTB-7: added the ability to delete group subscription.
## 0.7.0-SNAPSHOT

* JRTB-4: added ability to send notifications about new articles
* JRTB-8: added ability to set inactive telegram user
* JRTB-9: added ability to set active user and/or start using it.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.github.JBolivarLi</groupId>
<artifactId>javarush-telegrambot</artifactId>
<version>0.2.0-SNAPSHOT</version>
<version>0.7.0-SNAPSHOT</version>
<name>javarush-telegrambot</name>
<description>Telegram bot for Javarush from community to community</description>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;


@EnableScheduling
@SpringBootApplication
public class JavarushTelegrambotApplication {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public class HelpCommand implements Command {
+ "%s - отписаться от группы статей\n"
+ "%s - получить список групп, на которые подписан\n\n"

+ "%s - получить помощь в работе со мной\n",
+ "%s - получить помощь в работе со мной\n"
+ "Работа с данными группы:\n"
+ "%s - получить статистику \"\\u263A\"подписчиков бота:\n",
START.getCommandName(), STOP.getCommandName(), ADD_GROUP_SUB.getCommandName(),
DELETE_GROUP_SUB.getCommandName(), LIST_GROUP_SUB.getCommandName(), HELP.getCommandName());
DELETE_GROUP_SUB.getCommandName(), LIST_GROUP_SUB.getCommandName(), HELP.getCommandName(), STAT.getCommandName());


public HelpCommand(SendBotMessageService sendBotMessageService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public StopCommand(SendBotMessageService sendBotMessageService, TelegramUserServ

@Override
public void execute(Update update) {
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), STOP_MESSAGE);
telegramUserService.findByChatId(update.getMessage().getChatId().toString())
.ifPresent(it -> {
it.setActive(false);
telegramUserService.save(it);
});
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), STOP_MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ public interface JavaRushGroupClient {
* @return {@link GroupDiscussionInfo} object.
*/
GroupDiscussionInfo getGroupById(Integer id);
Integer findLastPostId(Integer groupSub);
}

Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient;


import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupDiscussionInfo;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupInfo;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupRequestArgs;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupsCountRequestArgs;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.*;
import kong.unirest.GenericType;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import static org.springframework.util.CollectionUtils.isEmpty;
import java.util.Optional;

import java.util.List;

Expand All @@ -26,9 +25,11 @@
public class JavaRushGroupClientImpl implements JavaRushGroupClient {

private final String javarushApiGroupPath;
private final String getJavarushApiPostPath;

public JavaRushGroupClientImpl(@Value("${javarush.api.path}") String javarushApi) {
this.javarushApiGroupPath = javarushApi + "/groups";
this.getJavarushApiPostPath = javarushApi + "/posts";
}

@Override
Expand Down Expand Up @@ -66,6 +67,17 @@ public GroupDiscussionInfo getGroupById(Integer id) {
.asObject(GroupDiscussionInfo.class)
.getBody();
}
@Override
public Integer findLastPostId(Integer groupSubId) {
List<PostInfo> posts = Unirest.get(getJavarushApiPostPath)
.queryString("order", "NEW")
.queryString("groupKid", groupSubId.toString())
.queryString("limit", "1")
.asObject(new GenericType<List<PostInfo>>() {
})
.getBody();
return isEmpty(posts) ? 0 : Optional.ofNullable(posts.get(0)).map(PostInfo::getId).orElse(0);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient;

import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.PostInfo;

import java.util.List;

/**
* Client for Javarush Open API corresponds to Posts.
*/
public interface JavaRushPostClient {

/**
* Find new posts since lastPostId in provided group.
*
* @param groupId provided group ID.
* @param lastPostId provided last post ID.
* @return the collection of the new {@link PostInfo}.
*/
List<PostInfo> findNewPosts(Integer groupId, Integer lastPostId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient;
import kong.unirest.GenericType;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.PostInfo;
import kong.unirest.Unirest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class JavaRushPostClientImpl implements JavaRushPostClient {

private final String javarushApiPostPath;

public JavaRushPostClientImpl(@Value("${javarush.api.path}") String javarushApi) {
this.javarushApiPostPath = javarushApi + "/posts";
}

@Override
public List<PostInfo> findNewPosts(Integer groupId, Integer lastPostId) {
List<PostInfo> lastPostsByGroup = Unirest.get(javarushApiPostPath)
.queryString("order", "NEW")
.queryString("groupKid", groupId)
.queryString("limit", 15)
.asObject(new GenericType<List<PostInfo>>() {
}).getBody();
List<PostInfo> newPosts = new ArrayList<>();
for (PostInfo post : lastPostsByGroup) {
if (lastPostId.equals(post.getId())) {
return newPosts;
}
newPosts.add(post);
}
return newPosts;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto;


import lombok.Data;

/**
* DTO, which represents base user information.
*/
@Data
public class BaseUserInfo {
private String city;
private String country;
private String displayName;
private Integer id;
private String job;
private String key;
private Integer level;
private String pictureUrl;
private String position;
private UserPublicStatus publicStatus;
private String publicStatusMessage;
private Integer rating;
private Integer userId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto;

/**
* DTO, which represents languages.
*/
public enum Language {
UNKNOWN,
ENGLISH,
GERMAN,
SPANISH,
HINDI,
FRENCH,
PORTUGUESE,
POLISH,
BENGALI,
PUNJABI,
CHINESE,
ITALIAN,
INDONESIAN,
MARATHI,
TAMIL,
TELUGU,
JAPANESE,
KOREAN,
URDU,
TAIWANESE,
NETHERLANDS,
RUSSIAN,
UKRAINIAN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto;

/**
* DTO, which represents like's status.
*/
public enum LikeStatus {

UNKNOWN,
LIKE,
HOT,
FOLLOW,
FAVORITE,
SOLUTION,
HELPFUL,
ARTICLE,
OSCAR,
DISLIKE,
WRONG,
SPAM,
ABUSE,
FOUL,
TROLLING,
OFFTOPIC,
DUPLICATE,
DIRTY,
OUTDATED,
BORING,
UNCLEAR,
HARD,
EASY,
FAKE,
SHAM,
AWFUL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto;

/**
* DTO, which represents like's information.
*/
public class LikesInfo {

private Integer count;
private LikeStatus status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto;


import lombok.Data;

/**
* DTO, which represents post information.
*/
@Data
public class PostInfo {

private BaseUserInfo authorInfo;
private Integer commentsCount;
private String content;
private Long createdTime;
private String description;
private GroupInfo groupInfo;
private Integer id;
private String key;
private Language language;
private LikesInfo likesInfo;
private GroupInfo originalGroupInfo;
private String pictureUrl;
private Double rating;
private Integer ratingCount;
private String title;
private PostType type;
private Long updatedTime;
private UserDiscussionInfo userDiscussionInfo;
private Integer views;
private VisibilityStatus visibilityStatus;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto;

/**
* DTO, which represents post types.
*/
public enum PostType {
UNKNOWN, USUAL, INNER_LINK, OUTER_LINK
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto;

/**
* DTO, which represents user public status.
*/
public enum UserPublicStatus {
UNKNOWN,
BEGINNER,
ACTIVE,
STRONG,
GRADUATED,
INTERNSHIP_IN_PROGRESS,
INTERNSHIP_COMPLETED,
RESUME_COMPLETED,
LOOKING_FOR_JOB,
HAVE_JOB;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto;

/**
* DTO, which represents visibility status.
*/
public enum VisibilityStatus {
UNKNOWN,
RESTRICTED,
PUBLIC,
PROTECTED,
PRIVATE,
DISABLED,
DELETED
}
Loading

0 comments on commit 529a785

Please sign in to comment.