Unofficial VK SDK, written in Kotlin: make bots, use the API
MIT License
Unofficial VK.com SDK, written in Kotlin. Based on Kotlin Multiplatform project, but has Java-friendly API.
See the documentation: https://vksdk.github.io/vk-sdk-kotlin
Latest version:
For Kotlin 1.3.72 use version 0.0.5 and below.
// From here: https://vk.com/club151083290 take the ID
val groupId = 151083290
// Read more: https://vk.com/dev/access_token
val accessToken = "abcdef123456..."
// There are two http clients available for now:
// JVM-only OkHttp-based
// and common ktor-based
val vkHttpClient = VkOkHttpClient()
val client = VkApiClient(groupId, accessToken, VkApiClient.Type.Community, VkSettings(vkHttpClient))
client.onMessage { messageEvent ->
client.sendMessage {
peerId = messageEvent.message.peerId
message = "Hello, World!"
// You can use stickers, replies, location, etc.
// All of the message parameters are supported.
}.execute()
}
client.startLongPolling()
int groupId = 151083290;
String accessToken = "abcdef123456...";
HttpClient vkHttpClient = new VkOkHttpClient();
VkApiClient client = new VkApiClient(groupId, accessToken, VkApiClient.Type.Community, new VkSettings(vkHttpClient));
client.onMessage(event -> {
new Message()
.peerId(event.getMessage().getPeerId())
.text("Hello, world!")
.sendFrom(client)
.execute();
});
client.startLongPolling();
The primary goal of the library is to cover most of the possible needs but in an abstract way. You can't find here a hundred of pre-defined data classes for each API method, but you can write the highly customizable solution in most convenient way.
examples
directory and the documentation.code
or from access_token
.MessageNew
for message_new
eventJsonElement
for all the other eventsmessages.send
: use DSL for sending messages and building keyboards. All method capabilities are covered.
Library is uploaded to the Maven Central Repository.
For Gradle 6.0+ or with metadata enabled, add the dependencies in this way:
// core module is required
implementation "com.petersamokhin.vksdk:core:$vkSdkVersion"
// One of the HTTP clients is also required.
// You can use pre-defined OkHttp-based client, but only for JVM.
implementation "com.petersamokhin.vksdk:http-client-jvm-okhttp:$vkSdkVersion"
// Or else you can use the common HTTP client, which is based on ktor
// and available for all of the platforms, including JVM.
// In this case, you also must specify the ktor engine.
implementation "com.petersamokhin.vksdk:http-client-common-ktor:$vkSdkVersion"
Otherwise, add enableFeaturePreview("GRADLE_METADATA")
to settings.gradle
or else you should specify the platform.
Example for JVM:
implementation "com.petersamokhin.vksdk:core-jvm:$vkSdkVersion"
implementation "com.petersamokhin.vksdk:http-client-common-ktor-jvm:$vkSdkVersion"
// and OkHttp-based client is already JVM-only
implementation "com.petersamokhin.vksdk:http-client-jvm-okhttp:$vkSdkVersion"
linuxArm32Hfp
, linuxMips32
js
platform
See the License