From d2a00a9ab8f0dedecfe4c091e3f4da75e53917db Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Wed, 1 Mar 2023 20:39:53 +0200 Subject: [PATCH 1/6] simplified LongPollBot#initialize --- .../java/api/longpoll/bots/LongPollBot.java | 35 +++++-------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/src/main/java/api/longpoll/bots/LongPollBot.java b/src/main/java/api/longpoll/bots/LongPollBot.java index bd369a4d..322be7d5 100644 --- a/src/main/java/api/longpoll/bots/LongPollBot.java +++ b/src/main/java/api/longpoll/bots/LongPollBot.java @@ -3,7 +3,7 @@ import api.longpoll.bots.exceptions.VkApiException; import api.longpoll.bots.exceptions.VkResponseException; import api.longpoll.bots.methods.impl.events.GetUpdates; -import api.longpoll.bots.methods.impl.groups.GetLongPollServer; +import com.google.gson.JsonObject; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; @@ -19,16 +19,6 @@ public abstract class LongPollBot extends VkBot { */ private static final long DEFAULT_SESSION_DURATION = 9; - /** - * Group ID. - */ - private Integer groupId; - - /** - * Gets VK long poll server. - */ - private GetLongPollServer getLongPollServer; - /** * Gets VK updates. */ @@ -89,22 +79,15 @@ public void stopPolling() { public void initialize() throws VkApiException { initializedAt = LocalDateTime.now(); - if (groupId == null) { - groupId = vk.other.execute() - .setCode("return API.groups.getById()@.id[0];") - .execute() - .getResponse() - .getAsInt(); - } - - if (getLongPollServer == null) { - getLongPollServer = new GetLongPollServer(getAccessToken()); - } + JsonObject longPollServer = vk.other.execute() + .setCode("return API.groups.getLongPollServer({\"group_id\":API.groups.getById()@.id[0]});") + .execute() + .getResponse() + .getAsJsonObject(); - GetLongPollServer.ResponseBody longPollServer = getLongPollServer.setGroupId(groupId).execute(); - getUpdates = new GetUpdates(longPollServer.getResponse().getServer()) - .setKey(longPollServer.getResponse().getKey()) - .setTs(longPollServer.getResponse().getTs()); + getUpdates = new GetUpdates(longPollServer.get("server").getAsString()) + .setKey(longPollServer.get("key").getAsString()) + .setTs(longPollServer.get("ts").getAsInt()); } /** From a47b1c24015d5d7373c917a4467a6f441d032bad Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Thu, 2 Mar 2023 15:26:44 +0200 Subject: [PATCH 2/6] migrated User#city from String to User.City --- .../java/api/longpoll/bots/model/objects/basic/User.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/api/longpoll/bots/model/objects/basic/User.java b/src/main/java/api/longpoll/bots/model/objects/basic/User.java index 9113fa69..4e7e9e9c 100644 --- a/src/main/java/api/longpoll/bots/model/objects/basic/User.java +++ b/src/main/java/api/longpoll/bots/model/objects/basic/User.java @@ -158,7 +158,7 @@ public class User { * City specified on user's page in "Contacts" section. */ @SerializedName("city") - private String city; + private City city; /** * Number of common friends with current user. @@ -698,11 +698,11 @@ public void setNickname(String nickname) { this.nickname = nickname; } - public String getCity() { + public City getCity() { return city; } - public void setCity(String city) { + public void setCity(City city) { this.city = city; } From 2dae1096abd554f42ae96e5be528c0945049d68d Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Thu, 2 Mar 2023 15:33:54 +0200 Subject: [PATCH 3/6] created Lang.java, VkMethod#setLang --- .../longpoll/bots/methods/impl/VkMethod.java | 11 ++++++++ .../bots/model/objects/additional/Lang.java | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/api/longpoll/bots/model/objects/additional/Lang.java diff --git a/src/main/java/api/longpoll/bots/methods/impl/VkMethod.java b/src/main/java/api/longpoll/bots/methods/impl/VkMethod.java index de1f6d21..6757f433 100644 --- a/src/main/java/api/longpoll/bots/methods/impl/VkMethod.java +++ b/src/main/java/api/longpoll/bots/methods/impl/VkMethod.java @@ -3,6 +3,7 @@ import api.longpoll.bots.exceptions.VkApiException; import api.longpoll.bots.exceptions.VkResponseException; import api.longpoll.bots.http.LoggerInterceptor; +import api.longpoll.bots.model.objects.additional.Lang; import api.longpoll.bots.validator.VkResponseBodyValidator; import com.google.gson.Gson; import okhttp3.Call; @@ -176,4 +177,14 @@ public VkMethod addParam(String key, Object value) { formBodyBuilder.add(key, String.valueOf(value)); return this; } + + /** + * Sets {@code lang} parameter. + * + * @param lang lang value. + * @return current instance. + */ + public VkMethod setLang(Lang lang) { + return addParam("lang", lang); + } } diff --git a/src/main/java/api/longpoll/bots/model/objects/additional/Lang.java b/src/main/java/api/longpoll/bots/model/objects/additional/Lang.java new file mode 100644 index 00000000..5fe478c6 --- /dev/null +++ b/src/main/java/api/longpoll/bots/model/objects/additional/Lang.java @@ -0,0 +1,28 @@ +package api.longpoll.bots.model.objects.additional; + +/** + * Lang. + */ +public enum Lang { + RU(0), + UK(1), + BE(2), + EN(3), + ES(4), + FI(5), + DE(6), + IT(7); + + /** + * Lang code. + */ + private final int code; + + Lang(int code) { + this.code = code; + } + + public int getCode() { + return code; + } +} From 39ab7f1d89e4a54333b087a0e943ed357a6bf213 Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Tue, 7 Mar 2023 13:35:10 +0200 Subject: [PATCH 4/6] updated README.md --- README.md | 149 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index fefb22e5..2c8dbfb6 100644 --- a/README.md +++ b/README.md @@ -1,100 +1,139 @@ [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.yvasyliev/java-vk-bots-longpoll-api/badge.svg?kill_cache=1)](https://search.maven.org/artifact/com.github.yvasyliev/java-vk-bots-longpoll-api) -[![GitHub tag](https://img.shields.io/github/tag/yvasyliev/java-vk-bots-long-poll-api)](https://github.com/yvasyliev/java-vk-bots-long-poll-api/releases/?include_prereleases&sort=semver "View GitHub releases") -![Build status](https://github.com/yvasyliev/java-vk-bots-long-poll-api/actions/workflows/build-maven-project.yml/badge.svg?branch=master) +[![javadoc](https://javadoc.io/badge2/com.github.yvasyliev/java-vk-bots-longpoll-api/javadoc.svg?kill_cache=1)](https://javadoc.io/doc/com.github.yvasyliev/java-vk-bots-longpoll-api) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/yvasyliev/java-vk-bots-long-poll-api/blob/master/LICENSE) +![Build status](https://github.com/yvasyliev/java-vk-bots-long-poll-api/actions/workflows/build-maven-project.yml/badge.svg?branch=master) ![CodeQL](https://github.com/yvasyliev/java-vk-bots-long-poll-api/workflows/CodeQL/badge.svg) -[![Total alerts](https://img.shields.io/lgtm/alerts/g/yvasyliev/java-vk-bots-long-poll-api.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/yvasyliev/java-vk-bots-long-poll-api/alerts/) -[![Language grade: Java](https://img.shields.io/lgtm/grade/java/g/yvasyliev/java-vk-bots-long-poll-api.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/yvasyliev/java-vk-bots-long-poll-api/context:java) +[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=bugs)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) +[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=yvasyliev_java-vk-bots-long-poll-api&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=yvasyliev_java-vk-bots-long-poll-api) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](http://makeapullrequest.com) -[![javadoc](https://javadoc.io/badge2/com.github.yvasyliev/java-vk-bots-longpoll-api/javadoc.svg?kill_cache=1)](https://javadoc.io/doc/com.github.yvasyliev/java-vk-bots-longpoll-api) + # Java VK Bots Long Poll API + A Java library to create VK bots using Bots Long Poll API. ## Description -An easy-to-use and lightweight Java library that implements [Bots Long Poll API](https://vk.com/dev/bots_longpoll). Uses API version: `5.131`. + +An easy-to-use and lightweight Java library that implements [Bots Long Poll API](https://vk.com/dev/bots_longpoll). Uses +API version: `5.131`. ## Note + This library keeps on improving. Feel free to create issues or pull requests. ## Third-party dependencies + This library uses the next third-party dependencies: + * `Gson` * `SLF4J` * `OkHttp` ## Requirements + 1. `Java 8` or higher 2. `Maven` or other build tool ## Quickstart + 1. Create VK Community. 2. Go to `Manage` - `API usage` - `Access tokens` and create `access_token`. -3. Add the library to your `Maven` project: -```xml - - com.github.yvasyliev - java-vk-bots-longpoll-api - 4.0.0 - -``` +3. Add the library to your `Maven` project: + ```xml + + com.github.yvasyliev + java-vk-bots-longpoll-api + 4.0.0 + + ``` 4. Extend `LongPollBot` class and override necessary methods: -```java -public class HelloBot extends LongPollBot { - @Override - public void onMessageNew(MessageNew messageNew) { - try { + ```java + import api.longpoll.bots.LongPollBot; + import api.longpoll.bots.exceptions.VkApiException; + import api.longpoll.bots.model.events.messages.MessageNew; + import api.longpoll.bots.model.objects.basic.Message; + + public class HelloBot extends LongPollBot { + @Override + public void onMessageNew(MessageNew messageNew) { + try { Message message = messageNew.getMessage(); if (message.hasText()) { - String response = "Hello! Received your message: " + message.getText(); - vk.messages.send() - .setPeerId(message.getPeerId()) - .setMessage(response) - .execute(); + String response = "Hello! Received your message: " + message.getText(); + vk.messages.send() + .setPeerId(message.getPeerId()) + .setMessage(response) + .execute(); } - } catch (VkApiException e) { - e.printStackTrace(); - } - } - - @Override - public String getAccessToken() { - return "your_access_token"; - } - - public static void main(String[] args) throws VkApiException { - new HelloBot().startPolling(); - } -} -``` + } catch (VkApiException e) { + e.printStackTrace(); + } + } + + @Override + public String getAccessToken() { + return "your_access_token"; + } + + public static void main(String[] args) throws VkApiException { + new HelloBot().startPolling(); + } + } + ``` + ## How to send photos or documents? + Easy: + ```java -@Override -public void onMessageNew(MessageNew messageNew) { - try { - Message message = messageNew.getMessage(); - vk.messages.send() - .setPeerId(message.getPeerId()) - .setMessage("Sending some files to you...") - .addPhoto(new File("your_photo.png")) // to send photo as photo - .addDoc(new File("your_photo.png")) // to send photo as document - .execute(); - } catch (VkApiException e) { - e.printStackTrace(); - } +import api.longpoll.bots.LongPollBot; +import api.longpoll.bots.exceptions.VkApiException; +import api.longpoll.bots.model.events.messages.MessageNew; +import api.longpoll.bots.model.objects.basic.Message; +import java.io.File; + +public class HelloBot extends LongPollBot { + @Override + public void onMessageNew(MessageNew messageNew) { + try { + Message message = messageNew.getMessage(); + vk.messages.send() + .setPeerId(message.getPeerId()) + .setMessage("Sending some files to you...") + .addPhoto(new File("your_photo.png")) // to send photo as photo + .addDoc(new File("your_photo.png")) // to send photo as document + .execute(); + } catch (VkApiException e) { + e.printStackTrace(); + } + } } ``` + ## More Examples + Find more examples of bot usage [here](https://github.com/yvasyliev/java-vk-bots-long-poll-api-examples). + ## Async execution + Each API method can be executed asynchronously: + ```java -CompletableFuture future = vk.messages.send() +CompletableFuturefuture=vk.messages.send() .setPeerId(peerId) .setMessage("Sending message asynchronously...") .executeAsync(); ``` + ## Bot events + `LongPollBot` can handle the next events: | VK event | Handler method | @@ -146,6 +185,8 @@ CompletableFuture future = vk.messages.send() | `wall_repost` | `public void onWallRepost(WallPost wallPost)` | ## Logging -This library uses `SLF4J` API to log all events. You can add any `SLF4J` binding to your project to register events the way you want. + +This library uses `SLF4J` API to log all events. You can add any `SLF4J` binding to your project to register events the +way you want. It is highly recommended enabling `DEBUG` log level to see sent and received data. From d6aee5d0fb0f1389c66e477fe14dd86a74f33fec Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Tue, 7 Mar 2023 13:37:04 +0200 Subject: [PATCH 5/6] updated README.md --- README.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2c8dbfb6..3ca1c694 100644 --- a/README.md +++ b/README.md @@ -126,10 +126,21 @@ Find more examples of bot usage [here](https://github.com/yvasyliev/java-vk-bots Each API method can be executed asynchronously: ```java -CompletableFuturefuture=vk.messages.send() - .setPeerId(peerId) - .setMessage("Sending message asynchronously...") - .executeAsync(); +import api.longpoll.bots.LongPollBot; +import api.longpoll.bots.exceptions.VkApiException; +import api.longpoll.bots.methods.impl.messages.Send;import api.longpoll.bots.model.events.messages.MessageNew; +import api.longpoll.bots.model.objects.basic.Message; +import java.io.File;import java.util.concurrent.CompletableFuture; + +public class HelloBot extends LongPollBot { + @Override + public void onMessageNew(MessageNew messageNew) { + CompletableFuture future = vk.messages.send() + .setPeerId(message.getPeerId()) + .setMessage("Sending message asynchronously...") + .executeAsync(); + } +} ``` ## Bot events From 50bc974b20525acc143fd05f13d42cd8b655ec6c Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Tue, 7 Mar 2023 13:38:35 +0200 Subject: [PATCH 6/6] artifact version update --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ca1c694..34ae9d44 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ This library uses the next third-party dependencies: com.github.yvasyliev java-vk-bots-longpoll-api - 4.0.0 + 4.1.0 ``` 4. Extend `LongPollBot` class and override necessary methods: diff --git a/pom.xml b/pom.xml index 075f8562..767e0945 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.yvasyliev java-vk-bots-longpoll-api jar - 4.0.0 + 4.1.0 Java VK Bots Long Poll API A Java library to create VK bots using Bots Long Poll API https://github.com/yvasyliev/java-vk-bots-long-poll-api