1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-11-30 19:35:43 +00:00

Compare commits

..

41 Commits

Author SHA1 Message Date
6158143220 Update greetings.yml 2023-11-26 04:30:18 +06:00
f8182ddb85 Revert "pinned message in ExtendedOtherPartiesChat"
This reverts commit b7c3f9f607.
2023-11-26 03:20:05 +06:00
b7c3f9f607 pinned message in ExtendedOtherPartiesChat 2023-11-26 02:06:30 +06:00
8763ea23fa cratch fix of build fails 2023-11-25 17:54:40 +06:00
b412e7b3b7 improvement of DefaultKTgBotAPIKSLog 2023-11-25 12:56:00 +06:00
98e5d182bb update dependencies 2023-11-25 00:28:51 +06:00
ffc0f5abb7 add improvements in logging functionality 2023-11-23 20:00:52 +06:00
816cf00dac basically add logging 2023-11-23 12:47:58 +06:00
e34bc7453e update dependencies 2023-11-23 12:06:46 +06:00
3b2ccbf33b start 9.4.0 2023-11-23 12:00:32 +06:00
6ecfbdf56d improvements in publish.gradle 2023-11-05 13:22:32 +06:00
b49e1c50f5 remove temporal publish.gradle file 2023-11-05 13:09:54 +06:00
d7389dfcfe potential fix of publish.gradle 2023-11-05 13:09:29 +06:00
cf5cee3e53 Merge pull request #798 from InsanusMokrassar/9.3.0
9.3.0
2023-11-05 12:46:40 +06:00
8593263506 fill changelog 2023-11-05 12:36:36 +06:00
4422a4d09b update workflows jdk 2023-11-05 00:48:09 +06:00
9ecb50e377 update dependencies and gradle wrapper 2023-11-04 21:13:14 +06:00
8a4b40c6ec Update libs.versions.toml 2023-11-01 12:45:24 +06:00
bba667db30 Merge branch 'master' into 9.3.0 2023-10-31 18:45:52 +06:00
ca0d256bbb update dependencies 2023-10-31 18:43:51 +06:00
dc2fd07632 Merge pull request #797 from InsanusMokrassar/9.2.4
9.2.4
2023-10-25 19:59:05 +06:00
c4214798e3 Merge branch 'master' into 9.3.0-RC 2023-10-24 16:15:49 +06:00
a0b14233e0 update microutils 2023-10-20 22:35:03 +06:00
1a479706e2 update version of microutils and add arm platform target 2023-10-20 22:07:14 +06:00
2719e166a8 update workgflows 2023-10-17 23:46:28 +06:00
051684db23 update changelog 2023-10-17 23:43:13 +06:00
805cec76ce update kdokka 2023-10-17 23:39:30 +06:00
899c195fd5 update gradle wrapper 2023-10-17 23:38:31 +06:00
f5937fc4d6 upgrade up to 9.3.0-RC 2023-10-17 23:37:51 +06:00
8cf7b349df Merge branch 'master' into 9.3.0 2023-10-15 18:04:44 +06:00
bf8f8b9e6f update microutils 2023-10-15 17:49:13 +06:00
edc0b1c492 update ksp and ktor 2023-10-12 13:27:54 +06:00
a85d58aac1 update up to 1.9.20-RC🎉 2023-10-12 13:21:06 +06:00
826c27874d update microutils 2023-10-09 20:52:33 +06:00
4e917e8cf8 update libs and changelog with thanks to Anton Lakotka 2023-10-09 13:07:56 +06:00
96ffae2062 Update libs.versions.toml 2023-09-27 23:26:57 +06:00
4180721aed update dependencies 2023-09-27 18:18:30 +06:00
4ab0845333 migrate onto 9.3.0 2023-09-27 18:02:14 +06:00
3f9a4e95a3 update up to kotlin 1.9.20-Beta2 2023-09-27 18:02:14 +06:00
834d60ff16 update dependencies 2023-09-27 18:02:14 +06:00
650d96974f update dependencies 2023-09-27 18:02:14 +06:00
36 changed files with 296 additions and 61 deletions

View File

@@ -5,6 +5,9 @@ on: [pull_request, issues]
jobs: jobs:
greeting: greeting:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps: steps:
- uses: actions/first-interaction@v1 - uses: actions/first-interaction@v1
with: with:

View File

@@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions/setup-java@v1 - uses: actions/setup-java@v1
with: with:
java-version: 11 java-version: 17
- name: Build - name: Build
run: ./gradlew dokkaHtmlMultiModule run: ./gradlew dokkaHtmlMultiModule
- name: Publish KDocs - name: Publish KDocs

