From 0320da7614f8431c2b9a85c25cae6d284e3305f5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jan 2021 22:10:38 +0600 Subject: [PATCH] updates in long polling methods --- CHANGELOG.md | 4 +++ .../behaviour_builder/BehaviourBuilders.kt | 5 +-- .../utils/updates/retrieving/LongPolling.kt | 33 ++++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b94ea9055..66cef9afd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ * `Behaviour Builder`: * Extension `TelegramBot#buildBehaviour` have changed its return value: now it is `Job` instead of `FlowsUpdatesFilter` +* `Utils` + * New extensions `TelegramBot#longPolling` were added as new recommended way to start getting updates via long + polling + * Old extensions `RequestsExecutor#startGettingFlowsUpdatesByLongPolling` has been deprecated ## 0.30.13 diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt index 5ca9087674..cd14764a51 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt @@ -1,6 +1,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter import dev.inmo.tgbotapi.utils.PreviewFeature @@ -46,8 +47,8 @@ suspend fun TelegramBot.buildBehaviour( it, block ) - startGettingOfUpdatesByLongPolling( - updatesFilter = it, + longPolling( + it, scope = scope ) } diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index b22778975d..7519638811 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.extensions.utils.updates.retrieving import dev.inmo.micro_utils.coroutines.ExceptionHandler import dev.inmo.micro_utils.coroutines.safely import dev.inmo.tgbotapi.bot.RequestsExecutor +import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.exceptions.RequestException import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates import dev.inmo.tgbotapi.extensions.utils.updates.lastUpdateIdentifier @@ -15,7 +16,7 @@ import dev.inmo.tgbotapi.updateshandlers.* import dev.inmo.tgbotapi.utils.* import kotlinx.coroutines.* -fun RequestsExecutor.startGettingOfUpdatesByLongPolling( +fun TelegramBot.startGettingOfUpdatesByLongPolling( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), exceptionsHandler: (ExceptionHandler)? = null, @@ -65,6 +66,33 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling( } } +/** + * Will [startGettingOfUpdatesByLongPolling] using incoming [flowsUpdatesFilter]. It is assumed that you ALREADY CONFIGURE + * all updates receivers, because this method will trigger getting of updates and. + */ +fun TelegramBot.longPolling( + flowsUpdatesFilter: FlowsUpdatesFilter, + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + exceptionsHandler: ExceptionHandler? = null +): Job = flowsUpdatesFilter.run { + startGettingOfUpdatesByLongPolling(timeoutSeconds, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver) +} + +/** + * Will enable [longPolling] by creating [FlowsUpdatesFilter] with [flowsUpdatesFilterUpdatesKeeperCount] as an argument + * and applied [flowUpdatesPreset]. It is assumed that you WILL CONFIGURE all updates receivers in [flowUpdatesPreset], + * because of after [flowUpdatesPreset] method calling will be triggered getting of updates. + */ +@Suppress("unused") +fun TelegramBot.longPolling( + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + exceptionsHandler: ExceptionHandler? = null, + flowsUpdatesFilterUpdatesKeeperCount: Int = 100, + flowUpdatesPreset: FlowsUpdatesFilter.() -> Unit +): Job = longPolling(FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply(flowUpdatesPreset), timeoutSeconds, scope, exceptionsHandler) + /** * This method will create a new one [FlowsUpdatesFilter]. This method could be unsafe due to the fact that it will start * getting updates IMMEDIATELY. That means that your bot will be able to skip some of them until you will call @@ -72,6 +100,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling( * [flowUpdatesPreset] lambda - it will be called BEFORE starting updates getting */ @FlowPreview +@Deprecated("Will be removed soon", ReplaceWith("longPolling", "dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling")) @Suppress("unused") fun RequestsExecutor.startGettingFlowsUpdatesByLongPolling( timeoutSeconds: Seconds = 30, @@ -97,6 +126,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling( updatesFilter.asUpdateReceiver ) +@Deprecated("Will be removed soon", ReplaceWith("longPolling", "dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling")) fun RequestsExecutor.startGettingOfUpdatesByLongPolling( messageCallback: UpdateReceiver? = null, messageMediaGroupCallback: UpdateReceiver? = null, @@ -141,6 +171,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling( ) } +@Deprecated("Will be removed soon", ReplaceWith("longPolling", "dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling")) @Suppress("unused") fun RequestsExecutor.startGettingOfUpdatesByLongPolling( messageCallback: UpdateReceiver? = null,