1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

Merge pull request #67 from InsanusMokrassar/0.24.1

0.24.1
This commit is contained in:
InsanusMokrassar 2020-03-18 23:02:01 +06:00 committed by GitHub
commit badb138bc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 399 additions and 70 deletions

View File

@ -9,6 +9,31 @@
* `TelegramBotAPI-extensions-api`: * `TelegramBotAPI-extensions-api`:
* Most part of sending media messages functions was removed and replaced with their `InputFile` args analogs * Most part of sending media messages functions was removed and replaced with their `InputFile` args analogs
### 0.24.1
* `TelegramBotAPI`:
* `UpdateReceiver` was replaced to the package `com.github.insanusmokrassar.TelegramBotAPI.updateshandlers`
* All functions inside `com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesPolling` are deprecated
and will be removed in some soon versions. Their replacement are able inside `TelegramBotAPI-extensions-api`
* `UpdatesFilter` is interface for now
* Previous `UpdatesFilter` class was renamed to `SimpleUpdatesFilter` and for backward compatibility was added
builder function `UpdatesFilter`, which will be removed in near releases
* `FlowsUpdatesFilter` now implements `UpdatesFilter`
* `BaseSentMessageUpdate` and `BaseEditMessageUpdate` interfaces was added
* `EditChannelPostUpdate` now is implementing `BaseEditMessageUpdate` interface
* `EditMessageUpdate` now is implementing `BaseEditMessageUpdate` interface
* `ChannelPostUpdate` now is implementing `BaseSentMessageUpdate` interface
* `MessageUpdate` now is implementing `BaseSentMessageUpdate` interface
* `UpdatesPoller` and all its usages, childs and childs usages now are deprecated
* `GetUpdates#timeout` type now is `Seconds` (in fact it is `Int` as previously)
* `KtorRequestsExecutor` now is using a copy of incoming `HttpClient` object and install `HttpTimeout` feature
* `AbstractRequestCallFactory` now setting up a custom delay in case if request is `GetUpdates`
* `TelegramBotAPI-extensions-api`:
* All functions from `com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesPolling` now available
in package `com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates.UpdatesPolling`
* Now new method of getting updates available: `startGettingUpdates` with `UpdatesFilter` as incoming first
parameter
* `startGettingUpdates` with `receiver` and `allowedUpdates` parameters now will handle updates by itself
## 0.23.0 TelegramBotAPI 4.6 ## 0.23.0 TelegramBotAPI 4.6

View File

