diff --git a/simbot-component-telegram-api/src/jvmMain/java/module-info.java b/simbot-component-telegram-api/src/jvmMain/java/module-info.java index 34ae149..23ca42c 100644 --- a/simbot-component-telegram-api/src/jvmMain/java/module-info.java +++ b/simbot-component-telegram-api/src/jvmMain/java/module-info.java @@ -3,6 +3,7 @@ requires simbot.common.core; requires simbot.common.ktor.inputfile; requires transitive simbot.component.telegram.type; + requires io.ktor.client.core; exports love.forte.simbot.telegram.api; exports love.forte.simbot.telegram.api.bot; diff --git a/simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/BotRequests.kt b/simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/BotRequests.kt index c5007fc..91a75f6 100644 --- a/simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/BotRequests.kt +++ b/simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/BotRequests.kt @@ -32,11 +32,10 @@ import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName import kotlin.jvm.JvmSynthetic -// TODO JVM - /** * Use [TelegramBot] to request the [TelegramApi] and get [HttpResponse]. * @see TelegramApi.requestRawBy + * @see TelegramBot.executeRaw */ @JvmSynthetic public suspend fun TelegramApi<*>.requestRawBy(bot: TelegramBot): HttpResponse = @@ -45,6 +44,7 @@ public suspend fun TelegramApi<*>.requestRawBy(bot: TelegramBot): HttpResponse = /** * Use [TelegramBot] to request the [TelegramApi] and get [TelegramApiResult] with [R]. * @see TelegramApi.requestResult + * @see TelegramBot.executeResult */ @JvmSynthetic public suspend fun TelegramApi.requestResultBy(bot: TelegramBot): TelegramApiResult = @@ -53,6 +53,7 @@ public suspend fun TelegramApi.requestResultBy(bot: TelegramBot): T /** * Use [TelegramBot] to request the [TelegramApi] and get [R]. * @see TelegramApi.requestData + * @see TelegramBot.execute */ @JvmSynthetic public suspend fun TelegramApi.requestDataBy(bot: TelegramBot): R = diff --git a/simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/TelegramBot.kt b/simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/TelegramBot.kt index fd7fd8b..5ea65bf 100644 --- a/simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/TelegramBot.kt +++ b/simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/TelegramBot.kt @@ -17,6 +17,7 @@ package love.forte.simbot.component.telegram.core.bot +import io.ktor.client.statement.* import love.forte.simbot.bot.Bot import love.forte.simbot.bot.ContactRelation import love.forte.simbot.bot.GroupRelation @@ -27,6 +28,8 @@ import love.forte.simbot.component.telegram.core.bot.command.TelegramBotCommands import love.forte.simbot.component.telegram.core.bot.command.TelegramBotCommandsUpdater import love.forte.simbot.component.telegram.core.component.TelegramComponent import love.forte.simbot.suspendrunner.ST +import love.forte.simbot.telegram.api.TelegramApi +import love.forte.simbot.telegram.api.TelegramApiResult import love.forte.simbot.telegram.api.update.Update import love.forte.simbot.telegram.type.BotCommandScope import love.forte.simbot.telegram.type.User @@ -124,6 +127,42 @@ public interface TelegramBot : Bot { */ public val commandsUpdater: TelegramBotCommandsUpdater + /** + * Execute [TelegramApi] by this bot. + * + * @throws Exception Any exception that may occur during the API request process + * + * @see requestDataBy + */ + @ST + public suspend fun execute(api: TelegramApi): R = + api.requestDataBy(this) + + /** + * Execute [TelegramApi] by this bot. + * + * @throws Exception Any exception that may occur during the API request process + * + * @return The raw [HttpResponse] + * @see requestRawBy + */ + @ST + public suspend fun executeRaw(api: TelegramApi<*>): HttpResponse = + api.requestRawBy(this) + + /** + * Execute [TelegramApi] by this bot. + * + * @throws Exception Any exception that may occur during the API request process + * + * @return The [TelegramApiResult] + * @see requestResultBy + */ + @ST + public suspend fun executeResult(api: TelegramApi): TelegramApiResult = + api.requestResultBy(this) + + override val groupRelation: GroupRelation? get() = null diff --git a/simbot-component-telegram-stdlib/src/jvmMain/java/module-info.java b/simbot-component-telegram-stdlib/src/jvmMain/java/module-info.java index adcbab0..38a7f11 100644 --- a/simbot-component-telegram-stdlib/src/jvmMain/java/module-info.java +++ b/simbot-component-telegram-stdlib/src/jvmMain/java/module-info.java @@ -11,6 +11,7 @@ requires static kotlinx.coroutines.reactive; requires static org.reactivestreams; + requires io.ktor.client.core; exports love.forte.simbot.telegram.stdlib.bot; exports love.forte.simbot.telegram.stdlib.event; diff --git a/simbot-component-telegram-stdlib/src/jvmMain/kotlin/love/forte/simbot/telegram/stdlib/bot/BotRequests.jvm.kt b/simbot-component-telegram-stdlib/src/jvmMain/kotlin/love/forte/simbot/telegram/stdlib/bot/BotRequests.jvm.kt index cc35bab..a0f761a 100644 --- a/simbot-component-telegram-stdlib/src/jvmMain/kotlin/love/forte/simbot/telegram/stdlib/bot/BotRequests.jvm.kt +++ b/simbot-component-telegram-stdlib/src/jvmMain/kotlin/love/forte/simbot/telegram/stdlib/bot/BotRequests.jvm.kt @@ -23,65 +23,132 @@ package love.forte.simbot.telegram.stdlib.bot import io.ktor.client.statement.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.future.future +import love.forte.simbot.annotations.InternalSimbotAPI +import love.forte.simbot.suspendrunner.reserve.SuspendReserve +import love.forte.simbot.suspendrunner.reserve.suspendReserve import love.forte.simbot.suspendrunner.runInNoScopeBlocking import love.forte.simbot.telegram.api.* import java.util.concurrent.CompletableFuture +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext /** * [requestRawBy] in async. * @see TelegramApi.requestRaw */ -@JvmSynthetic @JvmOverloads -public fun TelegramApi<*>.requestByAsync(bot: Bot, scope: CoroutineScope = bot): CompletableFuture = - scope.future { requestRawBy(bot) } +public fun TelegramApi<*>.requestByAsync( + bot: Bot, + scope: CoroutineScope? = null, + context: CoroutineContext? = null, +): CompletableFuture = + (scope ?: bot).future(context = context ?: EmptyCoroutineContext) { requestRawBy(bot) } /** * [requestResultBy] in async. * @see TelegramApi.requestResult */ -@JvmSynthetic @JvmOverloads public fun TelegramApi.requestResultByAsync( bot: Bot, - scope: CoroutineScope = bot + scope: CoroutineScope? = null, + context: CoroutineContext? = null, ): CompletableFuture> = - scope.future { requestResultBy(bot) } + (scope ?: bot).future(context = context ?: EmptyCoroutineContext) { requestResultBy(bot) } /** * [requestDataBy] in async. * @see TelegramApi.requestData */ -@JvmSynthetic @JvmOverloads -public fun TelegramApi.requestDataByAsync(bot: Bot, scope: CoroutineScope = bot): CompletableFuture = - scope.future { requestDataBy(bot) } +public fun TelegramApi.requestDataByAsync( + bot: Bot, + scope: CoroutineScope? = null, + context: CoroutineContext? = null, +): CompletableFuture = + (scope ?: bot).future(context = context ?: EmptyCoroutineContext) { requestDataBy(bot) } /** * [requestRawBy] in blocking. * @see requestRawBy */ -@JvmSynthetic -public fun TelegramApi<*>.requestByBlocking(bot: Bot): HttpResponse = - runInNoScopeBlocking { requestRawBy(bot) } +@JvmOverloads +public fun TelegramApi<*>.requestByBlocking( + bot: Bot, + context: CoroutineContext? = null, +): HttpResponse = + runInNoScopeBlocking(context = context ?: EmptyCoroutineContext) { requestRawBy(bot) } /** * [requestResultBy] in blocking. * @see requestResultBy */ -@JvmSynthetic +@JvmOverloads public fun TelegramApi.requestResultByBlocking( - bot: Bot + bot: Bot, + context: CoroutineContext? = null, ): TelegramApiResult = - runInNoScopeBlocking { requestResultBy(bot) } + runInNoScopeBlocking(context = context ?: EmptyCoroutineContext) { requestResultBy(bot) } + +/** + * [requestDataBy] in blocking. + * @see requestDataBy + */ +@JvmOverloads +public fun TelegramApi.requestDataByBlocking( + bot: Bot, + context: CoroutineContext? = null, +): R = + runInNoScopeBlocking(context = context ?: EmptyCoroutineContext) { requestDataBy(bot) } + + +/** + * [requestRawBy] in blocking. + * @see requestRawBy + */ +@OptIn(InternalSimbotAPI::class) +@JvmOverloads +public fun TelegramApi<*>.requestByReserve( + bot: Bot, + scope: CoroutineScope? = null, + context: CoroutineContext? = null, +): SuspendReserve = + suspendReserve( + scope = scope ?: bot, + context = context ?: EmptyCoroutineContext, + ) { requestRawBy(bot) } + +/** + * [requestResultBy] in blocking. + * @see requestResultBy + */ +@OptIn(InternalSimbotAPI::class) +@JvmOverloads +public fun TelegramApi.requestResultByReserve( + bot: Bot, + scope: CoroutineScope? = null, + context: CoroutineContext? = null, +): SuspendReserve> = + suspendReserve( + scope = scope ?: bot, + context = context ?: EmptyCoroutineContext, + ) { requestResultBy(bot) } /** * [requestDataBy] in blocking. * @see requestDataBy */ -@JvmSynthetic -public fun TelegramApi.requestDataByBlocking(bot: Bot): R = - runInNoScopeBlocking { requestDataBy(bot) } +@OptIn(InternalSimbotAPI::class) +@JvmOverloads +public fun TelegramApi.requestDataByReserve( + bot: Bot, + scope: CoroutineScope? = null, + context: CoroutineContext? = null, +): SuspendReserve = + suspendReserve( + scope = scope ?: bot, + context = context ?: EmptyCoroutineContext, + ) { requestDataBy(bot) }