1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-12-20 05:05:44 +00:00
This commit is contained in:
2021-06-28 01:00:20 +06:00
parent 47fe048b10
commit 77dff639f0
7 changed files with 69 additions and 28 deletions

View File

@@ -28,16 +28,34 @@ class EntitiesBuilder internal constructor(
*/
fun build(): TextSourcesList = entities.toList()
fun add(source: TextSource) {
fun add(source: TextSource): EntitiesBuilder {
entitiesList.add(source)
return this
}
fun addAll(sources: Iterable<TextSource>): EntitiesBuilder {
entitiesList.addAll(sources)
return this
}
operator fun TextSource.unaryPlus() = add(this)
operator fun TextSourcesList.unaryPlus() = entitiesList.addAll(this)
operator fun invoke(vararg source: TextSource) = entitiesList.addAll(source)
operator fun TextSourcesList.unaryPlus() = addAll(this)
operator fun invoke(vararg source: TextSource) = addAll(source.toList())
operator fun String.unaryPlus() {
operator fun String.unaryPlus(): EntitiesBuilder {
add(dev.inmo.tgbotapi.types.MessageEntity.textsources.regular(this))
return this@EntitiesBuilder
}
operator fun plus(text: String) = text.unaryPlus()
operator fun plus(source: TextSource) = add(source)
operator fun plus(sources: Iterable<TextSource>) = addAll(sources)
operator fun plus(other: EntitiesBuilder) = if (other == this) {
// do nothing; assume user
this
} else {
addAll(other.entitiesList)
}
}

View File

@@ -17,13 +17,11 @@ import io.ktor.client.features.HttpRequestTimeoutException
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun TelegramBot.startGettingOfUpdatesByLongPolling(
fun TelegramBot.longPollingFlow(
timeoutSeconds: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
exceptionsHandler: (ExceptionHandler<Unit>)? = null,
allowedUpdates: List<String>? = null,
updatesReceiver: UpdateReceiver<Update>
): Job = scope.launch {
): Flow<Update> = channelFlow {
var lastUpdateIdentifier: UpdateIdentifier? = null
while (isActive) {
@@ -43,6 +41,12 @@ fun TelegramBot.startGettingOfUpdatesByLongPolling(
)
).let { originalUpdates ->
val converted = originalUpdates.convertWithMediaGroupUpdates()
/**
* Dirty hack for cases when the media group was retrieved not fully:
*
* We are throw out the last media group and will reretrieve it again in the next get updates
* and it will guarantee that it is full
*/
/**
* Dirty hack for cases when the media group was retrieved not fully:
*
@@ -56,17 +60,31 @@ fun TelegramBot.startGettingOfUpdatesByLongPolling(
}
}
safely {
safelyWithResult {
for (update in updates) {
updatesReceiver(update)
send(update)
lastUpdateIdentifier = update.lastUpdateIdentifier()
}
}.onFailure {
cancel(it as? CancellationException ?: return@onFailure)
}
}
}
}
fun TelegramBot.startGettingOfUpdatesByLongPolling(
timeoutSeconds: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
exceptionsHandler: (ExceptionHandler<Unit>)? = null,
allowedUpdates: List<String>? = null,
updatesReceiver: UpdateReceiver<Update>
): Job = longPollingFlow(timeoutSeconds, exceptionsHandler, allowedUpdates).subscribeSafely(
scope,
exceptionsHandler ?: defaultSafelyExceptionHandler,
updatesReceiver
)
fun TelegramBot.retrieveAccumulatedUpdates(
avoidInlineQueries: Boolean = false,
avoidCallbackQueries: Boolean = false,