@ -2,14 +2,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
suspend fun RequestsExecutor.getUpdates( suspend fun RequestsExecutor.getUpdates(
offset: UpdateIdentifier? = null, offset: UpdateIdentifier? = null,
limit: Int? = null, limit: Int? = null,
timeout: Int? = null, timeout: Seconds? = null,
allowed_updates: List<String>? = ALL_UPDATES_LIST allowed_updates: List<String>? = ALL_UPDATES_LIST
) = execute( ) = execute(
GetUpdates( GetUpdates(
@ -20,7 +19,7 @@ suspend fun RequestsExecutor.getUpdates(
suspend fun RequestsExecutor.getUpdates( suspend fun RequestsExecutor.getUpdates(
lastUpdate: Update, lastUpdate: Update,
limit: Int? = null, limit: Int? = null,
timeout: Int? = null, timeout: Seconds? = null,
allowed_updates: List<String>? = ALL_UPDATES_LIST allowed_updates: List<String>? = ALL_UPDATES_LIST
) = getUpdates( ) = getUpdates(
lastUpdate.updateId + 1, limit, timeout, allowed_updates lastUpdate.updateId + 1, limit, timeout, allowed_updates

View File

@ -0,0 +1,70 @@
package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.*
internal fun Update.lastUpdateIdentifier(): UpdateIdentifier {
return if (this is SentMediaGroupUpdate) {
origins.last().updateId
} else {
updateId
}
}
internal fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? {
return maxBy { it.updateId } ?.lastUpdateIdentifier()
}
internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseSentMessageUpdate>>()
for (update in this) {
when (update) {
is BaseEditMessageUpdate -> resultUpdates.add(
update.toEditMediaGroupUpdate()
)
is BaseSentMessageUpdate -> {
val data = update.data
if (data is MediaGroupMessage) {
mediaGroups.getOrPut(data.mediaGroupId) {
mutableListOf()
}.add(update)
} else {
resultUpdates.add(update)
}
}
else -> resultUpdates.add(update)
}
}
mediaGroups.values.map {
it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate ->
resultUpdates.add(mediaGroupUpdate)
}
}
resultUpdates.sortBy { it.updateId }
return resultUpdates
}
internal fun List<BaseSentMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let {
if (isEmpty()) {
return@let null
}
val resultList = sortedBy { it.updateId }
when (first()) {
is MessageUpdate -> MessageMediaGroupUpdate(resultList)
is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(resultList)
else -> null
}
}
internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let {
when (this) {
is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}")
}
}

View File

@ -0,0 +1,137 @@
package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.lastUpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.getUpdates
import com.github.insanusmokrassar.TelegramBotAPI.types.Seconds
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.*
import io.ktor.client.features.HttpRequestTimeoutException
import kotlinx.coroutines.*
fun RequestsExecutor.startGettingOfUpdates(
timeoutSeconds: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
allowedUpdates: List<String>? = null,
updatesReceiver: UpdateReceiver<Update>
): Job = scope.launch {
var lastUpdateIdentifier: UpdateIdentifier? = null
while (isActive) {
try {
supervisorScope {
val updates = getUpdates(
offset = lastUpdateIdentifier?.plus(1),
timeout = timeoutSeconds,
allowed_updates = allowedUpdates
).convertWithMediaGroupUpdates()
supervisorScope {
for (update in updates) {
updatesReceiver(update)
lastUpdateIdentifier = update.lastUpdateIdentifier()
}
}
}
} catch (e: HttpRequestTimeoutException) {
e // it is ok due to mechanism of long polling
} catch (e: RequestException) {
e // it is not ok, but in most cases it will mean that there is some limit for requests count
delay(1000L)
}
}
}
fun RequestsExecutor.startGettingOfUpdates(
updatesFilter: UpdatesFilter,
timeoutSeconds: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): Job = startGettingOfUpdates(
timeoutSeconds,
scope,
updatesFilter.allowedUpdates,
updatesFilter.asUpdateReceiver
)
fun RequestsExecutor.startGettingOfUpdates(
messageCallback: UpdateReceiver<MessageUpdate>? = null,
messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null,
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
editedMessageMediaGroupCallback: UpdateReceiver<EditMessageMediaGroupUpdate>? = null,
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
channelPostMediaGroupCallback: UpdateReceiver<ChannelPostMediaGroupUpdate>? = null,
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
editedChannelPostMediaGroupCallback: UpdateReceiver<EditChannelPostMediaGroupUpdate>? = null,
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
pollCallback: UpdateReceiver<PollUpdate>? = null,
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
timeoutSeconds: Seconds = 30,
scope: CoroutineScope = GlobalScope
): Job {
return startGettingOfUpdates(
SimpleUpdatesFilter(
messageCallback,
messageMediaGroupCallback,
editedMessageCallback,
editedMessageMediaGroupCallback,
channelPostCallback,
channelPostMediaGroupCallback,
editedChannelPostCallback,
editedChannelPostMediaGroupCallback,
chosenInlineResultCallback,
inlineQueryCallback,
callbackQueryCallback,
shippingQueryCallback,
preCheckoutQueryCallback,
pollCallback,
pollAnswerCallback
),
timeoutSeconds,
scope
)
}
fun RequestsExecutor.startGettingOfUpdates(
messageCallback: UpdateReceiver<MessageUpdate>? = null,
mediaGroupCallback: UpdateReceiver<MediaGroupUpdate>? = null,
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
pollCallback: UpdateReceiver<PollUpdate>? = null,
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
timeoutSeconds: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): Job = startGettingOfUpdates(
messageCallback = messageCallback,
messageMediaGroupCallback = mediaGroupCallback,
editedMessageCallback = editedMessageCallback,
editedMessageMediaGroupCallback = mediaGroupCallback,
channelPostCallback = channelPostCallback,
channelPostMediaGroupCallback = mediaGroupCallback,
editedChannelPostCallback = editedChannelPostCallback,
editedChannelPostMediaGroupCallback = mediaGroupCallback,
chosenInlineResultCallback = chosenInlineResultCallback,
inlineQueryCallback = inlineQueryCallback,
callbackQueryCallback = callbackQueryCallback,
shippingQueryCallback = shippingQueryCallback,
preCheckoutQueryCallback = preCheckoutQueryCallback,
pollCallback = pollCallback,
pollAnswerCallback = pollAnswerCallback,
timeoutSeconds = timeoutSeconds,
scope = scope
)

View File

@ -13,14 +13,16 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.call.receive import io.ktor.client.call.receive
import io.ktor.client.features.ClientRequestException import io.ktor.client.features.ClientRequestException
import io.ktor.client.features.HttpTimeout
import io.ktor.client.statement.HttpStatement import io.ktor.client.statement.HttpStatement
import io.ktor.client.statement.readText import io.ktor.client.statement.readText
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.supervisorScope
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
class KtorRequestsExecutor( class KtorRequestsExecutor(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
private val client: HttpClient = HttpClient(), client: HttpClient = HttpClient(),
callsFactories: List<KtorCallFactory> = emptyList(), callsFactories: List<KtorCallFactory> = emptyList(),
excludeDefaultFactories: Boolean = false, excludeDefaultFactories: Boolean = false,
private val requestsLimiter: RequestLimiter = EmptyLimiter, private val requestsLimiter: RequestLimiter = EmptyLimiter,
@ -34,50 +36,59 @@ class KtorRequestsExecutor(
} }
} }
private val client = client.config {
install(HttpTimeout)
}
override suspend fun <T : Any> execute(request: Request<T>): T { override suspend fun <T : Any> execute(request: Request<T>): T {
return requestsLimiter.limit { return try {
var statement: HttpStatement? = null supervisorScope {
for (factory in callsFactories) { requestsLimiter.limit {
statement = factory.prepareCall( var statement: HttpStatement? = null
client, for (factory in callsFactories) {
telegramAPIUrlsKeeper.commonAPIUrl, statement = factory.prepareCall(
request client,
) telegramAPIUrlsKeeper.commonAPIUrl,
if (statement != null) { request
break )
if (statement != null) {
break
}
}
val response = statement?.execute() ?: throw IllegalArgumentException("Can't execute request: $request")
val content = response.receive<String>()
val responseObject = jsonFormatter.parse(Response.serializer(), content)
(responseObject.result?.let {
jsonFormatter.fromJson(request.resultDeserializer, it)
} ?: responseObject.parameters?.let {
val error = it.error
if (error is RetryAfterError) {
delay(error.leftToRetry)
execute(request)
} else {
null
}
} ?: response.let {
throw newRequestException(
responseObject,
content,
"Can't get result object from $content"
)
})
} }
} }
try { } catch (e: ClientRequestException) {
val response = statement ?.execute() ?: throw IllegalArgumentException("Can't execute request: $request") val content = e.response.readText()
val content = response.receive<String>() val responseObject = jsonFormatter.parse(Response.serializer(), content)
val responseObject = jsonFormatter.parse(Response.serializer(), content) throw newRequestException(
responseObject,
(responseObject.result?.let { content,
jsonFormatter.fromJson(request.resultDeserializer, it) "Can't get result object from $content"
} ?: responseObject.parameters?.let { )
val error = it.error } catch (e: Exception) {
if (error is RetryAfterError) { throw e
delay(error.leftToRetry)
execute(request)
} else {
null
}
} ?: response.let {
throw newRequestException(
responseObject,
content,
"Can't get result object from $content"
)
})
} catch (e: ClientRequestException) {
val content = e.response.readText()
val responseObject = jsonFormatter.parse(Response.serializer(), content)
throw newRequestException(
responseObject,
content,
"Can't get result object from $content"
)
}
} }
} }

View File

@ -1,8 +1,10 @@
package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory
import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.features.timeout
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.statement.HttpStatement import io.ktor.client.statement.HttpStatement
import io.ktor.http.ContentType import io.ktor.http.ContentType
@ -28,6 +30,16 @@ abstract class AbstractRequestCallFactory : KtorCallFactory {
method = HttpMethod.Post method = HttpMethod.Post
accept(ContentType.Application.Json) accept(ContentType.Application.Json)
if (request is GetUpdates) {
request.timeout ?.times(1000L) ?.let { customTimeoutMillis ->
if (customTimeoutMillis > 0) {
timeout {
requestTimeoutMillis = customTimeoutMillis
}
}
}
}
body = preparedBody body = preparedBody
}, },
client client

View File

@ -4,6 +4,7 @@ import io.ktor.utils.io.core.Closeable
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
interface UpdatesPoller : Closeable { interface UpdatesPoller : Closeable {
fun start(scope: CoroutineScope = CoroutineScope(Dispatchers.Default)) fun start(scope: CoroutineScope = CoroutineScope(Dispatchers.Default))
} }

View File

@ -1,9 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests package com.github.insanusmokrassar.TelegramBotAPI.requests
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutDeserialization import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutDeserialization
import kotlinx.serialization.* import kotlinx.serialization.*
@ -17,7 +15,7 @@ private val updatesListSerializer = ArrayListSerializer(
data class GetUpdates( data class GetUpdates(
val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates
val limit: Int? = null, val limit: Int? = null,
val timeout: Int? = null, val timeout: Seconds? = null,
val allowed_updates: List<String>? = ALL_UPDATES_LIST val allowed_updates: List<String>? = ALL_UPDATES_LIST
): SimpleRequest<List<Update>> { ): SimpleRequest<List<Update>> {
override fun method(): String = "getUpdates" override fun method(): String = "getUpdates"

View File

@ -20,6 +20,8 @@ typealias PollIdentifier = String
typealias StickerSetName = String typealias StickerSetName = String
typealias FileUniqueId = String typealias FileUniqueId = String
typealias Seconds = Int
val callbackQueryAnswerLength = 0 until 200 val callbackQueryAnswerLength = 0 until 200
val captionLength = 0 until 1024 val captionLength = 0 until 1024
val textLength = 0 until 4096 val textLength = 0 until 4096

View File

@ -3,8 +3,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate
data class ChannelPostUpdate( data class ChannelPostUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: Message override val data: Message
) : BaseMessageUpdate ) : BaseSentMessageUpdate

View File

@ -2,9 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate
data class EditChannelPostUpdate( data class EditChannelPostUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: Message override val data: Message
) : BaseMessageUpdate ) : BaseEditMessageUpdate

View File

@ -2,9 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate
data class EditMessageUpdate( data class EditMessageUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: Message override val data: Message
) : BaseMessageUpdate ) : BaseEditMessageUpdate

View File

@ -3,8 +3,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate
data class MessageUpdate( data class MessageUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: Message override val data: Message
) : BaseMessageUpdate ) : BaseSentMessageUpdate

View File

@ -0,0 +1,3 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
interface BaseEditMessageUpdate : BaseMessageUpdate

View File

@ -0,0 +1,3 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
interface BaseSentMessageUpdate : BaseMessageUpdate

View File

@ -2,7 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers
import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -12,7 +12,7 @@ private fun <T> BroadcastChannel<T>.createUpdateReceiver(): UpdateReceiver<T> =
class FlowsUpdatesFilter( class FlowsUpdatesFilter(
broadcastChannelsSize: Int = Channel.CONFLATED broadcastChannelsSize: Int = Channel.CONFLATED
) { ): UpdatesFilter {
private val messageChannel: BroadcastChannel<MessageUpdate> = BroadcastChannel(broadcastChannelsSize) private val messageChannel: BroadcastChannel<MessageUpdate> = BroadcastChannel(broadcastChannelsSize)
private val messageMediaGroupChannel: BroadcastChannel<MessageMediaGroupUpdate> = BroadcastChannel(broadcastChannelsSize) private val messageMediaGroupChannel: BroadcastChannel<MessageMediaGroupUpdate> = BroadcastChannel(broadcastChannelsSize)
private val editedMessageChannel: BroadcastChannel<EditMessageUpdate> = BroadcastChannel(broadcastChannelsSize) private val editedMessageChannel: BroadcastChannel<EditMessageUpdate> = BroadcastChannel(broadcastChannelsSize)
@ -28,7 +28,12 @@ class FlowsUpdatesFilter(
private val preCheckoutQueryChannel: BroadcastChannel<PreCheckoutQueryUpdate> = BroadcastChannel(broadcastChannelsSize) private val preCheckoutQueryChannel: BroadcastChannel<PreCheckoutQueryUpdate> = BroadcastChannel(broadcastChannelsSize)
private val pollChannel: BroadcastChannel<PollUpdate> = BroadcastChannel(broadcastChannelsSize) private val pollChannel: BroadcastChannel<PollUpdate> = BroadcastChannel(broadcastChannelsSize)
val filter = UpdatesFilter( override val allowedUpdates: List<String>
get() = filter.allowedUpdates
override val asUpdateReceiver: UpdateReceiver<Update>
get() = filter.asUpdateReceiver
val filter = SimpleUpdatesFilter(
messageChannel.createUpdateReceiver(), messageChannel.createUpdateReceiver(),
messageMediaGroupChannel.createUpdateReceiver(), messageMediaGroupChannel.createUpdateReceiver(),
editedMessageChannel.createUpdateReceiver(), editedMessageChannel.createUpdateReceiver(),

View File

@ -11,13 +11,13 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaG
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.utils.* import com.github.insanusmokrassar.TelegramBotAPI.utils.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeUnsafe import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeUnsafe
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.HttpClientEngine import io.ktor.client.engine.HttpClientEngine
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
fun KtorUpdatesPoller( fun KtorUpdatesPoller(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
engine: HttpClientEngine, engine: HttpClientEngine,
@ -42,6 +42,7 @@ fun KtorUpdatesPoller(
) )
} }
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
class KtorUpdatesPoller( class KtorUpdatesPoller(
private val executor: RequestsExecutor, private val executor: RequestsExecutor,
private val timeoutSeconds: Int? = null, private val timeoutSeconds: Int? = null,

View File

@ -5,9 +5,58 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver
data class UpdatesFilter( typealias UpdateReceiver<T> = suspend (T) -> Unit
interface UpdatesFilter {
val asUpdateReceiver: UpdateReceiver<Update>
val allowedUpdates: List<String>
}
@Deprecated(
"It is builder function for SimpleUpdatesFilter",
ReplaceWith(
"SimpleUpdatesFilter",
"com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.SimpleUpdatesFilter"
)
)
fun UpdatesFilter(
messageCallback: UpdateReceiver<MessageUpdate>? = null,
messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null,
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
editedMessageMediaGroupCallback: UpdateReceiver<EditMessageMediaGroupUpdate>? = null,
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
channelPostMediaGroupCallback: UpdateReceiver<ChannelPostMediaGroupUpdate>? = null,
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
editedChannelPostMediaGroupCallback: UpdateReceiver<EditChannelPostMediaGroupUpdate>? = null,
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
pollUpdateCallback: UpdateReceiver<PollUpdate>? = null,
pollAnswerUpdateCallback: UpdateReceiver<PollAnswerUpdate>? = null,
unknownUpdateTypeCallback: UpdateReceiver<UnknownUpdateType>? = null
) = SimpleUpdatesFilter(
messageCallback,
messageMediaGroupCallback,
editedMessageCallback,
editedMessageMediaGroupCallback,
channelPostCallback,
channelPostMediaGroupCallback,
editedChannelPostCallback,
editedChannelPostMediaGroupCallback,
chosenInlineResultCallback,
inlineQueryCallback,
callbackQueryCallback,
shippingQueryCallback,
preCheckoutQueryCallback,
pollUpdateCallback,
pollAnswerUpdateCallback,
unknownUpdateTypeCallback
)
data class SimpleUpdatesFilter(
private val messageCallback: UpdateReceiver<MessageUpdate>? = null, private val messageCallback: UpdateReceiver<MessageUpdate>? = null,
private val messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null, private val messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null,
private val editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null, private val editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
@ -24,9 +73,9 @@ data class UpdatesFilter(
private val pollUpdateCallback: UpdateReceiver<PollUpdate>? = null, private val pollUpdateCallback: UpdateReceiver<PollUpdate>? = null,
private val pollAnswerUpdateCallback: UpdateReceiver<PollAnswerUpdate>? = null, private val pollAnswerUpdateCallback: UpdateReceiver<PollAnswerUpdate>? = null,
private val unknownUpdateTypeCallback: UpdateReceiver<UnknownUpdateType>? = null private val unknownUpdateTypeCallback: UpdateReceiver<UnknownUpdateType>? = null
) { ) : UpdatesFilter {
val asUpdateReceiver: UpdateReceiver<Update> = this::invoke override val asUpdateReceiver: UpdateReceiver<Update> = this::invoke
val allowedUpdates = listOfNotNull( override val allowedUpdates = listOfNotNull(
(messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE }, (messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE },
(editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE }, (editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE },
(channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST }, (channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST },
@ -96,7 +145,7 @@ fun createSimpleUpdateFilter(
pollCallback: UpdateReceiver<PollUpdate>? = null, pollCallback: UpdateReceiver<PollUpdate>? = null,
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null, pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
unknownCallback: UpdateReceiver<UnknownUpdateType>? = null unknownCallback: UpdateReceiver<UnknownUpdateType>? = null
): UpdatesFilter = UpdatesFilter( ): UpdatesFilter = SimpleUpdatesFilter(
messageCallback = messageCallback, messageCallback = messageCallback,
messageMediaGroupCallback = mediaGroupCallback, messageMediaGroupCallback = mediaGroupCallback,
editedMessageCallback = editedMessageCallback, editedMessageCallback = editedMessageCallback,
@ -113,4 +162,4 @@ fun createSimpleUpdateFilter(
pollUpdateCallback = pollCallback, pollUpdateCallback = pollCallback,
pollAnswerUpdateCallback = pollAnswerCallback, pollAnswerUpdateCallback = pollAnswerCallback,
unknownUpdateTypeCallback = unknownCallback unknownUpdateTypeCallback = unknownCallback
) )

View File

@ -10,8 +10,16 @@ import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.KtorUpdatesPol
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
import kotlinx.coroutines.* import kotlinx.coroutines.*
typealias UpdateReceiver<T> = suspend (T) -> Unit @Deprecated(
"Replaced",
ReplaceWith(
"UpdateReceiver",
"com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver"
)
)
typealias UpdateReceiver<T> = com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver<T>
@Deprecated("Replaced into TelegramBotAPI-extensions-api")
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
timeoutMillis: Long = 30 * 1000, timeoutMillis: Long = 30 * 1000,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
@ -28,6 +36,7 @@ fun RequestsExecutor.startGettingOfUpdates(
} }
} }
@Deprecated("Replaced into TelegramBotAPI-extensions-api")
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
messageCallback: UpdateReceiver<MessageUpdate>? = null, messageCallback: UpdateReceiver<MessageUpdate>? = null,
messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null, messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null,
@ -72,6 +81,7 @@ fun RequestsExecutor.startGettingOfUpdates(
) )
} }
@Deprecated("Replaced into TelegramBotAPI-extensions-api")
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
messageCallback: UpdateReceiver<MessageUpdate>? = null, messageCallback: UpdateReceiver<MessageUpdate>? = null,
mediaGroupCallback: UpdateReceiver<MediaGroupUpdate>? = null, mediaGroupCallback: UpdateReceiver<MediaGroupUpdate>? = null,

View File

@ -4,13 +4,13 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.HttpClientEngine import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
import io.ktor.client.engine.cio.endpoint import io.ktor.client.engine.cio.endpoint
import io.ktor.util.KtorExperimentalAPI import io.ktor.util.KtorExperimentalAPI
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
@KtorExperimentalAPI @KtorExperimentalAPI
fun KtorUpdatesPoller( fun KtorUpdatesPoller(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
@ -44,6 +44,7 @@ fun KtorUpdatesPoller(
) )
} }
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
@KtorExperimentalAPI @KtorExperimentalAPI
fun KtorUpdatesPoller( fun KtorUpdatesPoller(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,

View File

@ -7,6 +7,6 @@ uuid_version=0.0.7
ktor_version=1.3.1 ktor_version=1.3.1
library_group=com.github.insanusmokrassar library_group=com.github.insanusmokrassar
library_version=0.24.0 library_version=0.24.1
gradle_bintray_plugin_version=1.8.4 gradle_bintray_plugin_version=1.8.4