From 3b2ccbf33b0b366b324a9374824a2ea8559df740 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 23 Nov 2023 12:00:32 +0600 Subject: [PATCH 01/10] start 9.4.0 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67ee7bb52e..0943568b50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 9.4.0 + ## 9.3.0 This release become possible thanks to [Anton Lakotka](https://youtrack.jetbrains.com/users/anton.lakotka) diff --git a/gradle.properties b/gradle.properties index 7342f4c756..1a95c1f955 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,4 +6,4 @@ kotlin.incremental=true kotlin.incremental.js=true library_group=dev.inmo -library_version=9.3.0 +library_version=9.4.0 From e34bc7453eaa3df0b278f439c762e0f95c419d19 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 23 Nov 2023 12:06:46 +0600 Subject: [PATCH 02/10] update dependencies --- CHANGELOG.md | 5 +++++ build.gradle | 1 + gradle/libs.versions.toml | 9 ++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0943568b50..eb1c5cd50c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 9.4.0 +* `Version`: + * `Serialization`: `1.6.0` -> `1.6.1` + * `Ktor`: `2.3.5` -> `2.3.6` + * `MicroUtils`: `0.20.12` -> `0.20.14` + ## 9.3.0 This release become possible thanks to [Anton Lakotka](https://youtrack.jetbrains.com/users/anton.lakotka) diff --git a/build.gradle b/build.gradle index 4e92c5d877..20b3a8a096 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ buildscript { plugins { alias(libs.plugins.kotlin.dokka) + alias(libs.plugins.versions) } // temporal crutch until legacy tests will be stabled or legacy target will be removed diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ced16411b3..e84ecb753a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,19 +1,21 @@ [versions] kotlin = "1.9.20" -kotlin-serialization = "1.6.0" +kotlin-serialization = "1.6.1" kotlin-coroutines = "1.7.3" javax-activation = "1.1.1" korlibs = "4.0.10" uuid = "0.8.1" -ktor = "2.3.5" +ktor = "2.3.6" ksp = "1.9.20-1.0.14" kotlin-poet = "1.14.2" -microutils = "0.20.12" +microutils = "0.20.14" + +versions = "0.50.0" github-release-plugin = "2.4.1" dokka = "1.9.10" @@ -71,3 +73,4 @@ kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } kotlin-dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" } +versions = { id = "com.github.ben-manes.versions", version.ref = "versions" } From 816cf00dac7db2777beac075bb4970ac652d6b89 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 23 Nov 2023 12:47:58 +0600 Subject: [PATCH 03/10] basically add logging --- gradle/libs.versions.toml | 3 +++ tgbotapi.core/build.gradle | 2 ++ .../tgbotapi/bot/ktor/KtorRequestsExecutor.kt | 9 ++++++-- .../bot/ktor/KtorRequestsExecutorFactories.kt | 7 +++++- .../ktor/base/AbstractRequestCallFactory.kt | 14 +++++++++++- .../ktor/base/DefaultKtorRequestsExecutor.kt | 22 ++++++++++++++++--- .../DownloadFileChannelRequestCallFactory.kt | 2 ++ .../base/DownloadFileRequestCallFactory.kt | 2 ++ .../ktor/base/MultipartRequestCallFactory.kt | 4 +++- .../MultipleClientKtorRequestsExecutor.kt | 5 +++++ .../bot/ktor/base/SimpleRequestCallFactory.kt | 4 +++- .../dev/inmo/tgbotapi/utils/DefaultKSLog.kt | 8 +++++++ 12 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e84ecb753a..686ac43d4a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,6 +14,7 @@ ksp = "1.9.20-1.0.14" kotlin-poet = "1.14.2" microutils = "0.20.14" +kslog = "1.3.0" versions = "0.50.0" @@ -54,6 +55,8 @@ microutils-languageCodes = { module = "dev.inmo:micro_utils.language_codes", ver microutils-ktor-common = { module = "dev.inmo:micro_utils.ktor.common", version.ref = "microutils" } microutils-fsm-common = { module = "dev.inmo:micro_utils.fsm.common", version.ref = "microutils" } +kslog = { module = "dev.inmo:kslog", version.ref = "kslog" } + # ksp dependencies kotlin-poet = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlin-poet" } diff --git a/tgbotapi.core/build.gradle b/tgbotapi.core/build.gradle index f5269f1187..f0e60eaf0e 100644 --- a/tgbotapi.core/build.gradle +++ b/tgbotapi.core/build.gradle @@ -31,6 +31,8 @@ kotlin { api libs.microutils.languageCodes api libs.ktor.client.core + + api libs.kslog } } commonTest { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt index 833c420840..83ca46fbfa 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt @@ -1,8 +1,10 @@ package dev.inmo.tgbotapi.bot.ktor +import dev.inmo.kslog.common.KSLog import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.settings.limiters.ExceptionsOnlyLimiter import dev.inmo.tgbotapi.bot.settings.limiters.RequestLimiter +import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.nonstrictJsonFormat import io.ktor.client.* @@ -22,6 +24,7 @@ expect class KtorRequestsExecutor internal constructor( requestsLimiter: RequestLimiter, jsonFormatter: Json, pipelineStepsHolder: KtorPipelineStepsHolder, + logger: KSLog, diff: Unit // just a diff property to know where constructor and where calling function with defaults ) : BaseRequestsExecutor @@ -32,7 +35,8 @@ fun KtorRequestsExecutor( excludeDefaultFactories: Boolean = false, requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter, jsonFormatter: Json = nonstrictJsonFormat, - pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder + pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder, + logger: KSLog = DefaultKTgBotAPIKSLog, ) = KtorRequestsExecutor( telegramAPIUrlsKeeper = telegramAPIUrlsKeeper, client = client, @@ -41,5 +45,6 @@ fun KtorRequestsExecutor( requestsLimiter = requestsLimiter, jsonFormatter = jsonFormatter, pipelineStepsHolder = pipelineStepsHolder, - diff = kotlin.Unit + logger = logger, + diff = kotlin.Unit, ) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt index 695ecd989d..a25b7b1572 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.bot.ktor +import dev.inmo.kslog.common.KSLog import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.ktor.base.* @@ -25,6 +26,8 @@ class KtorRequestsExecutorBuilder( var excludeDefaultFactories: Boolean = false var requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter var jsonFormatter: Json = nonstrictJsonFormat + var logger: KSLog = DefaultKTgBotAPIKSLog + var pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder fun build() = KtorRequestsExecutor( telegramAPIUrlsKeeper, @@ -32,7 +35,9 @@ class KtorRequestsExecutorBuilder( callsFactories, excludeDefaultFactories, requestsLimiter, - jsonFormatter + jsonFormatter, + pipelineStepsHolder, + logger ) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory.kt index 7d1d957aaf..f950f30e5b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory.kt @@ -1,23 +1,30 @@ package dev.inmo.tgbotapi.bot.ktor.base +import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.v +import dev.inmo.kslog.common.w import dev.inmo.micro_utils.coroutines.runCatchingSafely import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory import dev.inmo.tgbotapi.bot.exceptions.newRequestException import dev.inmo.tgbotapi.requests.GetUpdatesRequest import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.types.Response +import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import io.ktor.client.HttpClient import io.ktor.client.plugins.timeout import io.ktor.client.request.* import io.ktor.client.statement.bodyAsText import io.ktor.http.ContentType +import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json import kotlin.collections.set var defaultUpdateTimeoutForZeroDelay = 1000L -abstract class AbstractRequestCallFactory : KtorCallFactory { +abstract class AbstractRequestCallFactory( + protected open val logger: KSLog = DefaultKTgBotAPIKSLog +) : KtorCallFactory { private val methodsCache: MutableMap = mutableMapOf() override suspend fun makeCall( client: HttpClient, @@ -26,6 +33,7 @@ abstract class AbstractRequestCallFactory : KtorCallFactory { jsonFormatter: Json ): T? { val preparedBody = prepareCallBody(client, urlsKeeper, request) ?: return null + logger.v { "Prepared body for $request: $preparedBody" } client.post { url( @@ -54,7 +62,9 @@ abstract class AbstractRequestCallFactory : KtorCallFactory { setBody(preparedBody) }.let { response -> val content = response.bodyAsText() + logger.v { "Raw answer for $request: $content" } val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) + logger.v { "Answer as json for $request: $responseObject" } return runCatchingSafely { (responseObject.result?.let { @@ -66,6 +76,8 @@ abstract class AbstractRequestCallFactory : KtorCallFactory { "Can't get result object from $content" ) }) + }.onFailure { + logger.w { "Got exception answer for $request: $it" } }.getOrThrow() } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt index 49ca017764..50fa0d3bd3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt @@ -1,5 +1,9 @@ package dev.inmo.tgbotapi.bot.ktor.base +import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.e +import dev.inmo.kslog.common.i +import dev.inmo.kslog.common.v import dev.inmo.micro_utils.coroutines.runCatchingSafely import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.exceptions.BotException @@ -28,12 +32,15 @@ class DefaultKtorRequestsExecutor internal constructor( private val requestsLimiter: RequestLimiter, private val jsonFormatter: Json, private val pipelineStepsHolder: KtorPipelineStepsHolder, + private val logger: KSLog, diff: Unit ) : BaseRequestsExecutor(telegramAPIUrlsKeeper) { private val callsFactories: List = callsFactories.run { if (!excludeDefaultFactories) { + logger.v { "Installing default factories" } this + createTelegramBotDefaultKtorCallRequestsFactories() } else { + logger.v { "Default factories will not be installed" } this } } @@ -46,19 +53,23 @@ class DefaultKtorRequestsExecutor internal constructor( override suspend fun execute(request: Request): T { return runCatchingSafely { + logger.v { "Start request $request" } pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories) requestsLimiter.limit(request) { var result: T? = null lateinit var factoryHandledRequest: KtorCallFactory for (potentialFactory in callsFactories) { pipelineStepsHolder.onBeforeCallFactoryMakeCall(request, potentialFactory) - result = potentialFactory.makeCall( + logger.v { "Trying factory $potentialFactory for $request" } + val resultFromFactory = potentialFactory.makeCall( client, telegramAPIUrlsKeeper, request, jsonFormatter ) - result = pipelineStepsHolder.onAfterCallFactoryMakeCall(result, request, potentialFactory) + logger.v { "Result of factory $potentialFactory handling $request: $resultFromFactory" } + result = pipelineStepsHolder.onAfterCallFactoryMakeCall(resultFromFactory, request, potentialFactory) + logger.v { "Result of pipeline $pipelineStepsHolder handling $resultFromFactory: $result" } if (result != null) { factoryHandledRequest = potentialFactory break @@ -71,6 +82,7 @@ class DefaultKtorRequestsExecutor internal constructor( } }.let { val result = it.exceptionOrNull() ?.let { e -> + logger.v(e) { "Got exception on handling of $request" } pipelineStepsHolder.onRequestException(request, e) ?.let { return@let it } when (e) { @@ -90,9 +102,13 @@ class DefaultKtorRequestsExecutor internal constructor( } is BotException -> e else -> CommonBotException(cause = e) + }.also { + logger.v(e) { "Result exception on handling of $request: $it" } } } ?.let { Result.failure(it) } ?: it - pipelineStepsHolder.onRequestReturnResult(result, request, callsFactories) + pipelineStepsHolder.onRequestReturnResult(result, request, callsFactories).also { + logger.v { "Result of handling $request: $it" } + } } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt index d1b9458173..d3f1f94490 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt @@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory import dev.inmo.tgbotapi.requests.DownloadFileStream import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.utils.ByteReadChannelAllocator +import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import io.ktor.client.HttpClient import io.ktor.client.call.receive @@ -15,6 +16,7 @@ import io.ktor.utils.io.* import kotlinx.coroutines.* import kotlinx.serialization.json.Json +@RiskFeature object DownloadFileChannelRequestCallFactory : KtorCallFactory { override suspend fun makeCall( client: HttpClient, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt index 323b7b12fe..2d3a661e7a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt @@ -4,12 +4,14 @@ import dev.inmo.micro_utils.coroutines.safely import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory import dev.inmo.tgbotapi.requests.DownloadFile import dev.inmo.tgbotapi.requests.abstracts.Request +import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import io.ktor.client.HttpClient import io.ktor.client.request.get import io.ktor.client.statement.readBytes import kotlinx.serialization.json.Json +@RiskFeature object DownloadFileRequestCallFactory : KtorCallFactory { override suspend fun makeCall( client: HttpClient, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/MultipartRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/MultipartRequestCallFactory.kt index b01f66e2dd..0f82a3e871 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/MultipartRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/MultipartRequestCallFactory.kt @@ -1,6 +1,8 @@ package dev.inmo.tgbotapi.bot.ktor.base +import dev.inmo.kslog.common.KSLog import dev.inmo.tgbotapi.requests.abstracts.* +import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.mapWithCommonValues import io.ktor.client.HttpClient @@ -8,7 +10,7 @@ import io.ktor.client.request.forms.* import io.ktor.http.Headers import io.ktor.http.HttpHeaders -class MultipartRequestCallFactory : AbstractRequestCallFactory() { +class MultipartRequestCallFactory(logger: KSLog? = null) : AbstractRequestCallFactory(logger ?: DefaultKTgBotAPIKSLog) { override fun prepareCallBody( client: HttpClient, urlsKeeper: TelegramAPIUrlsKeeper, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/MultipleClientKtorRequestsExecutor.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/MultipleClientKtorRequestsExecutor.kt index adb30b2228..65ef477789 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/MultipleClientKtorRequestsExecutor.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/MultipleClientKtorRequestsExecutor.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.bot.ktor.base +import dev.inmo.kslog.common.KSLog import dev.inmo.micro_utils.coroutines.runCatchingSafely import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory @@ -48,6 +49,7 @@ class MultipleClientKtorRequestsExecutor ( jsonFormatter: Json, pipelineStepsHolder: KtorPipelineStepsHolder, requestExecutorsCount: Int, + logger: KSLog, clientFactory: () -> HttpClient ) : BaseRequestsExecutor(telegramAPIUrlsKeeper) { private val requestExecutors = (0 until requestExecutorsCount).map { @@ -59,6 +61,7 @@ class MultipleClientKtorRequestsExecutor ( requestsLimiter, jsonFormatter, pipelineStepsHolder, + logger, Unit ) }.toSet() @@ -80,6 +83,7 @@ class MultipleClientKtorRequestsExecutor ( requestsLimiter: RequestLimiter, jsonFormatter: Json, pipelineStepsHolder: KtorPipelineStepsHolder, + logger: KSLog, diff: Unit ) : this( telegramAPIUrlsKeeper, @@ -89,6 +93,7 @@ class MultipleClientKtorRequestsExecutor ( jsonFormatter, pipelineStepsHolder, client.engineConfig.threadsCount, + logger, { platformClientCopy(client) } ) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/SimpleRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/SimpleRequestCallFactory.kt index 686d1df365..c87caf79e9 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/SimpleRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/SimpleRequestCallFactory.kt @@ -1,12 +1,14 @@ package dev.inmo.tgbotapi.bot.ktor.base +import dev.inmo.kslog.common.KSLog import dev.inmo.tgbotapi.requests.abstracts.* +import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import io.ktor.client.HttpClient import io.ktor.http.ContentType import io.ktor.http.content.TextContent -class SimpleRequestCallFactory : AbstractRequestCallFactory() { +class SimpleRequestCallFactory(logger: KSLog? = null) : AbstractRequestCallFactory(logger ?: DefaultKTgBotAPIKSLog) { override fun prepareCallBody( client: HttpClient, urlsKeeper: TelegramAPIUrlsKeeper, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt new file mode 100644 index 0000000000..650639ec81 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt @@ -0,0 +1,8 @@ +package dev.inmo.tgbotapi.utils + +import dev.inmo.kslog.common.KSLog + +/** + * Default realization of [KSLog] which will be used everywhere where there is no some custom variant of [KSLog] + */ +var DefaultKTgBotAPIKSLog = KSLog("KTgBot") From ffc0f5abb7aeed5c66b363ab6801c0f6782941d5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 23 Nov 2023 20:00:52 +0600 Subject: [PATCH 04/10] add improvements in logging functionality --- .../inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt | 6 +++--- .../tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt index a25b7b1572..2ab970a366 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutorFactories.kt @@ -11,9 +11,9 @@ import io.ktor.client.HttpClient import kotlinx.serialization.json.Json @RiskFeature -fun createTelegramBotDefaultKtorCallRequestsFactories() = listOf( - SimpleRequestCallFactory(), - MultipartRequestCallFactory(), +fun createTelegramBotDefaultKtorCallRequestsFactories(logger: KSLog? = null) = listOf( + SimpleRequestCallFactory(logger), + MultipartRequestCallFactory(logger), DownloadFileRequestCallFactory, DownloadFileChannelRequestCallFactory ) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt index 50fa0d3bd3..fd7a0bf237 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor.kt @@ -38,7 +38,7 @@ class DefaultKtorRequestsExecutor internal constructor( private val callsFactories: List = callsFactories.run { if (!excludeDefaultFactories) { logger.v { "Installing default factories" } - this + createTelegramBotDefaultKtorCallRequestsFactories() + this + createTelegramBotDefaultKtorCallRequestsFactories(logger) } else { logger.v { "Default factories will not be installed" } this From 98e5d182bb177ad01fe1232c68c5161dfd849836 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 25 Nov 2023 00:28:51 +0600 Subject: [PATCH 05/10] update dependencies --- CHANGELOG.md | 3 ++- gradle/libs.versions.toml | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb1c5cd50c..2b841162b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,10 @@ ## 9.4.0 * `Version`: + * `Kotlin`: `1.9.20` -> `1.9.21` * `Serialization`: `1.6.0` -> `1.6.1` * `Ktor`: `2.3.5` -> `2.3.6` - * `MicroUtils`: `0.20.12` -> `0.20.14` + * `MicroUtils`: `0.20.12` -> `0.20.15` ## 9.3.0 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 686ac43d4a..4a0944291d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,6 @@ [versions] -kotlin = "1.9.20" +kotlin = "1.9.21" kotlin-serialization = "1.6.1" kotlin-coroutines = "1.7.3" @@ -11,10 +11,10 @@ uuid = "0.8.1" ktor = "2.3.6" ksp = "1.9.20-1.0.14" -kotlin-poet = "1.14.2" +kotlin-poet = "1.15.1" -microutils = "0.20.14" -kslog = "1.3.0" +microutils = "0.20.15" +kslog = "1.3.1" versions = "0.50.0" From b412e7b3b79f9f3ae7c0497fd94b1bb9156523a8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 25 Nov 2023 12:56:00 +0600 Subject: [PATCH 06/10] improvement of DefaultKTgBotAPIKSLog --- .../kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt index 650639ec81..a7736691f3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt @@ -1,8 +1,11 @@ package dev.inmo.tgbotapi.utils import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.TagLogger /** * Default realization of [KSLog] which will be used everywhere where there is no some custom variant of [KSLog] + * + * By default, uses [TagLogger] with tag `KTgBot` (which in fact falling back to [KSLog.default] with `KTgBot` default tag) */ -var DefaultKTgBotAPIKSLog = KSLog("KTgBot") +var DefaultKTgBotAPIKSLog: KSLog = TagLogger("KTgBot") From 8763ea23fa6125047664252f89dd040f2d803ffd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 25 Nov 2023 17:52:42 +0600 Subject: [PATCH 07/10] cratch fix of build fails --- tgbotapi.behaviour_builder.fsm/build.gradle | 2 ++ tgbotapi.behaviour_builder/build.gradle | 3 ++- tgbotapi.utils/build.gradle | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tgbotapi.behaviour_builder.fsm/build.gradle b/tgbotapi.behaviour_builder.fsm/build.gradle index ab3b919b5e..fb7c09bced 100644 --- a/tgbotapi.behaviour_builder.fsm/build.gradle +++ b/tgbotapi.behaviour_builder.fsm/build.gradle @@ -13,6 +13,8 @@ kotlin { sourceSets { commonMain { dependencies { + api project(":tgbotapi.core") + api project(":tgbotapi.utils") api project(":tgbotapi.behaviour_builder") api libs.microutils.fsm.common } diff --git a/tgbotapi.behaviour_builder/build.gradle b/tgbotapi.behaviour_builder/build.gradle index 5b14d5a10f..cb0e6bcca1 100644 --- a/tgbotapi.behaviour_builder/build.gradle +++ b/tgbotapi.behaviour_builder/build.gradle @@ -13,7 +13,8 @@ kotlin { sourceSets { commonMain { dependencies { - api project(":tgbotapi.utils") + api project(path: ':tgbotapi.core') + api project(path: ':tgbotapi.utils') } } } diff --git a/tgbotapi.utils/build.gradle b/tgbotapi.utils/build.gradle index 086728d73c..69aeb7a1fe 100644 --- a/tgbotapi.utils/build.gradle +++ b/tgbotapi.utils/build.gradle @@ -13,7 +13,7 @@ kotlin { sourceSets { commonMain { dependencies { - api project(":tgbotapi.core") + api project(path: ":tgbotapi.core") } } } From b7c3f9f60722f86430ed45352973d143ec4566de Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 26 Nov 2023 02:06:30 +0600 Subject: [PATCH 08/10] pinned message in ExtendedOtherPartiesChat --- CHANGELOG.md | 2 ++ .../kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt | 5 ++++- .../inmo/tgbotapi/types/chat/ExtendedAbstracts.kt | 13 +++++++++---- .../inmo/tgbotapi/extensions/utils/ClassCastsNew.kt | 10 ++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b841162b0..83399a485f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ * `Serialization`: `1.6.0` -> `1.6.1` * `Ktor`: `2.3.5` -> `2.3.6` * `MicroUtils`: `0.20.12` -> `0.20.15` +* `Core`: + * New abstraction `ExtendedOtherPartiesChat` with `pinnedMessage` ## 9.3.0 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt index 91d502f931..65256cb987 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt @@ -81,7 +81,10 @@ data class ExtendedPrivateChatImpl( @SerialName(emojiStatusCustomEmojiIdField) override val statusEmojiId: CustomEmojiId? = null, @SerialName(emojiStatusExpirationDateField) - override val statusEmojiExpiration: TelegramDate? = null + override val statusEmojiExpiration: TelegramDate? = null, + @SerialName(pinnedMessageField) + @Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class) + override val pinnedMessage: Message? = null, ) : ExtendedPrivateChat typealias ExtendedUser = ExtendedPrivateChatImpl diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt index 09ac9d6f4f..bf9e7cc122 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt @@ -17,7 +17,7 @@ sealed interface ExtendedGroupChat : GroupChat, ExtendedPublicChat { } @Serializable(ExtendedChatSerializer.Companion::class) -sealed interface ExtendedPrivateChat : PrivateChat, ExtendedChatWithUsername { +sealed interface ExtendedPrivateChat : PrivateChat, ExtendedChatWithUsername, ExtendedOtherPartiesChat { val bio: String val hasPrivateForwards: Boolean val hasRestrictedVoiceAndVideoMessages: Boolean @@ -28,11 +28,9 @@ sealed interface ExtendedPrivateChat : PrivateChat, ExtendedChatWithUsername { get() = hasPrivateForwards } -sealed interface ExtendedPublicChat : ExtendedChat, PublicChat { +sealed interface ExtendedPublicChat : ExtendedOtherPartiesChat, PublicChat { val description: String val inviteLink: String? - @Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class) - val pinnedMessage: Message? val membersHidden: Boolean } @@ -68,6 +66,13 @@ sealed interface ExtendedChat : Chat { val chatPhoto: ChatPhoto? } + +@Serializable(ExtendedChatSerializer.Companion::class) +sealed interface ExtendedOtherPartiesChat : ExtendedChat { + @Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class) + val pinnedMessage: Message? +} + @Serializable(ExtendedChatSerializer.Companion::class) sealed interface ExtendedChatWithUsername : UsernameChat, ExtendedChat { val activeUsernames: List diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt index 5dadba2ded..234934b60c 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt @@ -120,6 +120,7 @@ import dev.inmo.tgbotapi.types.chat.ExtendedChat import dev.inmo.tgbotapi.types.chat.ExtendedChatWithUsername import dev.inmo.tgbotapi.types.chat.ExtendedForumChat import dev.inmo.tgbotapi.types.chat.ExtendedGroupChat +import dev.inmo.tgbotapi.types.chat.ExtendedOtherPartiesChat import dev.inmo.tgbotapi.types.chat.ExtendedPrivateChat import dev.inmo.tgbotapi.types.chat.ExtendedPublicChat import dev.inmo.tgbotapi.types.chat.ExtendedSupergroupChat @@ -1965,6 +1966,15 @@ public inline fun Chat.extendedChatOrThrow(): ExtendedChat = this as public inline fun Chat.ifExtendedChat(block: (ExtendedChat) -> T): T? = extendedChatOrNull() ?.let(block) +public inline fun Chat.extendedOtherPartiesChatOrNull(): ExtendedOtherPartiesChat? = this as? + dev.inmo.tgbotapi.types.chat.ExtendedOtherPartiesChat + +public inline fun Chat.extendedOtherPartiesChatOrThrow(): ExtendedOtherPartiesChat = this as + dev.inmo.tgbotapi.types.chat.ExtendedOtherPartiesChat + +public inline fun Chat.ifExtendedOtherPartiesChat(block: (ExtendedOtherPartiesChat) -> T): T? = + extendedOtherPartiesChatOrNull() ?.let(block) + public inline fun Chat.extendedChatWithUsernameOrNull(): ExtendedChatWithUsername? = this as? dev.inmo.tgbotapi.types.chat.ExtendedChatWithUsername From f8182ddb8584fb27242e334505d347bc5ab9721e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 26 Nov 2023 03:20:05 +0600 Subject: [PATCH 09/10] Revert "pinned message in ExtendedOtherPartiesChat" This reverts commit b7c3f9f60722f86430ed45352973d143ec4566de. --- CHANGELOG.md | 2 -- .../kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt | 5 +---- .../inmo/tgbotapi/types/chat/ExtendedAbstracts.kt | 13 ++++--------- .../inmo/tgbotapi/extensions/utils/ClassCastsNew.kt | 10 ---------- 4 files changed, 5 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83399a485f..2b841162b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,6 @@ * `Serialization`: `1.6.0` -> `1.6.1` * `Ktor`: `2.3.5` -> `2.3.6` * `MicroUtils`: `0.20.12` -> `0.20.15` -* `Core`: - * New abstraction `ExtendedOtherPartiesChat` with `pinnedMessage` ## 9.3.0 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt index 65256cb987..91d502f931 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt @@ -81,10 +81,7 @@ data class ExtendedPrivateChatImpl( @SerialName(emojiStatusCustomEmojiIdField) override val statusEmojiId: CustomEmojiId? = null, @SerialName(emojiStatusExpirationDateField) - override val statusEmojiExpiration: TelegramDate? = null, - @SerialName(pinnedMessageField) - @Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class) - override val pinnedMessage: Message? = null, + override val statusEmojiExpiration: TelegramDate? = null ) : ExtendedPrivateChat typealias ExtendedUser = ExtendedPrivateChatImpl diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt index bf9e7cc122..09ac9d6f4f 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt @@ -17,7 +17,7 @@ sealed interface ExtendedGroupChat : GroupChat, ExtendedPublicChat { } @Serializable(ExtendedChatSerializer.Companion::class) -sealed interface ExtendedPrivateChat : PrivateChat, ExtendedChatWithUsername, ExtendedOtherPartiesChat { +sealed interface ExtendedPrivateChat : PrivateChat, ExtendedChatWithUsername { val bio: String val hasPrivateForwards: Boolean val hasRestrictedVoiceAndVideoMessages: Boolean @@ -28,9 +28,11 @@ sealed interface ExtendedPrivateChat : PrivateChat, ExtendedChatWithUsername, Ex get() = hasPrivateForwards } -sealed interface ExtendedPublicChat : ExtendedOtherPartiesChat, PublicChat { +sealed interface ExtendedPublicChat : ExtendedChat, PublicChat { val description: String val inviteLink: String? + @Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class) + val pinnedMessage: Message? val membersHidden: Boolean } @@ -66,13 +68,6 @@ sealed interface ExtendedChat : Chat { val chatPhoto: ChatPhoto? } - -@Serializable(ExtendedChatSerializer.Companion::class) -sealed interface ExtendedOtherPartiesChat : ExtendedChat { - @Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class) - val pinnedMessage: Message? -} - @Serializable(ExtendedChatSerializer.Companion::class) sealed interface ExtendedChatWithUsername : UsernameChat, ExtendedChat { val activeUsernames: List diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt index 234934b60c..5dadba2ded 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt @@ -120,7 +120,6 @@ import dev.inmo.tgbotapi.types.chat.ExtendedChat import dev.inmo.tgbotapi.types.chat.ExtendedChatWithUsername import dev.inmo.tgbotapi.types.chat.ExtendedForumChat import dev.inmo.tgbotapi.types.chat.ExtendedGroupChat -import dev.inmo.tgbotapi.types.chat.ExtendedOtherPartiesChat import dev.inmo.tgbotapi.types.chat.ExtendedPrivateChat import dev.inmo.tgbotapi.types.chat.ExtendedPublicChat import dev.inmo.tgbotapi.types.chat.ExtendedSupergroupChat @@ -1966,15 +1965,6 @@ public inline fun Chat.extendedChatOrThrow(): ExtendedChat = this as public inline fun Chat.ifExtendedChat(block: (ExtendedChat) -> T): T? = extendedChatOrNull() ?.let(block) -public inline fun Chat.extendedOtherPartiesChatOrNull(): ExtendedOtherPartiesChat? = this as? - dev.inmo.tgbotapi.types.chat.ExtendedOtherPartiesChat - -public inline fun Chat.extendedOtherPartiesChatOrThrow(): ExtendedOtherPartiesChat = this as - dev.inmo.tgbotapi.types.chat.ExtendedOtherPartiesChat - -public inline fun Chat.ifExtendedOtherPartiesChat(block: (ExtendedOtherPartiesChat) -> T): T? = - extendedOtherPartiesChatOrNull() ?.let(block) - public inline fun Chat.extendedChatWithUsernameOrNull(): ExtendedChatWithUsername? = this as? dev.inmo.tgbotapi.types.chat.ExtendedChatWithUsername From 615814322086738010fc18fc1ef68988870205bc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 26 Nov 2023 04:30:18 +0600 Subject: [PATCH 10/10] Update greetings.yml --- .github/workflows/greetings.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/greetings.yml b/.github/workflows/greetings.yml index bdde1cca7b..9a3b585e28 100644 --- a/.github/workflows/greetings.yml +++ b/.github/workflows/greetings.yml @@ -5,6 +5,9 @@ on: [pull_request, issues] jobs: greeting: runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write steps: - uses: actions/first-interaction@v1 with: