1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

Merge pull request #21 from InsanusMokrassar/0.11.0

0.11.0
This commit is contained in:
InsanusMokrassar 2019-02-21 15:57:39 +08:00 committed by GitHub
commit c7ba6c1028
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 186 additions and 73 deletions

View File

@ -1,5 +1,12 @@
# 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
* Most part of abstractions was replaced from `requests` and `types` on more high level

View File

@ -1,4 +1,4 @@
project.version = "0.10.3"
project.version = "0.11.0"
project.group = "com.github.insanusmokrassar"
buildscript {
@ -40,3 +40,9 @@ dependencies {
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = [ disableImplicitReflectionSerializerAnnotation ]
}
}

View File

@ -1,8 +1,10 @@
kotlin.code.style=official
kotlin_version=1.3.11
kotlin_coroutines_version=1.1.0
kotlin_serialisation_runtime_version=0.9.1
kotlin_version=1.3.21
kotlin_coroutines_version=1.1.1
kotlin_serialisation_runtime_version=0.10.0
joda_time_version=2.10.1
ktor_version=1.1.1
ktor_version=1.1.2
gradle_bintray_plugin_version=1.8.4
disableImplicitReflectionSerializerAnnotation=-Xexperimental=kotlinx.serialization.ImplicitReflectionSerializer

View File

@ -1,6 +1,6 @@
#Wed Nov 07 21:46:14 CST 2018
#Thu Feb 21 12:05:40 HKT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
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

View File

