com.github.stormbit.vksdk.clients.Client.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vk-bot-sdk-kotlin Show documentation
Show all versions of vk-bot-sdk-kotlin Show documentation
The Kotlin library for working with VK api
The newest version!
package com.github.stormbit.vksdk.clients
import com.github.stormbit.vksdk.events.Event
import com.github.stormbit.vksdk.longpoll.LongPoll
import com.github.stormbit.vksdk.objects.Chat
import com.github.stormbit.vksdk.objects.Message
import com.github.stormbit.vksdk.objects.upload.Upload
import com.github.stormbit.vksdk.utils.json
import com.github.stormbit.vksdk.utils.send
import com.github.stormbit.vksdk.vkapi.*
import com.github.stormbit.vksdk.vkapi.methods.docs.DocsApi
import com.github.stormbit.vksdk.vkapi.methods.friends.FriendsApi
import com.github.stormbit.vksdk.vkapi.methods.groups.GroupsApi
import com.github.stormbit.vksdk.vkapi.methods.likes.LikesApi
import com.github.stormbit.vksdk.vkapi.methods.messages.MessagesApi
import com.github.stormbit.vksdk.vkapi.methods.photos.PhotosApi
import com.github.stormbit.vksdk.vkapi.methods.users.UsersApi
import com.github.stormbit.vksdk.vkapi.methods.video.VideoApi
import com.github.stormbit.vksdk.vkapi.methods.wall.WallApi
import io.ktor.client.*
import io.ktor.client.features.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.http.*
import kotlinx.serialization.KSerializer
import org.slf4j.Logger
import org.slf4j.LoggerFactory
abstract class Client(internal var httpClient: HttpClient) {
internal val log: Logger = LoggerFactory.getLogger(Client::class.java)
init {
httpClient = httpClient.configure()
}
var id = 0
protected set
abstract val longPoll: LongPoll
internal var token: String? = null
internal var baseParams = Parameters.Empty
internal var headers = Parameters.Empty
/**
* Api methods
*/
abstract val messages: MessagesApi
abstract val groups: GroupsApi
abstract val users: UsersApi
abstract val photos: PhotosApi
abstract val video: VideoApi
abstract val docs: DocsApi
abstract val friends: FriendsApi
abstract val likes: LikesApi
abstract val wall: WallApi
abstract val upload: Upload
internal var messageHandler: DefaultMessageRoute? = null
suspend fun sendMessage(block: Message.() -> Unit): Int {
val message = Message(block)
return message.send(this)
}
fun startLongPoll() {
longPoll.start()
}
suspend fun onMessage(block: (suspend DefaultMessageRoute.() -> Unit)) {
messageHandler = DefaultMessageRoute(ApiContext(this), null, emptyList())
messageHandler?.block()
}
/**
* If the client need to start typing
* after receiving message
* and until client's message is sent
*
* @param enable true or false
*/
fun enableTyping(enable: Boolean) = longPoll.enableTyping(enable)
inline fun on(noinline callback: suspend T.() -> Unit) =
longPoll.registerEvent(callback, T::class)
fun chat(chatId: Int): Chat {
return Chat(this, chatId)
}
fun execute(code: String, serializer: KSerializer) = VkApiRequest(
this,
HttpMethod.Get,
"execute",
parametersOf("code", code),
serializer
)
private fun HttpClient.configure() = config {
expectSuccess = false
install(HttpTimeout)
install(JsonFeature) {
serializer = KotlinxSerializer(json)
}
}
override fun toString(): String = "{\"id\": $id}"
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy