1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

Fixes in TelegramBot#withAction

This commit is contained in:
InsanusMokrassar 2021-09-08 22:19:08 +06:00
parent 0fec35f0dc
commit 7af5ab17b7
3 changed files with 14 additions and 17 deletions

View File

@ -10,6 +10,8 @@
* `MultipartRequestCallFactory` now will use file name as multipart `filename` parameter instead of generated * `MultipartRequestCallFactory` now will use file name as multipart `filename` parameter instead of generated
filename filename
* New extension `MPPFile#asMultipartFile` * New extension `MPPFile#asMultipartFile`
* `API`
* Fixes in `TelegramBot#withAction`
* `Behaviour Builder`: * `Behaviour Builder`:
* New extensions `BehaviourContext#commandWithArgs` and `BehaviourContext#onCommandWithArgs` * New extensions `BehaviourContext#commandWithArgs` and `BehaviourContext#onCommandWithArgs`

View File

@ -1,13 +1,13 @@
package dev.inmo.tgbotapi.extensions.api.send package dev.inmo.tgbotapi.extensions.api.send
import dev.inmo.micro_utils.coroutines.safely import dev.inmo.micro_utils.coroutines.*
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.send.SendAction import dev.inmo.tgbotapi.requests.send.SendAction
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.actions.* import dev.inmo.tgbotapi.types.actions.*
import dev.inmo.tgbotapi.types.chat.abstracts.Chat import dev.inmo.tgbotapi.types.chat.abstracts.Chat
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlin.coroutines.coroutineContext
private const val refreshTime: MilliSeconds = (botActionActualityTime - 1) * 1000L private const val refreshTime: MilliSeconds = (botActionActualityTime - 1) * 1000L
typealias TelegramBotActionCallback<T> = suspend TelegramBot.() -> T typealias TelegramBotActionCallback<T> = suspend TelegramBot.() -> T
@ -16,8 +16,7 @@ suspend fun <T> TelegramBot.withAction(
actionRequest: SendAction, actionRequest: SendAction,
block: TelegramBotActionCallback<T> block: TelegramBotActionCallback<T>
): T { ): T {
val botActionJob = supervisorScope { val botActionJob = CoroutineScope(coroutineContext).launch {
launch {
while (isActive) { while (isActive) {
delay(refreshTime) delay(refreshTime)
safelyWithoutExceptions { safelyWithoutExceptions {
@ -25,12 +24,9 @@ suspend fun <T> TelegramBot.withAction(
} }
} }
} }
} val result = safelyWithResult { block() }
return try {
safely { block() }
} finally {
botActionJob.cancel() botActionJob.cancel()
} return result.getOrThrow()
} }
suspend fun <T> TelegramBot.withAction( suspend fun <T> TelegramBot.withAction(

View File

@ -4,6 +4,7 @@ import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.bot.RequestsExecutor
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlin.coroutines.coroutineContext
fun <T: Any> RequestsExecutor.executeAsync( fun <T: Any> RequestsExecutor.executeAsync(
request: Request<T>, request: Request<T>,
@ -16,9 +17,7 @@ fun <T: Any> RequestsExecutor.executeAsync(
suspend fun <T: Any> RequestsExecutor.executeAsync( suspend fun <T: Any> RequestsExecutor.executeAsync(
request: Request<T> request: Request<T>
): Deferred<T> = coroutineScope { ): Deferred<T> = executeAsync(request, CoroutineScope(coroutineContext))
executeAsync(request, this)
}
suspend fun <T: Any> RequestsExecutor.executeUnsafe( suspend fun <T: Any> RequestsExecutor.executeUnsafe(
request: Request<T>, request: Request<T>,