View File

@@ -7,9 +7,9 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions/setup-java@v1 - uses: actions/setup-java@v1
with: with:
java-version: 11 java-version: 17
- name: Setup LibCurl - name: Setup LibCurl
run: sudo apt install -y libcurl4-openssl-dev run: sudo apt update && sudo apt install -y libcurl4-openssl-dev
- name: Rewrite version - name: Rewrite version
run: | run: |
branch="`echo "${{ github.ref }}" | grep -o "[^/]*$"`" branch="`echo "${{ github.ref }}" | grep -o "[^/]*$"`"

View File

@@ -1,5 +1,31 @@
# TelegramBotAPI changelog # TelegramBotAPI changelog
## 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.15`
## 9.3.0
This release become possible thanks to [Anton Lakotka](https://youtrack.jetbrains.com/users/anton.lakotka)
**THIS RELEASE CONTAINS UPDATES UP TO RELEASE CANDIDATES VERSIONS**
**UPDATE MAY HAVE BREAKING CHANGES**
**SINCE THIS UPDATE IT WILL BE REQUIRED TO USE JDK 17+ FOR DEVELOPMENT**
* `Version`:
* `Kotlin`: `1.8.22` -> `1.9.20`
* `Serialization`: `1.5.1` -> `1.6.0`
* `KorLibs`: `4.0.3` -> `4.0.10`
* `UUID`: `0.7.1` -> `0.8.1`
* `Ktor`: `2.3.4` -> `2.3.5`
* `MicroUtils`: `0.19.9` -> `0.20.12`
## 9.2.4 ## 9.2.4
* `Utils`: * `Utils`:

View File

@@ -16,6 +16,7 @@ buildscript {
plugins { plugins {
alias(libs.plugins.kotlin.dokka) alias(libs.plugins.kotlin.dokka)
alias(libs.plugins.versions)
} }
// temporal crutch until legacy tests will be stabled or legacy target will be removed // temporal crutch until legacy tests will be stabled or legacy target will be removed

View File

@@ -6,4 +6,4 @@ kotlin.incremental=true
kotlin.incremental.js=true kotlin.incremental.js=true
library_group=dev.inmo library_group=dev.inmo
library_version=9.2.4 library_version=9.4.0

View File

@@ -1,22 +1,25 @@
[versions] [versions]
kotlin = "1.8.22" kotlin = "1.9.21"
kotlin-serialization = "1.5.1" kotlin-serialization = "1.6.1"
kotlin-coroutines = "1.7.3" kotlin-coroutines = "1.7.3"
javax-activation = "1.1.1" javax-activation = "1.1.1"
korlibs = "4.0.3" korlibs = "4.0.10"
uuid = "0.7.1" uuid = "0.8.1"
ktor = "2.3.4" ktor = "2.3.6"
ksp = "1.8.22-1.0.11" ksp = "1.9.20-1.0.14"
kotlin-poet = "1.14.2" kotlin-poet = "1.15.1"
microutils = "0.19.9" microutils = "0.20.15"
kslog = "1.3.1"
versions = "0.50.0"
github-release-plugin = "2.4.1" github-release-plugin = "2.4.1"
dokka = "1.8.20" dokka = "1.9.10"
[libraries] [libraries]
@@ -52,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-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" } microutils-fsm-common = { module = "dev.inmo:micro_utils.fsm.common", version.ref = "microutils" }
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
# ksp dependencies # ksp dependencies
kotlin-poet = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlin-poet" } kotlin-poet = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlin-poet" }
@@ -71,3 +76,4 @@ kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", 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-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlin-dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" } kotlin-dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
versions = { id = "com.github.ben-manes.versions", version.ref = "versions" }

View File

@@ -2,4 +2,4 @@ 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-7.6.3-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip

View File

@@ -5,7 +5,7 @@ kotlin {
jvm { jvm {
compilations.main { compilations.main {
kotlinOptions { kotlinOptions {
jvmTarget = "1.8" jvmTarget = "17"
} }
} }
} }
@@ -15,6 +15,7 @@ kotlin {
} }
linuxX64() linuxX64()
mingwX64() mingwX64()
linuxArm64()
sourceSets { sourceSets {
commonMain { commonMain {
@@ -40,10 +41,18 @@ kotlin {
implementation libs.kotlin.test.junit implementation libs.kotlin.test.junit
} }
} }
all {
languageSettings {
optIn('dev.inmo.tgbotapi.utils.RiskFeature')
optIn('dev.inmo.tgbotapi.utils.PreviewFeature')
optIn('dev.inmo.micro_utils.common.Warning')
optIn('dev.inmo.micro_utils.common.PreviewFeature')
}
}
} }
} }
java { java {
sourceCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_17
} }