@ -16,7 +16,7 @@ import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.util.cio.toByteArray
import kotlinx.coroutines.delay
import kotlinx.io.charsets.Charset
import kotlinx.serialization.json.JSON
import kotlinx.serialization.json.Json
class KtorRequestsExecutor(
token: String,
@ -25,7 +25,7 @@ class KtorRequestsExecutor(
callsFactories: List<KtorCallFactory> = emptyList(),
excludeDefaultFactories: Boolean = false,
private val requestsLimiter: RequestLimiter = EmptyLimiter,
private val jsonFormatter: JSON = JSON.nonstrict
private val jsonFormatter: Json = Json.nonstrict
) : BaseRequestsExecutor(token, hostUrl) {
constructor(
token: String,

View File

@ -33,7 +33,7 @@ class MultipartRequestCallFactory : KtorCallFactory {
when (value) {
is MultipartFile -> append(
key,
value.file.inputStream().asInput(),
value.file.asInput(),
Headers.build {
append(HttpHeaders.ContentType, value.mimeType)
append(HttpHeaders.ContentDisposition, "filename=${value.fileId}")

View File

@ -1,8 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.toJsonWithoutNulls
import io.ktor.client.HttpClient
import io.ktor.client.call.HttpClientCall
@ -27,7 +26,7 @@ class SimpleRequestCallFactory : KtorCallFactory {
method = HttpMethod.Post
accept(ContentType.Application.Json)
val content = request.toJsonWithoutNulls().toString()
val content = request.toJsonWithoutNulls(SimpleRequestSerializer).toString()
body = TextContent(
content,

View File

@ -1,5 +1,6 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile
import kotlinx.serialization.*
import java.io.File
import java.nio.file.Files
@ -22,24 +23,23 @@ fun String.toInputFile(): InputFile = FileId(this)
@Serializer(FileId::class)
object FileIdSerializer : KSerializer<FileId> {
override fun serialize(output: Encoder, obj: FileId) = output.encodeString(obj.fileId)
override fun deserialize(input: Decoder): FileId = FileId(input.decodeString())
override fun serialize(encoder: Encoder, obj: FileId) = encoder.encodeString(obj.fileId)
override fun deserialize(decoder: Decoder): FileId = FileId(decoder.decodeString())
}
// TODO:: add checks for files size
/**
* Contains info about file for sending
*/
@Serializable
data class MultipartFile (
val file: File,
val mimeType: String = Files.probeContentType(file.toPath()),
val filename: String = file.name
val file: StorageFile,
val mimeType: String = file.contentType,
val filename: String = file.fileName
) : InputFile() {
override val fileId: String by lazy {
"${UUID.randomUUID()}.${file.extension}"
}
override val fileId: String = file.generateCustomName()
}
fun File.toInputFile(): InputFile = MultipartFile(
this
StorageFile(this)
)

View File

@ -1,3 +1,10 @@
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>
object SimpleRequestSerializer : KSerializer<SimpleRequest<*>> by ContextSerializer(SimpleRequest::class)

View File

@ -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.MultipartRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.toJson
import kotlinx.serialization.*
import kotlinx.serialization.internal.BooleanSerializer
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonTreeMapper
@Serializable
data class SetChatPhoto (
@ -19,5 +19,5 @@ data class SetChatPhoto (
override fun method(): String = "setChatPhoto"
override fun resultSerializer(): KSerializer<Boolean> = BooleanSerializer
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())
}

View File

@ -32,8 +32,8 @@ fun String.asUsername(): Username = Username(this)
@Serializer(ChatIdentifier::class)
internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> {
override fun deserialize(input: Decoder): ChatIdentifier {
val id = input.decodeString()
override fun deserialize(decoder: Decoder): ChatIdentifier {
val id = decoder.decodeString()
return id.toLongOrNull() ?.let {
ChatId(it)
} ?: 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) {
is ChatId -> output.encodeString(obj.chatId.toString())
is Username -> output.encodeString(obj.username)
is ChatId -> encoder.encodeString(obj.chatId.toString())
is Username -> encoder.encodeString(obj.username)
}
}
}

View File

@ -8,32 +8,32 @@ import sun.reflect.generics.reflectiveObjects.NotImplementedException
@Serializer(InlineQueryResult::class)
object InlineQueryResultSerializer :
KSerializer<InlineQueryResult> {
override fun serialize(output: Encoder, obj: InlineQueryResult) {
override fun serialize(encoder: Encoder, obj: InlineQueryResult) {
when(obj) {
is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(output, obj)
is InlineQueryResultAudioCachedImpl -> InlineQueryResultAudioCachedImpl.serializer().serialize(output, obj)
is InlineQueryResultAudioImpl -> InlineQueryResultAudioImpl.serializer().serialize(output, obj)
is InlineQueryResultContact -> InlineQueryResultContact.serializer().serialize(output, obj)
is InlineQueryResultDocumentCachedImpl -> InlineQueryResultDocumentCachedImpl.serializer().serialize(output, obj)
is InlineQueryResultDocumentImpl -> InlineQueryResultDocumentImpl.serializer().serialize(output, obj)
is InlineQueryResultGame -> InlineQueryResultGame.serializer().serialize(output, obj)
is InlineQueryResultGifCachedImpl -> InlineQueryResultGifCachedImpl.serializer().serialize(output, obj)
is InlineQueryResultGifImpl -> InlineQueryResultGifImpl.serializer().serialize(output, obj)
is InlineQueryResultLocation -> InlineQueryResultLocation.serializer().serialize(output, obj)
is InlineQueryResultMpeg4GifCachedImpl -> InlineQueryResultMpeg4GifCachedImpl.serializer().serialize(output, obj)
is InlineQueryResultMpeg4GifImpl -> InlineQueryResultMpeg4GifImpl.serializer().serialize(output, obj)
is InlineQueryResultPhotoCachedImpl -> InlineQueryResultPhotoCachedImpl.serializer().serialize(output, obj)
is InlineQueryResultPhotoImpl -> InlineQueryResultPhotoImpl.serializer().serialize(output, obj)
is InlineQueryResultStickerCached -> InlineQueryResultStickerCached.serializer().serialize(output, obj)
is InlineQueryResultVenue -> InlineQueryResultVenue.serializer().serialize(output, obj)
is InlineQueryResultVideoCachedImpl -> InlineQueryResultVideoCachedImpl.serializer().serialize(output, obj)
is InlineQueryResultVideoImpl -> InlineQueryResultVideoImpl.serializer().serialize(output, obj)
is InlineQueryResultVoiceCachedImpl -> InlineQueryResultVoiceCachedImpl.serializer().serialize(output, obj)
is InlineQueryResultVoiceImpl -> InlineQueryResultVoiceImpl.serializer().serialize(output, obj)
is InlineQueryResultArticle -> InlineQueryResultArticle.serializer().serialize(encoder, obj)
is InlineQueryResultAudioCachedImpl -> InlineQueryResultAudioCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultAudioImpl -> InlineQueryResultAudioImpl.serializer().serialize(encoder, obj)
is InlineQueryResultContact -> InlineQueryResultContact.serializer().serialize(encoder, obj)
is InlineQueryResultDocumentCachedImpl -> InlineQueryResultDocumentCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultDocumentImpl -> InlineQueryResultDocumentImpl.serializer().serialize(encoder, obj)
is InlineQueryResultGame -> InlineQueryResultGame.serializer().serialize(encoder, obj)
is InlineQueryResultGifCachedImpl -> InlineQueryResultGifCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultGifImpl -> InlineQueryResultGifImpl.serializer().serialize(encoder, obj)
is InlineQueryResultLocation -> InlineQueryResultLocation.serializer().serialize(encoder, obj)
is InlineQueryResultMpeg4GifCachedImpl -> InlineQueryResultMpeg4GifCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultMpeg4GifImpl -> InlineQueryResultMpeg4GifImpl.serializer().serialize(encoder, obj)
is InlineQueryResultPhotoCachedImpl -> InlineQueryResultPhotoCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultPhotoImpl -> InlineQueryResultPhotoImpl.serializer().serialize(encoder, obj)
is InlineQueryResultStickerCached -> InlineQueryResultStickerCached.serializer().serialize(encoder, obj)
is InlineQueryResultVenue -> InlineQueryResultVenue.serializer().serialize(encoder, obj)
is InlineQueryResultVideoCachedImpl -> InlineQueryResultVideoCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultVideoImpl -> InlineQueryResultVideoImpl.serializer().serialize(encoder, obj)
is InlineQueryResultVoiceCachedImpl -> InlineQueryResultVoiceCachedImpl.serializer().serialize(encoder, obj)
is InlineQueryResultVoiceImpl -> InlineQueryResultVoiceImpl.serializer().serialize(encoder, obj)
}
}
override fun deserialize(input: Decoder): InlineQueryResult {
override fun deserialize(decoder: Decoder): InlineQueryResult {
throw NotImplementedException()
}
}

View File

@ -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")
}

View File

@ -1,3 +1,7 @@
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

View File

@ -1,9 +1,11 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
import kotlinx.serialization.Serializable
const val inputMediaFileAttachmentNameTemplate = "attach://%s"
@Serializable(InputMediaSerializer::class)
interface InputMedia {
val type: String
val file: InputFile

View File

@ -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")
}
}

View File

@ -25,8 +25,8 @@ object HTMLParseMode : ParseMode() {
@Serializer(ParseMode::class)
internal class ParseModeSerializerObject: KSerializer<ParseMode> {
override fun deserialize(input: Decoder): ParseMode {
val mode = input.decodeString()
override fun deserialize(decoder: Decoder): ParseMode {
val mode = decoder.decodeString()
return when (mode) {
MarkdownParseMode.parseModeName -> MarkdownParseMode
HTMLParseMode.parseModeName -> HTMLParseMode
@ -34,7 +34,7 @@ internal class ParseModeSerializerObject: KSerializer<ParseMode> {
}
}
override fun serialize(output: Encoder, obj: ParseMode) {
output.encodeString(obj.parseModeName)
override fun serialize(encoder: Encoder, obj: ParseMode) {
encoder.encodeString(obj.parseModeName)
}
}

View File

@ -22,15 +22,15 @@ fun DateTime.toTelegramDate(): TelegramDate = TelegramDate(this)
@Serializer(TelegramDate::class)
internal class TelegramDateSerializer: KSerializer<TelegramDate> {
override fun serialize(output: Encoder, obj: TelegramDate) {
output.encodeLong(
override fun serialize(encoder: Encoder, obj: TelegramDate) {
encoder.encodeLong(
TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis)
)
}
override fun deserialize(input: Decoder): TelegramDate {
override fun deserialize(decoder: Decoder): TelegramDate {
return TelegramDate(
input.decodeLong()
decoder.decodeLong()
)
}
}

View File

@ -20,12 +20,12 @@ sealed class BotAction {
@Serializer(BotAction::class)
class BotActionSerializer: KSerializer<BotAction> {
override fun serialize(output: Encoder, obj: BotAction) {
output.encodeString(obj.actionName)
override fun serialize(encoder: Encoder, obj: BotAction) {
encoder.encodeString(obj.actionName)
}
override fun deserialize(input: Decoder): BotAction {
val actionName = input.decodeString()
override fun deserialize(decoder: Decoder): BotAction {
val actionName = decoder.decodeString()
return actions.firstOrNull { it.actionName == actionName } ?: throw IllegalStateException("Unknown action type: $actionName")
}
}

View File

@ -1,11 +1,16 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.*
import kotlinx.serialization.internal.ArrayListSerializer
@Serializable
data class InlineKeyboardMarkup(
@SerialName("inline_keyboard")
@Serializable(with = KeyboardSerializer::class)
val keyboard: Matrix<InlineKeyboardButton>
) : KeyboardMarkup
object KeyboardSerializer : KSerializer<Matrix<InlineKeyboardButton>> by ArrayListSerializer(
ArrayListSerializer(ContextSerializer(InlineKeyboardButton::class))
)

View File

@ -1,3 +1,6 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons
import kotlinx.serialization.Serializable
@Serializable(KeyboardMarkupSerializer::class)
interface KeyboardMarkup

View File

@ -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)
}
}
}

View File

@ -9,12 +9,12 @@ interface Currencied {
@Serializer(Currency::class)
object CurrencySerializer : KSerializer<Currency> {
override fun serialize(output: Encoder, obj: Currency) {
output.encodeString(obj.currencyCode)
override fun serialize(encoder: Encoder, obj: Currency) {
encoder.encodeString(obj.currencyCode)
}
override fun deserialize(input: Decoder): Currency {
return input.decodeString().let {
override fun deserialize(decoder: Decoder): Currency {
return decoder.decodeString().let {
Currency.getInstance(it)
}
}

View File

@ -4,13 +4,15 @@ import kotlinx.serialization.*
import kotlinx.serialization.json.*
@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.toJson(serializer: KSerializer<T>): JsonObject = JsonTreeMapper().writeTree(
this,
serializer
inline fun <reified T: Any> T.toJson(serializer: KSerializer<T>): JsonObject = Json.nonstrict.toJson(
serializer,
this
).jsonObject
fun JsonArray.withoutNulls(): JsonArray {

View File

@ -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()
}