mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
commit
c7ba6c1028
@ -1,5 +1,12 @@
|
|||||||
# TelegramBotAPI changelog
|
# TelegramBotAPI changelog
|
||||||
|
|
||||||
|
## 0.11.0
|
||||||
|
|
||||||
|
* Kotlin `1.3.11` -> `1.3.21`
|
||||||
|
* Kotlin coroutines `1.1.0` -> `1.1.1`
|
||||||
|
* Kotlin serialization `0.9.1` -> `0.10.0`
|
||||||
|
* Ktor `1.1.1` -> `1.1.2`
|
||||||
|
|
||||||
## 0.10.0
|
## 0.10.0
|
||||||
|
|
||||||
* Most part of abstractions was replaced from `requests` and `types` on more high level
|
* Most part of abstractions was replaced from `requests` and `types` on more high level
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
project.version = "0.10.3"
|
project.version = "0.11.0"
|
||||||
project.group = "com.github.insanusmokrassar"
|
project.group = "com.github.insanusmokrassar"
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
@ -40,3 +40,9 @@ dependencies {
|
|||||||
// Use JUnit test framework
|
// Use JUnit test framework
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compileKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
freeCompilerArgs = [ disableImplicitReflectionSerializerAnnotation ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
kotlin_version=1.3.11
|
kotlin_version=1.3.21
|
||||||
kotlin_coroutines_version=1.1.0
|
kotlin_coroutines_version=1.1.1
|
||||||
kotlin_serialisation_runtime_version=0.9.1
|
kotlin_serialisation_runtime_version=0.10.0
|
||||||
joda_time_version=2.10.1
|
joda_time_version=2.10.1
|
||||||
ktor_version=1.1.1
|
ktor_version=1.1.2
|
||||||
|
|
||||||
gradle_bintray_plugin_version=1.8.4
|
gradle_bintray_plugin_version=1.8.4
|
||||||
|
|
||||||
|
disableImplicitReflectionSerializerAnnotation=-Xexperimental=kotlinx.serialization.ImplicitReflectionSerializer
|
||||||
|
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
|||||||
#Wed Nov 07 21:46:14 CST 2018
|
#Thu Feb 21 12:05:40 HKT 2019
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
|
||||||
|
@ -16,7 +16,7 @@ import io.ktor.client.engine.okhttp.OkHttp
|
|||||||
import io.ktor.util.cio.toByteArray
|
import io.ktor.util.cio.toByteArray
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.io.charsets.Charset
|
import kotlinx.io.charsets.Charset
|
||||||
import kotlinx.serialization.json.JSON
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
class KtorRequestsExecutor(
|
class KtorRequestsExecutor(
|
||||||
token: String,
|
token: String,
|
||||||
@ -25,7 +25,7 @@ class KtorRequestsExecutor(
|
|||||||
callsFactories: List<KtorCallFactory> = emptyList(),
|
callsFactories: List<KtorCallFactory> = emptyList(),
|
||||||
excludeDefaultFactories: Boolean = false,
|
excludeDefaultFactories: Boolean = false,
|
||||||
private val requestsLimiter: RequestLimiter = EmptyLimiter,
|
private val requestsLimiter: RequestLimiter = EmptyLimiter,
|
||||||
private val jsonFormatter: JSON = JSON.nonstrict
|
private val jsonFormatter: Json = Json.nonstrict
|
||||||
) : BaseRequestsExecutor(token, hostUrl) {
|
) : BaseRequestsExecutor(token, hostUrl) {
|
||||||
constructor(
|
constructor(
|
||||||
token: String,
|
token: String,
|
||||||
|
@ -33,7 +33,7 @@ class MultipartRequestCallFactory : KtorCallFactory {
|
|||||||
when (value) {
|
when (value) {
|
||||||
is MultipartFile -> append(
|
is MultipartFile -> append(
|
||||||
key,
|
key,
|
||||||
value.file.inputStream().asInput(),
|
value.file.asInput(),
|
||||||
Headers.build {
|
Headers.build {
|
||||||
append(HttpHeaders.ContentType, value.mimeType)
|
append(HttpHeaders.ContentType, value.mimeType)
|
||||||
append(HttpHeaders.ContentDisposition, "filename=${value.fileId}")
|
append(HttpHeaders.ContentDisposition, "filename=${value.fileId}")
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base
|
package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory
|
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.toJsonWithoutNulls
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.toJsonWithoutNulls
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.call.HttpClientCall
|
import io.ktor.client.call.HttpClientCall
|
||||||
@ -27,7 +26,7 @@ class SimpleRequestCallFactory : KtorCallFactory {
|
|||||||
method = HttpMethod.Post
|
method = HttpMethod.Post
|
||||||
accept(ContentType.Application.Json)
|
accept(ContentType.Application.Json)
|
||||||
|
|
||||||
val content = request.toJsonWithoutNulls().toString()
|
val content = request.toJsonWithoutNulls(SimpleRequestSerializer).toString()
|
||||||
|
|
||||||
body = TextContent(
|
body = TextContent(
|
||||||
content,
|
content,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
|
package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
@ -22,24 +23,23 @@ fun String.toInputFile(): InputFile = FileId(this)
|
|||||||
|
|
||||||
@Serializer(FileId::class)
|
@Serializer(FileId::class)
|
||||||
object FileIdSerializer : KSerializer<FileId> {
|
object FileIdSerializer : KSerializer<FileId> {
|
||||||
override fun serialize(output: Encoder, obj: FileId) = output.encodeString(obj.fileId)
|
override fun serialize(encoder: Encoder, obj: FileId) = encoder.encodeString(obj.fileId)
|
||||||
override fun deserialize(input: Decoder): FileId = FileId(input.decodeString())
|
override fun deserialize(decoder: Decoder): FileId = FileId(decoder.decodeString())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO:: add checks for files size
|
// TODO:: add checks for files size
|
||||||
/**
|
/**
|
||||||
* Contains info about file for sending
|
* Contains info about file for sending
|
||||||
*/
|
*/
|
||||||
|
@Serializable
|
||||||
data class MultipartFile (
|
data class MultipartFile (
|
||||||
val file: File,
|
val file: StorageFile,
|
||||||
val mimeType: String = Files.probeContentType(file.toPath()),
|
val mimeType: String = file.contentType,
|
||||||
val filename: String = file.name
|
val filename: String = file.fileName
|
||||||
) : InputFile() {
|
) : InputFile() {
|
||||||
override val fileId: String by lazy {
|
override val fileId: String = file.generateCustomName()
|
||||||
"${UUID.randomUUID()}.${file.extension}"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun File.toInputFile(): InputFile = MultipartFile(
|
fun File.toInputFile(): InputFile = MultipartFile(
|
||||||
this
|
StorageFile(this)
|
||||||
)
|
)
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
|
package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
|
||||||
|
|
||||||
|
import kotlinx.serialization.ContextSerializer
|
||||||
|
import kotlinx.serialization.KSerializer
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable(SimpleRequestSerializer::class)
|
||||||
interface SimpleRequest<T: Any> : Request<T>
|
interface SimpleRequest<T: Any> : Request<T>
|
||||||
|
|
||||||
|
object SimpleRequestSerializer : KSerializer<SimpleRequest<*>> by ContextSerializer(SimpleRequest::class)
|
||||||
|
@ -4,10 +4,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.ChatRequ
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.toJson
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.internal.BooleanSerializer
|
import kotlinx.serialization.internal.BooleanSerializer
|
||||||
import kotlinx.serialization.json.JsonObject
|
import kotlinx.serialization.json.JsonObject
|
||||||
import kotlinx.serialization.json.JsonTreeMapper
|
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class SetChatPhoto (
|
data class SetChatPhoto (
|
||||||
@ -19,5 +19,5 @@ data class SetChatPhoto (
|
|||||||
override fun method(): String = "setChatPhoto"
|
override fun method(): String = "setChatPhoto"
|
||||||
override fun resultSerializer(): KSerializer<Boolean> = BooleanSerializer
|
override fun resultSerializer(): KSerializer<Boolean> = BooleanSerializer
|
||||||
override val mediaMap: Map<String, MultipartFile> = mapOf(photoField to photo)
|
override val mediaMap: Map<String, MultipartFile> = mapOf(photoField to photo)
|
||||||
override val paramsJson: JsonObject = JsonTreeMapper().writeTree(this, serializer()).jsonObject
|
override val paramsJson: JsonObject = this.toJson(serializer())
|
||||||
}
|
}
|
||||||
|
@ -32,8 +32,8 @@ fun String.asUsername(): Username = Username(this)
|
|||||||
|
|
||||||
@Serializer(ChatIdentifier::class)
|
@Serializer(ChatIdentifier::class)
|
||||||
internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> {
|
internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> {
|
||||||
override fun deserialize(input: Decoder): ChatIdentifier {
|
override fun deserialize(decoder: Decoder): ChatIdentifier {
|
||||||
val id = input.decodeString()
|
val id = decoder.decodeString()
|
||||||
return id.toLongOrNull() ?.let {
|
return id.toLongOrNull() ?.let {
|
||||||
ChatId(it)
|
ChatId(it)
|
||||||
} ?: if (!id.startsWith("@")) {
|
} ?: if (!id.startsWith("@")) {
|
||||||
@ -43,10 +43,10 @@ internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun serialize(output: Encoder, obj: ChatIdentifier) {
|
override fun serialize(encoder: Encoder, obj: ChatIdentifier) {
|
||||||
when (obj) {
|
when (obj) {
|
||||||
is ChatId -> output.encodeString(obj.chatId.toString())
|
is ChatId -> encoder.encodeString(obj.chatId.toString())
|
||||||
is Username -> output.encodeString(obj.username)
|
is Username -> encoder.encodeString(obj.username)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,32 +8,32 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException
|
|||||||
@Serializer(InlineQueryResult::class)
|
@Serializer(InlineQueryResult::class)
|
||||||
object InlineQueryResultSerializer :
|
object InlineQueryResultSerializer :
|
||||||
KSerializer<InlineQueryResult> {
|
KSerializer<InlineQueryResult> {
|
||||||
override fun serialize(output: Encoder, obj: InlineQueryResult) {
|
override fun serialize(encoder: Encoder, obj: InlineQueryResult) {
|
||||||
when(obj) {
|
when(obj) {
|
||||||
is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(output, obj)
|
is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultAudioCachedImpl -> InlineQueryResultAudioCachedImpl.serializer().serialize(output, obj)
|
is InlineQueryResultAudioCachedImpl -> InlineQueryResultAudioCachedImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultAudioImpl -> InlineQueryResultAudioImpl.serializer().serialize(output, obj)
|
is InlineQueryResultAudioImpl -> InlineQueryResultAudioImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultContact -> InlineQueryResultContact.serializer().serialize(output, obj)
|
is InlineQueryResultContact -> InlineQueryResultContact.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultDocumentCachedImpl -> InlineQueryResultDocumentCachedImpl.serializer().serialize(output, obj)
|
is InlineQueryResultDocumentCachedImpl -> InlineQueryResultDocumentCachedImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultDocumentImpl -> InlineQueryResultDocumentImpl.serializer().serialize(output, obj)
|
is InlineQueryResultDocumentImpl -> InlineQueryResultDocumentImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultGame -> InlineQueryResultGame.serializer().serialize(output, obj)
|
is InlineQueryResultGame -> InlineQueryResultGame.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultGifCachedImpl -> InlineQueryResultGifCachedImpl.serializer().serialize(output, obj)
|
is InlineQueryResultGifCachedImpl -> InlineQueryResultGifCachedImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultGifImpl -> InlineQueryResultGifImpl.serializer().serialize(output, obj)
|
is InlineQueryResultGifImpl -> InlineQueryResultGifImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultLocation -> InlineQueryResultLocation.serializer().serialize(output, obj)
|
is InlineQueryResultLocation -> InlineQueryResultLocation.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultMpeg4GifCachedImpl -> InlineQueryResultMpeg4GifCachedImpl.serializer().serialize(output, obj)
|
is InlineQueryResultMpeg4GifCachedImpl -> InlineQueryResultMpeg4GifCachedImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultMpeg4GifImpl -> InlineQueryResultMpeg4GifImpl.serializer().serialize(output, obj)
|
is InlineQueryResultMpeg4GifImpl -> InlineQueryResultMpeg4GifImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultPhotoCachedImpl -> InlineQueryResultPhotoCachedImpl.serializer().serialize(output, obj)
|
is InlineQueryResultPhotoCachedImpl -> InlineQueryResultPhotoCachedImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultPhotoImpl -> InlineQueryResultPhotoImpl.serializer().serialize(output, obj)
|
is InlineQueryResultPhotoImpl -> InlineQueryResultPhotoImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultStickerCached -> InlineQueryResultStickerCached.serializer().serialize(output, obj)
|
is InlineQueryResultStickerCached -> InlineQueryResultStickerCached.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultVenue -> InlineQueryResultVenue.serializer().serialize(output, obj)
|
is InlineQueryResultVenue -> InlineQueryResultVenue.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultVideoCachedImpl -> InlineQueryResultVideoCachedImpl.serializer().serialize(output, obj)
|
is InlineQueryResultVideoCachedImpl -> InlineQueryResultVideoCachedImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultVideoImpl -> InlineQueryResultVideoImpl.serializer().serialize(output, obj)
|
is InlineQueryResultVideoImpl -> InlineQueryResultVideoImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultVoiceCachedImpl -> InlineQueryResultVoiceCachedImpl.serializer().serialize(output, obj)
|
is InlineQueryResultVoiceCachedImpl -> InlineQueryResultVoiceCachedImpl.serializer().serialize(encoder, obj)
|
||||||
is InlineQueryResultVoiceImpl -> InlineQueryResultVoiceImpl.serializer().serialize(output, obj)
|
is InlineQueryResultVoiceImpl -> InlineQueryResultVoiceImpl.serializer().serialize(encoder, obj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deserialize(input: Decoder): InlineQueryResult {
|
override fun deserialize(decoder: Decoder): InlineQueryResult {
|
||||||
throw NotImplementedException()
|
throw NotImplementedException()
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InputMessageContent.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts.InputMessageContent
|
||||||
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
|
@Serializer(InputMessageContent::class)
|
||||||
|
object InputMessageContentSerializer : KSerializer<InputMessageContent> {
|
||||||
|
override fun serialize(encoder: Encoder, obj: InputMessageContent) {
|
||||||
|
when (obj) {
|
||||||
|
is InputContactMessageContent -> InputContactMessageContent.serializer().serialize(encoder, obj)
|
||||||
|
is InputLocationMessageContent -> InputLocationMessageContent.serializer().serialize(encoder, obj)
|
||||||
|
is InputTextMessageContent -> InputTextMessageContent.serializer().serialize(encoder, obj)
|
||||||
|
is InputVenueMessageContent -> InputVenueMessageContent.serializer().serialize(encoder, obj)
|
||||||
|
else -> throw IllegalArgumentException("Unknown for serializing InputContactMessageContent")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun deserialize(decoder: Decoder): InputMessageContent = throw IllegalStateException("Object can't be deserialized")
|
||||||
|
}
|
@ -1,3 +1,7 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts
|
package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InputMessageContentSerializer
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable(InputMessageContentSerializer::class)
|
||||||
interface InputMessageContent
|
interface InputMessageContent
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia
|
package com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
const val inputMediaFileAttachmentNameTemplate = "attach://%s"
|
const val inputMediaFileAttachmentNameTemplate = "attach://%s"
|
||||||
|
|
||||||
|
@Serializable(InputMediaSerializer::class)
|
||||||
interface InputMedia {
|
interface InputMedia {
|
||||||
val type: String
|
val type: String
|
||||||
val file: InputFile
|
val file: InputFile
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia
|
||||||
|
|
||||||
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
|
@Serializer(InputMedia::class)
|
||||||
|
object InputMediaSerializer : KSerializer<InputMedia> {
|
||||||
|
override fun serialize(encoder: Encoder, obj: InputMedia) {
|
||||||
|
when (obj) {
|
||||||
|
is InputMediaVideo -> InputMediaVideo.serializer().serialize(encoder, obj)
|
||||||
|
is InputMediaAudio -> InputMediaAudio.serializer().serialize(encoder, obj)
|
||||||
|
is InputMediaPhoto -> InputMediaPhoto.serializer().serialize(encoder, obj)
|
||||||
|
is InputMediaAnimation -> InputMediaAnimation.serializer().serialize(encoder, obj)
|
||||||
|
is InputMediaDocument -> InputMediaDocument.serializer().serialize(encoder, obj)
|
||||||
|
else -> throw IllegalArgumentException("Can't perform and serialize $obj")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun deserialize(decoder: Decoder): InputMedia {
|
||||||
|
throw IllegalStateException("Object can't be deserialized")
|
||||||
|
}
|
||||||
|
}
|
@ -25,8 +25,8 @@ object HTMLParseMode : ParseMode() {
|
|||||||
|
|
||||||
@Serializer(ParseMode::class)
|
@Serializer(ParseMode::class)
|
||||||
internal class ParseModeSerializerObject: KSerializer<ParseMode> {
|
internal class ParseModeSerializerObject: KSerializer<ParseMode> {
|
||||||
override fun deserialize(input: Decoder): ParseMode {
|
override fun deserialize(decoder: Decoder): ParseMode {
|
||||||
val mode = input.decodeString()
|
val mode = decoder.decodeString()
|
||||||
return when (mode) {
|
return when (mode) {
|
||||||
MarkdownParseMode.parseModeName -> MarkdownParseMode
|
MarkdownParseMode.parseModeName -> MarkdownParseMode
|
||||||
HTMLParseMode.parseModeName -> HTMLParseMode
|
HTMLParseMode.parseModeName -> HTMLParseMode
|
||||||
@ -34,7 +34,7 @@ internal class ParseModeSerializerObject: KSerializer<ParseMode> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun serialize(output: Encoder, obj: ParseMode) {
|
override fun serialize(encoder: Encoder, obj: ParseMode) {
|
||||||
output.encodeString(obj.parseModeName)
|
encoder.encodeString(obj.parseModeName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,15 +22,15 @@ fun DateTime.toTelegramDate(): TelegramDate = TelegramDate(this)
|
|||||||
|
|
||||||
@Serializer(TelegramDate::class)
|
@Serializer(TelegramDate::class)
|
||||||
internal class TelegramDateSerializer: KSerializer<TelegramDate> {
|
internal class TelegramDateSerializer: KSerializer<TelegramDate> {
|
||||||
override fun serialize(output: Encoder, obj: TelegramDate) {
|
override fun serialize(encoder: Encoder, obj: TelegramDate) {
|
||||||
output.encodeLong(
|
encoder.encodeLong(
|
||||||
TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis)
|
TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deserialize(input: Decoder): TelegramDate {
|
override fun deserialize(decoder: Decoder): TelegramDate {
|
||||||
return TelegramDate(
|
return TelegramDate(
|
||||||
input.decodeLong()
|
decoder.decodeLong()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,12 +20,12 @@ sealed class BotAction {
|
|||||||
|
|
||||||
@Serializer(BotAction::class)
|
@Serializer(BotAction::class)
|
||||||
class BotActionSerializer: KSerializer<BotAction> {
|
class BotActionSerializer: KSerializer<BotAction> {
|
||||||
override fun serialize(output: Encoder, obj: BotAction) {
|
override fun serialize(encoder: Encoder, obj: BotAction) {
|
||||||
output.encodeString(obj.actionName)
|
encoder.encodeString(obj.actionName)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deserialize(input: Decoder): BotAction {
|
override fun deserialize(decoder: Decoder): BotAction {
|
||||||
val actionName = input.decodeString()
|
val actionName = decoder.decodeString()
|
||||||
return actions.firstOrNull { it.actionName == actionName } ?: throw IllegalStateException("Unknown action type: $actionName")
|
return actions.firstOrNull { it.actionName == actionName } ?: throw IllegalStateException("Unknown action type: $actionName")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons
|
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
|
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.internal.ArrayListSerializer
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class InlineKeyboardMarkup(
|
data class InlineKeyboardMarkup(
|
||||||
@SerialName("inline_keyboard")
|
@SerialName("inline_keyboard")
|
||||||
|
@Serializable(with = KeyboardSerializer::class)
|
||||||
val keyboard: Matrix<InlineKeyboardButton>
|
val keyboard: Matrix<InlineKeyboardButton>
|
||||||
) : KeyboardMarkup
|
) : KeyboardMarkup
|
||||||
|
|
||||||
|
object KeyboardSerializer : KSerializer<Matrix<InlineKeyboardButton>> by ArrayListSerializer(
|
||||||
|
ArrayListSerializer(ContextSerializer(InlineKeyboardButton::class))
|
||||||
|
)
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons
|
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable(KeyboardMarkupSerializer::class)
|
||||||
interface KeyboardMarkup
|
interface KeyboardMarkup
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons
|
||||||
|
|
||||||
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
|
@Serializer(KeyboardMarkup::class)
|
||||||
|
object KeyboardMarkupSerializer : KSerializer<KeyboardMarkup> {
|
||||||
|
override fun serialize(encoder: Encoder, obj: KeyboardMarkup) {
|
||||||
|
when(obj) {
|
||||||
|
is ForceReply -> ForceReply.serializer().serialize(encoder, obj)
|
||||||
|
is InlineKeyboardMarkup -> InlineKeyboardMarkup.serializer().serialize(encoder, obj)
|
||||||
|
is ReplyKeyboardMarkup -> ReplyKeyboardMarkup.serializer().serialize(encoder, obj)
|
||||||
|
is ReplyKeyboardRemove -> ReplyKeyboardRemove.serializer().serialize(encoder, obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,12 +9,12 @@ interface Currencied {
|
|||||||
|
|
||||||
@Serializer(Currency::class)
|
@Serializer(Currency::class)
|
||||||
object CurrencySerializer : KSerializer<Currency> {
|
object CurrencySerializer : KSerializer<Currency> {
|
||||||
override fun serialize(output: Encoder, obj: Currency) {
|
override fun serialize(encoder: Encoder, obj: Currency) {
|
||||||
output.encodeString(obj.currencyCode)
|
encoder.encodeString(obj.currencyCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deserialize(input: Decoder): Currency {
|
override fun deserialize(decoder: Decoder): Currency {
|
||||||
return input.decodeString().let {
|
return decoder.decodeString().let {
|
||||||
Currency.getInstance(it)
|
Currency.getInstance(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,15 @@ import kotlinx.serialization.*
|
|||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.json.*
|
||||||
|
|
||||||
@ImplicitReflectionSerializer
|
@ImplicitReflectionSerializer
|
||||||
inline fun <reified T: Any> T.toJsonWithoutNulls(): JsonObject = toJson(serializerByValue(this)).withoutNulls()
|
inline fun <reified T: Any> T.toJsonWithoutNulls(): JsonObject = Json.nonstrict.toJson(
|
||||||
|
this
|
||||||
|
).jsonObject.withoutNulls()
|
||||||
|
|
||||||
inline fun <reified T: Any> T.toJsonWithoutNulls(serializer: KSerializer<T>): JsonObject = toJson(serializer).withoutNulls()
|
inline fun <reified T: Any> T.toJsonWithoutNulls(serializer: KSerializer<T>): JsonObject = toJson(serializer).withoutNulls()
|
||||||
|
|
||||||
inline fun <reified T: Any> T.toJson(serializer: KSerializer<T>): JsonObject = JsonTreeMapper().writeTree(
|
inline fun <reified T: Any> T.toJson(serializer: KSerializer<T>): JsonObject = Json.nonstrict.toJson(
|
||||||
this,
|
serializer,
|
||||||
serializer
|
this
|
||||||
).jsonObject
|
).jsonObject
|
||||||
|
|
||||||
fun JsonArray.withoutNulls(): JsonArray {
|
fun JsonArray.withoutNulls(): JsonArray {
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||||
|
|
||||||
|
import kotlinx.io.core.Input
|
||||||
|
import kotlinx.io.streams.asInput
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.Transient
|
||||||
|
import java.io.File
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class StorageFile(
|
||||||
|
@Transient
|
||||||
|
private val file: File = throw IllegalStateException("Can't create object without file")
|
||||||
|
) {
|
||||||
|
val contentType: String = Files.probeContentType(file.toPath())
|
||||||
|
val fileName: String = file.name
|
||||||
|
fun generateCustomName(): String = "${UUID.randomUUID()}.${file.extension}"
|
||||||
|
fun asInput(): Input = Files.newInputStream(file.toPath()).asInput()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user