View File

@@ -1,8 +1,7 @@
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
task javadocsJar(type: Jar) { task javadocsJar(type: Jar) {
archiveClassifier.convention("javadoc") archiveClassifier = 'javadoc'
archiveClassifier.set("javadoc")
} }
publishing { publishing {
@@ -97,4 +96,27 @@ if (project.hasProperty("signing.gnupg.keyName")) {
dependsOn(it) dependsOn(it)
} }
} }
// Workaround to make android sign operations depend on signing tasks
project.getTasks().withType(AbstractPublishToMaven.class).configureEach {
def signingTasks = project.getTasks().withType(Sign.class)
mustRunAfter(signingTasks)
}
// Workaround to make test tasks use sign
project.getTasks().withType(Sign.class).configureEach { signTask ->
def withoutSign = (signTask.name.startsWith("sign") ? signTask.name.minus("sign") : signTask.name)
def pubName = withoutSign.endsWith("Publication") ? withoutSign.substring(0, withoutSign.length() - "Publication".length()) : withoutSign
// These tasks only exist for native targets, hence findByName() to avoid trying to find them for other targets
// Task ':linkDebugTest<platform>' uses this output of task ':sign<platform>Publication' without declaring an explicit or implicit dependency
def debugTestTask = tasks.findByName("linkDebugTest$pubName")
if (debugTestTask != null) {
signTask.mustRunAfter(debugTestTask)
}
// Task ':compileTestKotlin<platform>' uses this output of task ':sign<platform>Publication' without declaring an explicit or implicit dependency
def testTask = tasks.findByName("compileTestKotlin$pubName")
if (testTask != null) {
signTask.mustRunAfter(testTask)
}
}
} }

View File

