A Java library to create VK bots using Bots Long Poll API.
An easy-to-use and lightweight Java library that implements Bots Long Poll API.
This library does not promise to implement all possible public VK APIs. Used API version: 5.118
.
This library keeps on improving. Feel free to create issues or pull requests.
This library uses the next third-party dependencies:
- Jsoup
- Gson
- SLF4J
- Java 8+
- Maven (or other build tool)
- Create VK Community and get
group_id
(from browser URL). - Go to
Manage
-API usage
-Access tokens
and createaccess_token
. - Add the library to your project using Maven:
<dependency>
<groupId>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<version>1.5.3</version>
</dependency>
- Extend
LongPollBot
class and override necessary methods:
public class HelloBot extends LongPollBot {
@Override
public void onMessageNew(MessageNewEvent messageNewEvent) {
try {
Message message = messageNewEvent.getMessage();
if (message.hasText()) {
String response = "Hello! Received your message: " + message.getText();
new MessagesSend(getAccessToken())
.setPeerId(message.getPeerId())
.setMessage(response)
.execute();
}
} catch (BotsLongPollException e) {
e.printStackTrace();
}
}
@Override
public String getAccessToken() {
return "your_access_token";
}
@Override
public int getGroupId() {
return 999999999;
}
public static void main(String[] args) throws BotsLongPollException {
new BotsLongPoll(new HelloBot()).run();
}
}
Each API method can be executed asynchronously:
new MessagesSend(getAccessToken())
.setPeerId(peerId)
.setMessage("Sending message asynchronously...")
.executeAsync();
executeAsync()
method returns CompletableFuture<T>
result.
LongPollBot
supports next event handlers:
onAppPayload(AppPayload appPayload)
onAudioNew(Audio audio)
onBoardPostDelete(BoardPostDeleteEvent boardPostDeleteEvent)
onBoardPostEdit(BoardPostEvent boardPostEvent)
onBoardPostNew(BoardPostEvent boardPostEvent)
onBoardPostRestore(BoardPostEvent boardPostEvent)
onGroupChangePhoto(GroupChangePhotoEvent groupChangePhotoEvent)
onGroupChangeSettings(GroupChangeSettingsEvent groupChangeSettingsEvent)
onGroupJoin(GroupJoinEvent groupJoinEvent)
onGroupLeave(GroupLeaveEvent groupLeaveEvent)
onLikeAdd(LikeEvent likeEvent)
onLikeRemove(LikeEvent likeEvent)
onMarketCommentDelete(MarketCommentDeleteEvent marketCommentDeleteEvent)
onMarketCommentEdit(MarketCommentEvent marketCommentEvent)
onMarketCommentNew(MarketCommentEvent marketCommentEvent)
onMarketCommentRestore(MarketCommentEvent marketCommentEvent)
onMarketOrderEdit(MarketOrder marketOrder)
onMarketOrderNew(MarketOrder marketOrder)
onMessageAllow(MessageAllowEvent messageAllowEvent)
onMessageDeny(MessageDenyEvent messageDenyEvent)
onMessageEdit(Message message)
onMessageEvent(MessageEvent messageEvent)
onMessageNew(MessageNewEvent messageNewEvent)
onMessageReply(Message message)
onMessageTypingState(MessageTypingStateEvent messageTypingStateEvent)
onPhotoCommentDelete(PhotoCommentDeleteEvent photoCommentDeleteEvent)
onPhotoCommentEdit(PhotoCommentEvent photoCommentEvent)
onPhotoCommentNew(PhotoCommentEvent photoCommentEvent)
onPhotoCommentRestore(PhotoCommentEvent photoCommentEvent)
onPhotoNew(Photo photo)
onUserBlock(UserBlockEvent userBlockEvent)
onUserUnblock(UserUnblockEvent userUnblockEvent)
onVideoCommentDelete(VideoCommentDeleteEvent videoCommentDeleteEvent)
onVideoCommentEdit(VideoCommentEvent videoCommentEvent)
onVideoCommentNew(VideoCommentEvent videoCommentEvent)
onVideoCommentRestore(VideoCommentEvent videoCommentEvent)
onVideoNew(Video video)
onVkpayTransaction(VkpayTransaction vkpayTransaction)
onWallPostNew(WallPost wallPost)
onWallReplyDelete(WallReplyDeleteEvent wallReplyDeleteEvent)
onWallReplyEdit(WallReplyEvent wallReplyEvent)
onWallReplyNew(WallReplyEvent wallReplyEvent)
onWallReplyRestore(WallReplyEvent wallReplyEvent)
onWallRepost(WallPost wallPost)
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.
Check out usage examples of Java VK Bots Long Poll API library.