mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
Fixes in TelegramBot#withAction
This commit is contained in:
parent
0fec35f0dc
commit
7af5ab17b7
@ -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`
|
||||||
|
|
||||||
|
@ -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,21 +16,17 @@ 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 {
|
execute(actionRequest)
|
||||||
execute(actionRequest)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return try {
|
val result = safelyWithResult { block() }
|
||||||
safely { block() }
|
botActionJob.cancel()
|
||||||
} finally {
|
return result.getOrThrow()
|
||||||
botActionJob.cancel()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun <T> TelegramBot.withAction(
|
suspend fun <T> TelegramBot.withAction(
|
||||||
|
@ -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>,
|
||||||
|
Loading…
Reference in New Issue
Block a user