@@ -13,6 +13,8 @@ kotlin {
sourceSets { sourceSets {
commonMain { commonMain {
dependencies { dependencies {
api project(":tgbotapi.core")
api project(":tgbotapi.utils")
api project(":tgbotapi.behaviour_builder") api project(":tgbotapi.behaviour_builder")
api libs.microutils.fsm.common api libs.microutils.fsm.common
} }

View File

@@ -13,7 +13,8 @@ kotlin {
sourceSets { sourceSets {
commonMain { commonMain {
dependencies { dependencies {
api project(":tgbotapi.utils") api project(path: ':tgbotapi.core')
api project(path: ':tgbotapi.utils')
} }
} }
} }

View File

@@ -0,0 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
actual var defaultCoroutineScopeProvider: () -> CoroutineScope = { CoroutineScope(Dispatchers.Default) }

View File

@@ -31,6 +31,8 @@ kotlin {
api libs.microutils.languageCodes api libs.microutils.languageCodes
api libs.ktor.client.core api libs.ktor.client.core
api libs.kslog
} }
} }
commonTest { commonTest {
@@ -55,6 +57,12 @@ kotlin {
} }
} }
linuxArm64Main {
dependencies {
api libs.ktor.client.cio
}
}
mingwX64Main { mingwX64Main {
dependencies { dependencies {
api libs.ktor.client.winhttp api libs.ktor.client.winhttp

View File

@@ -1,8 +1,10 @@
package dev.inmo.tgbotapi.bot.ktor package dev.inmo.tgbotapi.bot.ktor
import dev.inmo.kslog.common.KSLog
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
import dev.inmo.tgbotapi.bot.settings.limiters.ExceptionsOnlyLimiter import dev.inmo.tgbotapi.bot.settings.limiters.ExceptionsOnlyLimiter
import dev.inmo.tgbotapi.bot.settings.limiters.RequestLimiter 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.TelegramAPIUrlsKeeper
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import io.ktor.client.* import io.ktor.client.*
@@ -14,12 +16,35 @@ import kotlinx.serialization.json.Json
* * On JS, JVM and MingwX64 platforms it is [dev.inmo.tgbotapi.bot.ktor.base.DefaultKtorRequestsExecutor] * * On JS, JVM and MingwX64 platforms it is [dev.inmo.tgbotapi.bot.ktor.base.DefaultKtorRequestsExecutor]
* * On LinuxX64 it is [dev.inmo.tgbotapi.bot.ktor.base.MultipleClientKtorRequestsExecutor] * * On LinuxX64 it is [dev.inmo.tgbotapi.bot.ktor.base.MultipleClientKtorRequestsExecutor]
*/ */
expect class KtorRequestsExecutor ( expect class KtorRequestsExecutor internal constructor(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
client: HttpClient,
callsFactories: List<KtorCallFactory>,
excludeDefaultFactories: Boolean,
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
fun KtorRequestsExecutor(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
client: HttpClient = HttpClient(), client: HttpClient = HttpClient(),
callsFactories: List<KtorCallFactory> = emptyList(), callsFactories: List<KtorCallFactory> = emptyList(),
excludeDefaultFactories: Boolean = false, excludeDefaultFactories: Boolean = false,
requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter, requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter,
jsonFormatter: Json = nonstrictJsonFormat, jsonFormatter: Json = nonstrictJsonFormat,
pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder,
) : BaseRequestsExecutor logger: KSLog = DefaultKTgBotAPIKSLog,
) = KtorRequestsExecutor(
telegramAPIUrlsKeeper = telegramAPIUrlsKeeper,
client = client,
callsFactories = callsFactories,
excludeDefaultFactories = excludeDefaultFactories,
requestsLimiter = requestsLimiter,
jsonFormatter = jsonFormatter,
pipelineStepsHolder = pipelineStepsHolder,
logger = logger,
diff = kotlin.Unit,
)

View File

@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.bot.ktor package dev.inmo.tgbotapi.bot.ktor
import dev.inmo.kslog.common.KSLog
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.bot.ktor.base.* import dev.inmo.tgbotapi.bot.ktor.base.*
@@ -10,9 +11,9 @@ import io.ktor.client.HttpClient
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@RiskFeature @RiskFeature
fun createTelegramBotDefaultKtorCallRequestsFactories() = listOf( fun createTelegramBotDefaultKtorCallRequestsFactories(logger: KSLog? = null) = listOf(
SimpleRequestCallFactory(), SimpleRequestCallFactory(logger),
MultipartRequestCallFactory(), MultipartRequestCallFactory(logger),
DownloadFileRequestCallFactory, DownloadFileRequestCallFactory,
DownloadFileChannelRequestCallFactory DownloadFileChannelRequestCallFactory
) )
@@ -25,6 +26,8 @@ class KtorRequestsExecutorBuilder(
var excludeDefaultFactories: Boolean = false var excludeDefaultFactories: Boolean = false
var requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter var requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter
var jsonFormatter: Json = nonstrictJsonFormat var jsonFormatter: Json = nonstrictJsonFormat
var logger: KSLog = DefaultKTgBotAPIKSLog
var pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder
fun build() = KtorRequestsExecutor( fun build() = KtorRequestsExecutor(
telegramAPIUrlsKeeper, telegramAPIUrlsKeeper,
@@ -32,7 +35,9 @@ class KtorRequestsExecutorBuilder(
callsFactories, callsFactories,
excludeDefaultFactories, excludeDefaultFactories,
requestsLimiter, requestsLimiter,
jsonFormatter jsonFormatter,
pipelineStepsHolder,
logger
) )
} }

View File

@@ -1,23 +1,30 @@
package dev.inmo.tgbotapi.bot.ktor.base 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.micro_utils.coroutines.runCatchingSafely
import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
import dev.inmo.tgbotapi.bot.exceptions.newRequestException import dev.inmo.tgbotapi.bot.exceptions.newRequestException
import dev.inmo.tgbotapi.requests.GetUpdatesRequest import dev.inmo.tgbotapi.requests.GetUpdatesRequest
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.Response import dev.inmo.tgbotapi.types.Response
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.plugins.timeout import io.ktor.client.plugins.timeout
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.statement.bodyAsText import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType import io.ktor.http.ContentType
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlin.collections.set import kotlin.collections.set
var defaultUpdateTimeoutForZeroDelay = 1000L var defaultUpdateTimeoutForZeroDelay = 1000L
abstract class AbstractRequestCallFactory : KtorCallFactory { abstract class AbstractRequestCallFactory(
protected open val logger: KSLog = DefaultKTgBotAPIKSLog
) : KtorCallFactory {
private val methodsCache: MutableMap<String, String> = mutableMapOf() private val methodsCache: MutableMap<String, String> = mutableMapOf()
override suspend fun <T : Any> makeCall( override suspend fun <T : Any> makeCall(
client: HttpClient, client: HttpClient,
@@ -26,6 +33,7 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
jsonFormatter: Json jsonFormatter: Json
): T? { ): T? {
val preparedBody = prepareCallBody(client, urlsKeeper, request) ?: return null val preparedBody = prepareCallBody(client, urlsKeeper, request) ?: return null
logger.v { "Prepared body for $request: $preparedBody" }
client.post { client.post {
url( url(
@@ -54,7 +62,9 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
setBody(preparedBody) setBody(preparedBody)
}.let { response -> }.let { response ->
val content = response.bodyAsText() val content = response.bodyAsText()
logger.v { "Raw answer for $request: $content" }
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
logger.v { "Answer as json for $request: $responseObject" }
return runCatchingSafely { return runCatchingSafely {
(responseObject.result?.let { (responseObject.result?.let {
@@ -66,6 +76,8 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
"Can't get result object from $content" "Can't get result object from $content"
) )
}) })
}.onFailure {
logger.w { "Got exception answer for $request: $it" }
}.getOrThrow() }.getOrThrow()
} }
} }

View File

@@ -1,5 +1,9 @@
package dev.inmo.tgbotapi.bot.ktor.base 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.micro_utils.coroutines.runCatchingSafely
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
import dev.inmo.tgbotapi.bot.exceptions.BotException import dev.inmo.tgbotapi.bot.exceptions.BotException
@@ -7,6 +11,7 @@ import dev.inmo.tgbotapi.bot.exceptions.CommonBotException
import dev.inmo.tgbotapi.bot.exceptions.newRequestException import dev.inmo.tgbotapi.bot.exceptions.newRequestException
import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
import dev.inmo.tgbotapi.bot.ktor.KtorPipelineStepsHolder import dev.inmo.tgbotapi.bot.ktor.KtorPipelineStepsHolder
import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutor
import dev.inmo.tgbotapi.bot.ktor.createTelegramBotDefaultKtorCallRequestsFactories import dev.inmo.tgbotapi.bot.ktor.createTelegramBotDefaultKtorCallRequestsFactories
import dev.inmo.tgbotapi.bot.settings.limiters.ExceptionsOnlyLimiter import dev.inmo.tgbotapi.bot.settings.limiters.ExceptionsOnlyLimiter
import dev.inmo.tgbotapi.bot.settings.limiters.RequestLimiter import dev.inmo.tgbotapi.bot.settings.limiters.RequestLimiter
@@ -19,19 +24,23 @@ import io.ktor.client.plugins.*
import io.ktor.client.statement.* import io.ktor.client.statement.*
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
class DefaultKtorRequestsExecutor( class DefaultKtorRequestsExecutor internal constructor(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
client: HttpClient = HttpClient(), client: HttpClient,
callsFactories: List<KtorCallFactory> = emptyList(), callsFactories: List<KtorCallFactory>,
excludeDefaultFactories: Boolean = false, excludeDefaultFactories: Boolean,
private val requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter, private val requestsLimiter: RequestLimiter,
private val jsonFormatter: Json = nonstrictJsonFormat, private val jsonFormatter: Json,
private val pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder private val pipelineStepsHolder: KtorPipelineStepsHolder,
private val logger: KSLog,
diff: Unit
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) { ) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
private val callsFactories: List<KtorCallFactory> = callsFactories.run { private val callsFactories: List<KtorCallFactory> = callsFactories.run {
if (!excludeDefaultFactories) { if (!excludeDefaultFactories) {
this + createTelegramBotDefaultKtorCallRequestsFactories() logger.v { "Installing default factories" }
this + createTelegramBotDefaultKtorCallRequestsFactories(logger)
} else { } else {
logger.v { "Default factories will not be installed" }
this this
} }
} }
@@ -44,19 +53,23 @@ class DefaultKtorRequestsExecutor(
override suspend fun <T : Any> execute(request: Request<T>): T { override suspend fun <T : Any> execute(request: Request<T>): T {
return runCatchingSafely { return runCatchingSafely {
logger.v { "Start request $request" }
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories) pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
requestsLimiter.limit(request) { requestsLimiter.limit(request) {
var result: T? = null var result: T? = null
lateinit var factoryHandledRequest: KtorCallFactory lateinit var factoryHandledRequest: KtorCallFactory
for (potentialFactory in callsFactories) { for (potentialFactory in callsFactories) {
pipelineStepsHolder.onBeforeCallFactoryMakeCall(request, potentialFactory) pipelineStepsHolder.onBeforeCallFactoryMakeCall(request, potentialFactory)
result = potentialFactory.makeCall( logger.v { "Trying factory $potentialFactory for $request" }
val resultFromFactory = potentialFactory.makeCall(
client, client,
telegramAPIUrlsKeeper, telegramAPIUrlsKeeper,
request, request,
jsonFormatter 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) { if (result != null) {
factoryHandledRequest = potentialFactory factoryHandledRequest = potentialFactory
break break
@@ -69,6 +82,7 @@ class DefaultKtorRequestsExecutor(
} }
}.let { }.let {
val result = it.exceptionOrNull() ?.let { e -> val result = it.exceptionOrNull() ?.let { e ->
logger.v(e) { "Got exception on handling of $request" }
pipelineStepsHolder.onRequestException(request, e) ?.let { return@let it } pipelineStepsHolder.onRequestException(request, e) ?.let { return@let it }
when (e) { when (e) {
@@ -88,9 +102,13 @@ class DefaultKtorRequestsExecutor(
} }
is BotException -> e is BotException -> e
else -> CommonBotException(cause = e) else -> CommonBotException(cause = e)
}.also {
logger.v(e) { "Result exception on handling of $request: $it" }
} }
} ?.let { Result.failure(it) } ?: it } ?.let { Result.failure(it) } ?: it
pipelineStepsHolder.onRequestReturnResult(result, request, callsFactories) pipelineStepsHolder.onRequestReturnResult(result, request, callsFactories).also {
logger.v { "Result of handling $request: $it" }
}
} }
} }

View File

@@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
import dev.inmo.tgbotapi.requests.DownloadFileStream import dev.inmo.tgbotapi.requests.DownloadFileStream
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.utils.ByteReadChannelAllocator import dev.inmo.tgbotapi.utils.ByteReadChannelAllocator
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.call.receive import io.ktor.client.call.receive
@@ -15,6 +16,7 @@ import io.ktor.utils.io.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@RiskFeature
object DownloadFileChannelRequestCallFactory : KtorCallFactory { object DownloadFileChannelRequestCallFactory : KtorCallFactory {
override suspend fun <T : Any> makeCall( override suspend fun <T : Any> makeCall(
client: HttpClient, client: HttpClient,

View File

@@ -4,12 +4,14 @@ import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
import dev.inmo.tgbotapi.requests.DownloadFile import dev.inmo.tgbotapi.requests.DownloadFile
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.request.get import io.ktor.client.request.get
import io.ktor.client.statement.readBytes import io.ktor.client.statement.readBytes
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@RiskFeature
object DownloadFileRequestCallFactory : KtorCallFactory { object DownloadFileRequestCallFactory : KtorCallFactory {
override suspend fun <T : Any> makeCall( override suspend fun <T : Any> makeCall(
client: HttpClient, client: HttpClient,

View File

@@ -1,6 +1,8 @@
package dev.inmo.tgbotapi.bot.ktor.base package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.kslog.common.KSLog
import dev.inmo.tgbotapi.requests.abstracts.* import dev.inmo.tgbotapi.requests.abstracts.*
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import dev.inmo.tgbotapi.utils.mapWithCommonValues import dev.inmo.tgbotapi.utils.mapWithCommonValues
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
@@ -8,7 +10,7 @@ import io.ktor.client.request.forms.*
import io.ktor.http.Headers import io.ktor.http.Headers
import io.ktor.http.HttpHeaders import io.ktor.http.HttpHeaders
class MultipartRequestCallFactory : AbstractRequestCallFactory() { class MultipartRequestCallFactory(logger: KSLog? = null) : AbstractRequestCallFactory(logger ?: DefaultKTgBotAPIKSLog) {
override fun <T : Any> prepareCallBody( override fun <T : Any> prepareCallBody(
client: HttpClient, client: HttpClient,
urlsKeeper: TelegramAPIUrlsKeeper, urlsKeeper: TelegramAPIUrlsKeeper,

View File

@@ -1,12 +1,16 @@
package dev.inmo.tgbotapi.bot.ktor.base package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.kslog.common.KSLog
import dev.inmo.micro_utils.coroutines.runCatchingSafely import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
import dev.inmo.tgbotapi.bot.ktor.KtorPipelineStepsHolder import dev.inmo.tgbotapi.bot.ktor.KtorPipelineStepsHolder
import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutor
import dev.inmo.tgbotapi.bot.settings.limiters.ExceptionsOnlyLimiter
import dev.inmo.tgbotapi.bot.settings.limiters.RequestLimiter import dev.inmo.tgbotapi.bot.settings.limiters.RequestLimiter
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import io.ktor.client.* import io.ktor.client.*
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
@@ -45,6 +49,7 @@ class MultipleClientKtorRequestsExecutor (
jsonFormatter: Json, jsonFormatter: Json,
pipelineStepsHolder: KtorPipelineStepsHolder, pipelineStepsHolder: KtorPipelineStepsHolder,
requestExecutorsCount: Int, requestExecutorsCount: Int,
logger: KSLog,
clientFactory: () -> HttpClient clientFactory: () -> HttpClient
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) { ) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
private val requestExecutors = (0 until requestExecutorsCount).map { private val requestExecutors = (0 until requestExecutorsCount).map {
@@ -55,7 +60,9 @@ class MultipleClientKtorRequestsExecutor (
excludeDefaultFactories, excludeDefaultFactories,
requestsLimiter, requestsLimiter,
jsonFormatter, jsonFormatter,
pipelineStepsHolder pipelineStepsHolder,
logger,
Unit
) )
}.toSet() }.toSet()
private val freeClients = MutableStateFlow<Set<DefaultKtorRequestsExecutor>>(requestExecutors) private val freeClients = MutableStateFlow<Set<DefaultKtorRequestsExecutor>>(requestExecutors)
@@ -68,14 +75,16 @@ class MultipleClientKtorRequestsExecutor (
} }
} }
constructor( internal constructor(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
client: HttpClient, client: HttpClient,
callsFactories: List<KtorCallFactory>, callsFactories: List<KtorCallFactory>,
excludeDefaultFactories: Boolean, excludeDefaultFactories: Boolean,
requestsLimiter: RequestLimiter, requestsLimiter: RequestLimiter,
jsonFormatter: Json, jsonFormatter: Json,
pipelineStepsHolder: KtorPipelineStepsHolder pipelineStepsHolder: KtorPipelineStepsHolder,
logger: KSLog,
diff: Unit
) : this( ) : this(
telegramAPIUrlsKeeper, telegramAPIUrlsKeeper,
callsFactories, callsFactories,
@@ -84,6 +93,7 @@ class MultipleClientKtorRequestsExecutor (
jsonFormatter, jsonFormatter,
pipelineStepsHolder, pipelineStepsHolder,
client.engineConfig.threadsCount, client.engineConfig.threadsCount,
logger,
{ platformClientCopy(client) } { platformClientCopy(client) }
) )

View File

@@ -1,12 +1,14 @@
package dev.inmo.tgbotapi.bot.ktor.base package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.kslog.common.KSLog
import dev.inmo.tgbotapi.requests.abstracts.* import dev.inmo.tgbotapi.requests.abstracts.*
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.http.ContentType import io.ktor.http.ContentType
import io.ktor.http.content.TextContent import io.ktor.http.content.TextContent
class SimpleRequestCallFactory : AbstractRequestCallFactory() { class SimpleRequestCallFactory(logger: KSLog? = null) : AbstractRequestCallFactory(logger ?: DefaultKTgBotAPIKSLog) {
override fun <T : Any> prepareCallBody( override fun <T : Any> prepareCallBody(
client: HttpClient, client: HttpClient,
urlsKeeper: TelegramAPIUrlsKeeper, urlsKeeper: TelegramAPIUrlsKeeper,

View File

@@ -7,7 +7,9 @@ import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.chat.Chat
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyForwardedMessage
import dev.inmo.tgbotapi.types.stories.Story import dev.inmo.tgbotapi.types.stories.Story
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@@ -25,7 +27,7 @@ data class StoryContent(
replyToMessageId: MessageId?, replyToMessageId: MessageId?,
allowSendingWithoutReply: Boolean?, allowSendingWithoutReply: Boolean?,
replyMarkup: KeyboardMarkup? replyMarkup: KeyboardMarkup?
): Request<out Message> { ): Request<PossiblyForwardedMessage> {
return ForwardMessage( return ForwardMessage(
chat.id, chat.id,
toChatId = chatId, toChatId = chatId,

View File

@@ -0,0 +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 = TagLogger("KTgBot")

View File

@@ -0,0 +1 @@
package dev.inmo.tgbotapi

View File

@@ -0,0 +1,5 @@
package dev.inmo.tgbotapi.bot.ktor
import dev.inmo.tgbotapi.bot.ktor.base.MultipleClientKtorRequestsExecutor
actual typealias KtorRequestsExecutor = MultipleClientKtorRequestsExecutor

View File

@@ -0,0 +1,22 @@
package dev.inmo.tgbotapi.bot.ktor.base
import io.ktor.client.*
import io.ktor.client.engine.cio.*
/**
* This function is used in default constructor of [MultipleClientKtorRequestsExecutor] and on all non-native
* platforms and MingwX64 should return [client]
*
* On LinuxX64 it will create copy with Curl engine or throw an exception if engine is different with Curl
*
* @throws IllegalArgumentException When pass non Curl-based [HttpClient] on LinuxX64
*/
internal actual inline fun platformClientCopy(client: HttpClient): HttpClient = (client.engineConfig as? CIOEngineConfig) ?.let {
lateinit var config: HttpClientConfig<out CIOEngineConfig>
client.config {
config = this as HttpClientConfig<out CIOEngineConfig>
}.close()
HttpClient(CIO) {
this.plusAssign(config)
}
} ?: throw IllegalArgumentException("On LinuxX64 TelegramBotAPI currently support only Curl Ktor HttpClient engine")

View File

@@ -0,0 +1,9 @@
package dev.inmo.tgbotapi.requests.abstracts
import dev.inmo.micro_utils.common.MPPFile
import dev.inmo.micro_utils.ktor.common.input
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
actual fun MPPFile.asMultipartFile(): MultipartFile = MultipartFile(this.name) {
input()
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.utils
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.core.Input
import io.ktor.utils.io.readRemaining
actual suspend fun ByteReadChannel.asInput(): Input = readRemaining()

View File

@@ -0,0 +1,12 @@
package dev.inmo.tgbotapi.utils
import kotlinx.serialization.Serializable
//actual typealias MimeType = MimeType
@OptIn(RiskFeature::class)
@Serializable(MimeTypeSerializer::class)
actual data class MimeType(
actual val raw: String
)
internal actual fun createMimeType(raw: String): MimeType = MimeType(raw)

View File

@@ -4,6 +4,7 @@ import kotlinx.serialization.Serializable
//actual typealias MimeType = MimeType //actual typealias MimeType = MimeType
@OptIn(RiskFeature::class)
@Serializable(MimeTypeSerializer::class) @Serializable(MimeTypeSerializer::class)
actual data class MimeType( actual data class MimeType(
actual val raw: String actual val raw: String

View File

@@ -24,6 +24,7 @@ class TelegramBotAPISymbolProcessor(
private val outputFile: String = "Output", private val outputFile: String = "Output",
private val outputFolder: String? = null private val outputFolder: String? = null
) : SymbolProcessor { ) : SymbolProcessor {
@OptIn(RiskFeature::class)
private val classCastsIncludedClassName = ClassCastsIncluded::class.asClassName() private val classCastsIncludedClassName = ClassCastsIncluded::class.asClassName()
@OptIn(KspExperimental::class, RiskFeature::class) @OptIn(KspExperimental::class, RiskFeature::class)
override fun process(resolver: Resolver): List<KSAnnotated> { override fun process(resolver: Resolver): List<KSAnnotated> {

View File

@@ -13,7 +13,7 @@ kotlin {
sourceSets { sourceSets {
commonMain { commonMain {
dependencies { dependencies {
api project(":tgbotapi.core") api project(path: ":tgbotapi.core")
} }
} }
} }

View File

@@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.* import dev.inmo.tgbotapi.types.update.*
import dev.inmo.tgbotapi.types.update.abstracts.* import dev.inmo.tgbotapi.types.update.abstracts.*
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.extensions.asMediaGroupMessage import dev.inmo.tgbotapi.utils.extensions.asMediaGroupMessage
/** /**
@@ -22,6 +23,7 @@ fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? {
* Will convert incoming list of [Update]s to list with [Update]s, which include [dev.inmo.tgbotapi.types.message.abstracts.ContentMessage]s * Will convert incoming list of [Update]s to list with [Update]s, which include [dev.inmo.tgbotapi.types.message.abstracts.ContentMessage]s
* with [dev.inmo.tgbotapi.types.message.content.MediaGroupContent] * with [dev.inmo.tgbotapi.types.message.content.MediaGroupContent]
*/ */
@OptIn(RiskFeature::class)
fun List<Update>.convertWithMediaGroupUpdates(): List<Update> { fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
val resultUpdates = mutableListOf<Update>() val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<Pair<BaseSentMessageUpdate, PossiblySentViaBotCommonMessage<MediaGroupPartContent>>>>() val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<Pair<BaseSentMessageUpdate, PossiblySentViaBotCommonMessage<MediaGroupPartContent>>>>()