From 62c42236482013f930292f2faac297d0eaaafd91 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 11:16:31 +0800 Subject: [PATCH 01/11] started 0.14.1 --- CHANGELOG.md | 2 ++ build.gradle | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52759d01f1..828157d7c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ some default library * `ForwardedFromChannelMessage` - for messages from channels * Changed logic of forwarded messages preparing +### 0.14.1 + ## 0.13.0 Telegram Polls * Type `PollOption` and `AnonymousPollOption` added diff --git a/build.gradle b/build.gradle index a7ed40969e..75bf10f8a9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -project.version = "0.14.0" +project.version = "0.14.1" project.group = "com.github.insanusmokrassar" buildscript { From 25dcd9bc5231b7309871c89177d3a9660a4691ac Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 11:26:58 +0800 Subject: [PATCH 02/11] Replace "UpdatesFilter" and "UpdatesPoller" into another package --- CHANGELOG.md | 2 + .../updateshandlers/UpdatesFilter.kt | 106 +++++++++++++++++ .../updateshandlers/UpdatesPoller.kt | 98 ++++++++++++++++ .../utils/extensions/UpdatesFilter.kt | 111 ++++-------------- .../utils/extensions/UpdatesPoller.kt | 102 ++-------------- .../utils/extensions/UpdatesPolling.kt | 2 + .../utils/extensions/Webhooks.kt | 2 +- 7 files changed, 244 insertions(+), 179 deletions(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesPoller.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 828157d7c4..8ae4d701f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ some default library ### 0.14.1 +* Replace `UpdatesFilter` and `UpdatesPoller` into another package + ## 0.13.0 Telegram Polls * Type `PollOption` and `AnonymousPollOption` added diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt new file mode 100644 index 0000000000..ec04cf59ee --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt @@ -0,0 +1,106 @@ +package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers + +import com.github.insanusmokrassar.TelegramBotAPI.types.* +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.utils.extensions.UpdateReceiver +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesFilter + +data class UpdatesFilter( + private val messageCallback: UpdateReceiver? = null, + private val messageMediaGroupCallback: UpdateReceiver? = null, + private val editedMessageCallback: UpdateReceiver? = null, + private val editedMessageMediaGroupCallback: UpdateReceiver? = null, + private val channelPostCallback: UpdateReceiver? = null, + private val channelPostMediaGroupCallback: UpdateReceiver? = null, + private val editedChannelPostCallback: UpdateReceiver? = null, + private val editedChannelPostMediaGroupCallback: UpdateReceiver? = null, + private val chosenInlineResultCallback: UpdateReceiver? = null, + private val inlineQueryCallback: UpdateReceiver? = null, + private val callbackQueryCallback: UpdateReceiver? = null, + private val shippingQueryCallback: UpdateReceiver? = null, + private val preCheckoutQueryCallback: UpdateReceiver? = null +) { + val asUpdateReceiver: UpdateReceiver = this::invoke + val allowedUpdates = listOfNotNull( + (messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE }, + (editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE }, + (channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST }, + (editedChannelPostCallback ?: editedChannelPostMediaGroupCallback) ?.let { UPDATE_EDITED_CHANNEL_POST }, + chosenInlineResultCallback ?.let { UPDATE_CHOSEN_INLINE_RESULT }, + inlineQueryCallback ?.let { UPDATE_INLINE_QUERY }, + callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY }, + shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY }, + preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY } + ) + + suspend fun invoke(update: Update) { + when (update) { + is MessageUpdate -> messageCallback ?.invoke(update) + is MessageMediaGroupUpdate -> messageMediaGroupCallback ?.also { receiver -> + receiver(update) + } ?: messageCallback ?.also { receiver -> + update.origins.mapNotNull { it as? MessageUpdate }.forEach { + receiver(it) + } + } + is EditMessageUpdate -> editedMessageCallback ?.invoke(update) + is EditMessageMediaGroupUpdate -> editedMessageMediaGroupCallback ?.also { receiver -> + receiver(update) + } ?: editedMessageCallback ?.also { receiver -> + update.origins.mapNotNull { it as? EditMessageUpdate }.forEach { + receiver(it) + } + } + is ChannelPostUpdate -> channelPostCallback ?.invoke(update) + is ChannelPostMediaGroupUpdate -> channelPostMediaGroupCallback ?.also { receiver -> + receiver(update) + } ?: channelPostCallback ?.also { receiver -> + update.origins.mapNotNull { it as? ChannelPostUpdate }.forEach { + receiver(it) + } + } + is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(update) + is EditChannelPostMediaGroupUpdate -> editedChannelPostMediaGroupCallback ?.also { receiver -> + receiver(update) + } ?: editedChannelPostCallback ?.also { receiver -> + update.origins.mapNotNull { it as? EditChannelPostUpdate }.forEach { + receiver(it) + } + } + is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update) + is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update) + is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update) + is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(update) + is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update) + } + } +} + +fun createSimpleUpdateFilter( + messageCallback: UpdateReceiver? = null, + mediaGroupCallback: UpdateReceiver? = null, + editedMessageCallback: UpdateReceiver? = null, + channelPostCallback: UpdateReceiver? = null, + editedChannelPostCallback: UpdateReceiver? = null, + chosenInlineResultCallback: UpdateReceiver? = null, + inlineQueryCallback: UpdateReceiver? = null, + callbackQueryCallback: UpdateReceiver? = null, + shippingQueryCallback: UpdateReceiver? = null, + preCheckoutQueryCallback: UpdateReceiver? = null +): UpdatesFilter = UpdatesFilter( + 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 +) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesPoller.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesPoller.kt new file mode 100644 index 0000000000..672208d2ea --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesPoller.kt @@ -0,0 +1,98 @@ +package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers + +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates +import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.DeleteWebhook +import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeUnsafe +import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId +import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate +import kotlinx.coroutines.* +import java.util.concurrent.Executors + +class UpdatesPoller( + private val executor: RequestsExecutor, + private val requestsDelayMillis: Long = 1000, + private val scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + private val allowedUpdates: List? = null, + private val block: UpdateReceiver +) { + private var lastHandledUpdate: UpdateIdentifier = 0L + private val mediaGroup: MutableList = mutableListOf() + + private var pollerJob: Job? = null + + private suspend fun sendToBlock(data: Update) { + block(data) + lastHandledUpdate = data.updateId + } + + private suspend fun pushMediaGroupUpdate(update: BaseMessageUpdate? = null) { + val inputMediaGroupId = (update ?.data as? MediaGroupMessage) ?.mediaGroupId + if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) { + mediaGroup.sortBy { it.updateId } + mediaGroup.toMediaGroupUpdate() ?.let { + sendToBlock(it) + } ?: mediaGroup.forEach { + sendToBlock(it) + } + mediaGroup.clear() + } + inputMediaGroupId ?.let { + mediaGroup.add(update) + } ?: sendToBlock(update ?: return) + } + + private suspend fun getUpdates(): List { + return executor.execute( + GetUpdates( + lastHandledUpdate + 1, // incremented because offset counted from 1 when updates id from 0 + allowed_updates = allowedUpdates + ) + ).map { + it.asUpdate + } + } + + private suspend fun handleUpdates(updates: List) { + for (update in updates) { + (update as? BaseMessageUpdate) ?.let { + if (it.data is MediaGroupMessage) { + pushMediaGroupUpdate(it) + } else { + null + } + } ?:let { + pushMediaGroupUpdate() + sendToBlock(update) + } + } + + pushMediaGroupUpdate() + } + + suspend fun start(): Job { + executor.executeUnsafe(DeleteWebhook()) + return pollerJob ?: scope.launch { + while (isActive) { + delay(requestsDelayMillis) + try { + val updates = getUpdates() + handleUpdates(updates) + } catch (e: Exception) { + e.printStackTrace() + } + } + }.also { + pollerJob = it + } + } + + suspend fun stop() { + pollerJob ?.cancelAndJoin() + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt index 4fcbdf807e..d8ca32c75d 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt @@ -1,81 +1,25 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions -import com.github.insanusmokrassar.TelegramBotAPI.types.* 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.UpdatesFilter -data class UpdatesFilter( - private val messageCallback: UpdateReceiver? = null, - private val messageMediaGroupCallback: UpdateReceiver? = null, - private val editedMessageCallback: UpdateReceiver? = null, - private val editedMessageMediaGroupCallback: UpdateReceiver? = null, - private val channelPostCallback: UpdateReceiver? = null, - private val channelPostMediaGroupCallback: UpdateReceiver? = null, - private val editedChannelPostCallback: UpdateReceiver? = null, - private val editedChannelPostMediaGroupCallback: UpdateReceiver? = null, - private val chosenInlineResultCallback: UpdateReceiver? = null, - private val inlineQueryCallback: UpdateReceiver? = null, - private val callbackQueryCallback: UpdateReceiver? = null, - private val shippingQueryCallback: UpdateReceiver? = null, - private val preCheckoutQueryCallback: UpdateReceiver? = null -) { - val asUpdateReceiver: UpdateReceiver = this::invoke - val allowedUpdates = listOfNotNull( - (messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE }, - (editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE }, - (channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST }, - (editedChannelPostCallback ?: editedChannelPostMediaGroupCallback) ?.let { UPDATE_EDITED_CHANNEL_POST }, - chosenInlineResultCallback ?.let { UPDATE_CHOSEN_INLINE_RESULT }, - inlineQueryCallback ?.let { UPDATE_INLINE_QUERY }, - callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY }, - shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY }, - preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY } +@Deprecated( + "Replaced in separated package", + ReplaceWith( + "UpdatesFilter", + "com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter" ) +) +typealias UpdatesFilter = UpdatesFilter - suspend fun invoke(update: Update) { - when (update) { - is MessageUpdate -> messageCallback ?.invoke(update) - is MessageMediaGroupUpdate -> messageMediaGroupCallback ?.also { receiver -> - receiver(update) - } ?: messageCallback ?.also { receiver -> - update.origins.mapNotNull { it as? MessageUpdate }.forEach { - receiver(it) - } - } - is EditMessageUpdate -> editedMessageCallback ?.invoke(update) - is EditMessageMediaGroupUpdate -> editedMessageMediaGroupCallback ?.also { receiver -> - receiver(update) - } ?: editedMessageCallback ?.also { receiver -> - update.origins.mapNotNull { it as? EditMessageUpdate }.forEach { - receiver(it) - } - } - is ChannelPostUpdate -> channelPostCallback ?.invoke(update) - is ChannelPostMediaGroupUpdate -> channelPostMediaGroupCallback ?.also { receiver -> - receiver(update) - } ?: channelPostCallback ?.also { receiver -> - update.origins.mapNotNull { it as? ChannelPostUpdate }.forEach { - receiver(it) - } - } - is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(update) - is EditChannelPostMediaGroupUpdate -> editedChannelPostMediaGroupCallback ?.also { receiver -> - receiver(update) - } ?: editedChannelPostCallback ?.also { receiver -> - update.origins.mapNotNull { it as? EditChannelPostUpdate }.forEach { - receiver(it) - } - } - is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update) - is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update) - is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update) - is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(update) - is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update) - } - } -} - +@Deprecated( + "Replaced in separated package", + ReplaceWith( + "createSimpleUpdateFilter", + "com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.createSimpleUpdateFilter" + ) +) fun createSimpleUpdateFilter( messageCallback: UpdateReceiver? = null, mediaGroupCallback: UpdateReceiver? = null, @@ -87,18 +31,15 @@ fun createSimpleUpdateFilter( callbackQueryCallback: UpdateReceiver? = null, shippingQueryCallback: UpdateReceiver? = null, preCheckoutQueryCallback: UpdateReceiver? = null -): UpdatesFilter = UpdatesFilter( - 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 +): UpdatesFilter = com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.createSimpleUpdateFilter( + messageCallback, + mediaGroupCallback, + editedMessageCallback, + channelPostCallback, + editedChannelPostCallback, + chosenInlineResultCallback, + inlineQueryCallback, + callbackQueryCallback, + shippingQueryCallback, + preCheckoutQueryCallback ) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPoller.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPoller.kt index 7ead0ba33b..1d87bd6c17 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPoller.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPoller.kt @@ -1,96 +1,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions -import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor -import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates -import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.DeleteWebhook -import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier -import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update -import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId -import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate -import kotlinx.coroutines.* -import java.util.concurrent.Executors +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesPoller -class UpdatesPoller( - private val executor: RequestsExecutor, - private val requestsDelayMillis: Long = 1000, - private val scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - private val allowedUpdates: List? = null, - private val block: UpdateReceiver -) { - private var lastHandledUpdate: UpdateIdentifier = 0L - private val mediaGroup: MutableList = mutableListOf() - - private var pollerJob: Job? = null - - private suspend fun sendToBlock(data: Update) { - block(data) - lastHandledUpdate = data.updateId - } - - private suspend fun pushMediaGroupUpdate(update: BaseMessageUpdate? = null) { - val inputMediaGroupId = (update ?.data as? MediaGroupMessage) ?.mediaGroupId - if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) { - mediaGroup.sortBy { it.updateId } - mediaGroup.toMediaGroupUpdate() ?.let { - sendToBlock(it) - } ?: mediaGroup.forEach { - sendToBlock(it) - } - mediaGroup.clear() - } - inputMediaGroupId ?.let { - mediaGroup.add(update) - } ?: sendToBlock(update ?: return) - } - - private suspend fun getUpdates(): List { - return executor.execute( - GetUpdates( - lastHandledUpdate + 1, // incremented because offset counted from 1 when updates id from 0 - allowed_updates = allowedUpdates - ) - ).map { - it.asUpdate - } - } - - private suspend fun handleUpdates(updates: List) { - for (update in updates) { - (update as? BaseMessageUpdate) ?.let { - if (it.data is MediaGroupMessage) { - pushMediaGroupUpdate(it) - } else { - null - } - } ?:let { - pushMediaGroupUpdate() - sendToBlock(update) - } - } - - pushMediaGroupUpdate() - } - - suspend fun start(): Job { - executor.executeUnsafe(DeleteWebhook()) - return pollerJob ?: scope.launch { - while (isActive) { - delay(requestsDelayMillis) - try { - val updates = getUpdates() - handleUpdates(updates) - } catch (e: Exception) { - e.printStackTrace() - } - } - }.also { - pollerJob = it - } - } - - suspend fun stop() { - pollerJob ?.cancelAndJoin() - } -} \ No newline at end of file +@Deprecated( + "Replaced in separated package", + ReplaceWith( + "UpdatesPoller", + "com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesPoller" + ) +) +typealias UpdatesPoller = UpdatesPoller diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt index 8777aec2f5..09250d70d5 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt @@ -4,6 +4,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor 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.UpdatesFilter +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesPoller import kotlinx.coroutines.* import java.util.concurrent.Executors diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index c198a22872..85022a9fdc 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -8,8 +8,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaG import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate -import io.ktor.application.Application import io.ktor.application.call import io.ktor.request.receiveText import io.ktor.response.respond From f8fbe824b92cda6645c991894c3fa898f77b0465 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 11:30:26 +0800 Subject: [PATCH 03/11] replace "WebhookPrivateKeyConfig" --- CHANGELOG.md | 1 + .../webhook/WebhookPrivateKeyConfig.kt | 23 +++++++++++++++ .../extensions/WebhookPrivateKeyConfig.kt | 29 ++++++------------- 3 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/webhook/WebhookPrivateKeyConfig.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ae4d701f9..b79c03276e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ some default library ### 0.14.1 * Replace `UpdatesFilter` and `UpdatesPoller` into another package +* Replace `WebhookPrivateKeyConfig` ## 0.13.0 Telegram Polls diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/webhook/WebhookPrivateKeyConfig.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/webhook/WebhookPrivateKeyConfig.kt new file mode 100644 index 0000000000..f6e6a7a1b0 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/webhook/WebhookPrivateKeyConfig.kt @@ -0,0 +1,23 @@ +package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook + +import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient +import java.io.FileInputStream +import java.security.KeyStore + +@Serializable +data class WebhookPrivateKeyConfig( + private val keyStorePath: String, + private val keyStorePassword: String, + val aliasName: String, + private val aliasPassword: String +) { + @Transient + val keyStore = KeyStore.getInstance("JKS").apply { + load(FileInputStream(keyStorePath), keyStorePassword()) + } + + fun keyStorePassword(): CharArray = keyStorePassword.toCharArray() + + fun aliasPassword(): CharArray = aliasPassword.toCharArray() +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/WebhookPrivateKeyConfig.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/WebhookPrivateKeyConfig.kt index bb5f4810c4..9462b67e2b 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/WebhookPrivateKeyConfig.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/WebhookPrivateKeyConfig.kt @@ -1,23 +1,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions -import kotlinx.serialization.Serializable -import kotlinx.serialization.Transient -import java.io.FileInputStream -import java.security.KeyStore +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig -@Serializable -data class WebhookPrivateKeyConfig( - private val keyStorePath: String, - private val keyStorePassword: String, - val aliasName: String, - private val aliasPassword: String -) { - @Transient - val keyStore = KeyStore.getInstance("JKS").apply { - load(FileInputStream(keyStorePath), keyStorePassword()) - } - - fun keyStorePassword(): CharArray = keyStorePassword.toCharArray() - - fun aliasPassword(): CharArray = aliasPassword.toCharArray() -} +@Deprecated( + "Replaced in separated package", + ReplaceWith( + "WebhookPrivateKeyConfig", + "com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig" + ) +) +typealias WebhookPrivateKeyConfig = WebhookPrivateKeyConfig From ab2a3ea23f08625477df8de194c39bdae8262c6d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 11:54:57 +0800 Subject: [PATCH 04/11] add "FlowsUpdatesFilter" --- CHANGELOG.md | 1 + .../updateshandlers/FlowsUpdatesFilter.kt | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index b79c03276e..56386fae3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ some default library * Replace `UpdatesFilter` and `UpdatesPoller` into another package * Replace `WebhookPrivateKeyConfig` +* Added `FlowsUpdatesFilter` ## 0.13.0 Telegram Polls diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt new file mode 100644 index 0000000000..0948630dca --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt @@ -0,0 +1,60 @@ +package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers + +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.utils.extensions.UpdateReceiver +import kotlinx.coroutines.channels.BroadcastChannel +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.asFlow + +private fun BroadcastChannel.createUpdateReceiver(): UpdateReceiver = ::send + +class FlowsUpdatesFilter { + private val messageChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val messageMediaGroupChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val editedMessageChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val editedMessageMediaGroupChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val channelPostChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val channelPostMediaGroupChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val editedChannelPostChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val editedChannelPostMediaGroupChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val chosenInlineResultChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val inlineQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val channelQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val shippingQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val preCheckoutQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + + val filter = UpdatesFilter( + messageChannel.createUpdateReceiver(), + messageMediaGroupChannel.createUpdateReceiver(), + editedMessageChannel.createUpdateReceiver(), + editedMessageMediaGroupChannel.createUpdateReceiver(), + channelPostChannel.createUpdateReceiver(), + channelPostMediaGroupChannel.createUpdateReceiver(), + editedChannelPostChannel.createUpdateReceiver(), + editedChannelPostMediaGroupChannel.createUpdateReceiver(), + chosenInlineResultChannel.createUpdateReceiver(), + inlineQueryChannel.createUpdateReceiver(), + channelQueryChannel.createUpdateReceiver(), + shippingQueryChannel.createUpdateReceiver(), + preCheckoutQueryChannel.createUpdateReceiver() + ) + + val asUpdateReceiver: UpdateReceiver = filter.asUpdateReceiver + + val messageChannelFlow: Flow = messageChannel.asFlow() + val messageMediaGroupChannelFlow: Flow = messageMediaGroupChannel.asFlow() + val editedMessageChannelFlow: Flow = editedMessageChannel.asFlow() + val editedMessageMediaGroupChannelFlow: Flow = editedMessageMediaGroupChannel.asFlow() + val channelPostChannelFlow: Flow = channelPostChannel.asFlow() + val channelPostMediaGroupChannelFlow: Flow = channelPostMediaGroupChannel.asFlow() + val editedChannelPostChannelFlow: Flow = editedChannelPostChannel.asFlow() + val editedChannelPostMediaGroupChannelFlow: Flow = editedChannelPostMediaGroupChannel.asFlow() + val chosenInlineResultChannelFlow: Flow = chosenInlineResultChannel.asFlow() + val inlineQueryChannelFlow: Flow = inlineQueryChannel.asFlow() + val channelQueryChannelFlow: Flow = channelQueryChannel.asFlow() + val shippingQueryChannelFlow: Flow = shippingQueryChannel.asFlow() + val preCheckoutQueryChannelFlow: Flow = preCheckoutQueryChannel.asFlow() +} \ No newline at end of file From 9ddecb99ee032d43917d2181a13bb2f44280aefc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 11:58:43 +0800 Subject: [PATCH 05/11] remove redundant methods from FlowsUpdatesFilter --- .../TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt | 3 --- .../TelegramBotAPI/updateshandlers/UpdatesFilter.kt | 1 - 2 files changed, 4 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt index 0948630dca..d69f72496d 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt @@ -2,7 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers 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.utils.extensions.UpdateReceiver import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel @@ -42,8 +41,6 @@ class FlowsUpdatesFilter { preCheckoutQueryChannel.createUpdateReceiver() ) - val asUpdateReceiver: UpdateReceiver = filter.asUpdateReceiver - val messageChannelFlow: Flow = messageChannel.asFlow() val messageMediaGroupChannelFlow: Flow = messageMediaGroupChannel.asFlow() val editedMessageChannelFlow: Flow = editedMessageChannel.asFlow() diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt index ec04cf59ee..e63a3862c6 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt @@ -5,7 +5,6 @@ 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.utils.extensions.UpdateReceiver -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesFilter data class UpdatesFilter( private val messageCallback: UpdateReceiver? = null, From 62d13f89c84ef9b76499285740a0e918ecf2f946 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 12:03:40 +0800 Subject: [PATCH 06/11] acutalize Webhooks imports --- .../insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index 85022a9fdc..af6804d443 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -9,6 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate import io.ktor.application.call import io.ktor.request.receiveText From 981e6f43d9da91bc043fcde70bc39ed2fe30924a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 12:12:37 +0800 Subject: [PATCH 07/11] fix names of fields in FlowsUpdatesFilter --- .../updateshandlers/FlowsUpdatesFilter.kt | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt index d69f72496d..93603d1119 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt @@ -21,7 +21,7 @@ class FlowsUpdatesFilter { private val editedChannelPostMediaGroupChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) private val chosenInlineResultChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) private val inlineQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) - private val channelQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val callbackQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) private val shippingQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) private val preCheckoutQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) @@ -36,22 +36,22 @@ class FlowsUpdatesFilter { editedChannelPostMediaGroupChannel.createUpdateReceiver(), chosenInlineResultChannel.createUpdateReceiver(), inlineQueryChannel.createUpdateReceiver(), - channelQueryChannel.createUpdateReceiver(), + callbackQueryChannel.createUpdateReceiver(), shippingQueryChannel.createUpdateReceiver(), preCheckoutQueryChannel.createUpdateReceiver() ) - val messageChannelFlow: Flow = messageChannel.asFlow() - val messageMediaGroupChannelFlow: Flow = messageMediaGroupChannel.asFlow() - val editedMessageChannelFlow: Flow = editedMessageChannel.asFlow() - val editedMessageMediaGroupChannelFlow: Flow = editedMessageMediaGroupChannel.asFlow() - val channelPostChannelFlow: Flow = channelPostChannel.asFlow() - val channelPostMediaGroupChannelFlow: Flow = channelPostMediaGroupChannel.asFlow() - val editedChannelPostChannelFlow: Flow = editedChannelPostChannel.asFlow() - val editedChannelPostMediaGroupChannelFlow: Flow = editedChannelPostMediaGroupChannel.asFlow() - val chosenInlineResultChannelFlow: Flow = chosenInlineResultChannel.asFlow() - val inlineQueryChannelFlow: Flow = inlineQueryChannel.asFlow() - val channelQueryChannelFlow: Flow = channelQueryChannel.asFlow() - val shippingQueryChannelFlow: Flow = shippingQueryChannel.asFlow() - val preCheckoutQueryChannelFlow: Flow = preCheckoutQueryChannel.asFlow() + val messageFlow: Flow = messageChannel.asFlow() + val messageMediaGroupFlow: Flow = messageMediaGroupChannel.asFlow() + val editedMessageFlow: Flow = editedMessageChannel.asFlow() + val editedMessageMediaGroupFlow: Flow = editedMessageMediaGroupChannel.asFlow() + val channelPostFlow: Flow = channelPostChannel.asFlow() + val channelPostMediaGroupFlow: Flow = channelPostMediaGroupChannel.asFlow() + val editedChannelPostFlow: Flow = editedChannelPostChannel.asFlow() + val editedChannelPostMediaGroupFlow: Flow = editedChannelPostMediaGroupChannel.asFlow() + val chosenInlineResultFlow: Flow = chosenInlineResultChannel.asFlow() + val inlineQueryFlow: Flow = inlineQueryChannel.asFlow() + val callbackQueryFlow: Flow = callbackQueryChannel.asFlow() + val shippingQueryFlow: Flow = shippingQueryChannel.asFlow() + val preCheckoutQueryFlow: Flow = preCheckoutQueryChannel.asFlow() } \ No newline at end of file From a5ca9c0ce73b2088b5b03a3df79f5f6c487a74f1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 13:14:50 +0800 Subject: [PATCH 08/11] additional callback for polls in UpdatesFilter --- CHANGELOG.md | 1 + .../TelegramBotAPI/requests/GetUpdates.kt | 2 ++ .../TelegramBotAPI/types/UpdateTypes.kt | 4 +++- .../updateshandlers/FlowsUpdatesFilter.kt | 2 ++ .../TelegramBotAPI/updateshandlers/UpdatesFilter.kt | 13 +++++++++---- .../utils/extensions/UpdatesPolling.kt | 6 +++++- 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56386fae3d..86b0f9e79e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ some default library * Replace `UpdatesFilter` and `UpdatesPoller` into another package * Replace `WebhookPrivateKeyConfig` * Added `FlowsUpdatesFilter` +* `UpdatesFilter` now have additional callback for polls ## 0.13.0 Telegram Polls diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt index b5f88f9882..3c697c3a7d 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt @@ -26,6 +26,8 @@ const val UPDATE_CALLBACK_QUERY = com.github.insanusmokrassar.TelegramBotAPI.typ const val UPDATE_SHIPPING_QUERY = com.github.insanusmokrassar.TelegramBotAPI.types.UPDATE_SHIPPING_QUERY @Deprecated("Replaced to other package", ReplaceWith("UPDATE_PRE_CHECKOUT_QUERY", "com.github.insanusmokrassar.TelegramBotAPI.types.UPDATE_PRE_CHECKOUT_QUERY")) const val UPDATE_PRE_CHECKOUT_QUERY = com.github.insanusmokrassar.TelegramBotAPI.types.UPDATE_PRE_CHECKOUT_QUERY +@Deprecated("Replaced to other package", ReplaceWith("UPDATE_POLL", "com.github.insanusmokrassar.TelegramBotAPI.types.UPDATE_POLL")) +const val UPDATE_POLL = com.github.insanusmokrassar.TelegramBotAPI.types.UPDATE_POLL @Serializable data class GetUpdates( diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/UpdateTypes.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/UpdateTypes.kt index e5d477db3f..a24e00d4eb 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/UpdateTypes.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/UpdateTypes.kt @@ -9,6 +9,7 @@ const val UPDATE_INLINE_QUERY = "inline_query" const val UPDATE_CALLBACK_QUERY = "callback_query" const val UPDATE_SHIPPING_QUERY = "shipping_query" const val UPDATE_PRE_CHECKOUT_QUERY = "pre_checkout_query" +const val UPDATE_POLL = "poll" val ALL_UPDATES_LIST = listOf( UPDATE_MESSAGE, @@ -19,5 +20,6 @@ val ALL_UPDATES_LIST = listOf( UPDATE_INLINE_QUERY, UPDATE_CALLBACK_QUERY, UPDATE_SHIPPING_QUERY, - UPDATE_PRE_CHECKOUT_QUERY + UPDATE_PRE_CHECKOUT_QUERY, + UPDATE_POLL ) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt index 93603d1119..5fa8b96d1a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt @@ -24,6 +24,7 @@ class FlowsUpdatesFilter { private val callbackQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) private val shippingQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) private val preCheckoutQueryChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + private val pollChannel: BroadcastChannel = BroadcastChannel(Channel.CONFLATED) val filter = UpdatesFilter( messageChannel.createUpdateReceiver(), @@ -54,4 +55,5 @@ class FlowsUpdatesFilter { val callbackQueryFlow: Flow = callbackQueryChannel.asFlow() val shippingQueryFlow: Flow = shippingQueryChannel.asFlow() val preCheckoutQueryFlow: Flow = preCheckoutQueryChannel.asFlow() + val pollFlow: Flow = pollChannel.asFlow() } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt index e63a3862c6..218ec5737b 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt @@ -19,7 +19,8 @@ data class UpdatesFilter( private val inlineQueryCallback: UpdateReceiver? = null, private val callbackQueryCallback: UpdateReceiver? = null, private val shippingQueryCallback: UpdateReceiver? = null, - private val preCheckoutQueryCallback: UpdateReceiver? = null + private val preCheckoutQueryCallback: UpdateReceiver? = null, + private val pollUpdateCallback: UpdateReceiver? = null ) { val asUpdateReceiver: UpdateReceiver = this::invoke val allowedUpdates = listOfNotNull( @@ -31,7 +32,8 @@ data class UpdatesFilter( inlineQueryCallback ?.let { UPDATE_INLINE_QUERY }, callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY }, shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY }, - preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY } + preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }, + pollUpdateCallback ?.let { UPDATE_POLL } ) suspend fun invoke(update: Update) { @@ -73,6 +75,7 @@ data class UpdatesFilter( is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update) is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(update) is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update) + is PollUpdate -> pollUpdateCallback ?.invoke(update) } } } @@ -87,7 +90,8 @@ fun createSimpleUpdateFilter( inlineQueryCallback: UpdateReceiver? = null, callbackQueryCallback: UpdateReceiver? = null, shippingQueryCallback: UpdateReceiver? = null, - preCheckoutQueryCallback: UpdateReceiver? = null + preCheckoutQueryCallback: UpdateReceiver? = null, + pollCallback: UpdateReceiver? = null ): UpdatesFilter = UpdatesFilter( messageCallback = messageCallback, messageMediaGroupCallback = mediaGroupCallback, @@ -101,5 +105,6 @@ fun createSimpleUpdateFilter( inlineQueryCallback = inlineQueryCallback, callbackQueryCallback = callbackQueryCallback, shippingQueryCallback = shippingQueryCallback, - preCheckoutQueryCallback = preCheckoutQueryCallback + preCheckoutQueryCallback = preCheckoutQueryCallback, + pollUpdateCallback = pollCallback ) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt index 09250d70d5..42b87eced1 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt @@ -34,6 +34,7 @@ suspend fun RequestsExecutor.startGettingOfUpdates( callbackQueryCallback: UpdateReceiver? = null, shippingQueryCallback: UpdateReceiver? = null, preCheckoutQueryCallback: UpdateReceiver? = null, + pollCallback: UpdateReceiver? = null, requestsDelayMillis: Long = 1000, scope: CoroutineScope = GlobalScope ): Job { @@ -50,7 +51,8 @@ suspend fun RequestsExecutor.startGettingOfUpdates( inlineQueryCallback, callbackQueryCallback, shippingQueryCallback, - preCheckoutQueryCallback + preCheckoutQueryCallback, + pollCallback ) return startGettingOfUpdates( requestsDelayMillis, @@ -71,6 +73,7 @@ suspend fun RequestsExecutor.startGettingOfUpdates( callbackQueryCallback: UpdateReceiver? = null, shippingQueryCallback: UpdateReceiver? = null, preCheckoutQueryCallback: UpdateReceiver? = null, + pollCallback: UpdateReceiver? = null, requestsDelayMillis: Long = 1000, scope: CoroutineScope = GlobalScope ): Job = startGettingOfUpdates( @@ -87,6 +90,7 @@ suspend fun RequestsExecutor.startGettingOfUpdates( callbackQueryCallback = callbackQueryCallback, shippingQueryCallback = shippingQueryCallback, preCheckoutQueryCallback = preCheckoutQueryCallback, + pollCallback = pollCallback, requestsDelayMillis = requestsDelayMillis, scope = scope ) From 495a063f611b00d86603224968c3f7453f8fffe6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 May 2019 19:17:47 +0800 Subject: [PATCH 09/11] StopPoll#replyMarkup --- CHANGELOG.md | 1 + .../github/insanusmokrassar/TelegramBotAPI/requests/StopPoll.kt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86b0f9e79e..a71af3dd13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ some default library * Replace `WebhookPrivateKeyConfig` * Added `FlowsUpdatesFilter` * `UpdatesFilter` now have additional callback for polls +* `StopPoll#replyMarkup` now is optional ## 0.13.0 Telegram Polls diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/StopPoll.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/StopPoll.kt index 8f5a4118f0..f5327fa719 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/StopPoll.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/StopPoll.kt @@ -15,7 +15,7 @@ data class StopPoll( @SerialName(messageIdField) override val messageId: MessageIdentifier, @SerialName(replyMarkupField) - override val replyMarkup: InlineKeyboardMarkup? + override val replyMarkup: InlineKeyboardMarkup? = null ) : MessageAction, SimpleRequest, ReplyMarkup { override fun method(): String = "stopPoll" override fun resultSerializer(): KSerializer = Poll.serializer() From d0e0158a5d20ca163df4bb83d2239bce30a30960 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 11 May 2019 11:34:00 +0800 Subject: [PATCH 10/11] fix for poll channels skipping in FlowsUpdatesFilter --- .../TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt index 5fa8b96d1a..6efc1d9abf 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt @@ -39,7 +39,8 @@ class FlowsUpdatesFilter { inlineQueryChannel.createUpdateReceiver(), callbackQueryChannel.createUpdateReceiver(), shippingQueryChannel.createUpdateReceiver(), - preCheckoutQueryChannel.createUpdateReceiver() + preCheckoutQueryChannel.createUpdateReceiver(), + pollChannel.createUpdateReceiver() ) val messageFlow: Flow = messageChannel.asFlow() From 4b44c589ce100f9223853a95f7678f4c92e70272 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 12 May 2019 19:15:31 +0800 Subject: [PATCH 11/11] optimize imports --- .../TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt | 1 - .../bot/Ktor/base/MultipartRequestCallFactory.kt | 3 ++- .../insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt | 4 +--- .../TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt | 4 ++-- .../TelegramBotAPI/utils/extensions/UpdatesFilter.kt | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt index cc4e3a2643..958b1bd33d 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt @@ -14,7 +14,6 @@ import io.ktor.client.call.HttpClientCall import io.ktor.client.engine.HttpClientEngine import io.ktor.util.cio.toByteArray import kotlinx.coroutines.delay -import kotlinx.io.charsets.Charset import kotlinx.serialization.json.Json class KtorRequestsExecutor( diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt index 2f2e1015f0..c90ba16e95 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt @@ -5,7 +5,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mapWithCommonValues import io.ktor.client.HttpClient import io.ktor.client.request.forms.MultiPartFormDataContent import io.ktor.client.request.forms.formData -import io.ktor.http.* +import io.ktor.http.Headers +import io.ktor.http.HttpHeaders class MultipartRequestCallFactory : AbstractRequestCallFactory() { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt index 8a9eee0b57..347a2b7bd9 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt @@ -5,9 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.SendMe import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage -import kotlinx.serialization.KSerializer -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import kotlinx.serialization.* @Serializable data class SendPoll( diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt index a5ff3ee61e..64d3490c59 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardMarkup.kt @@ -2,8 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton import com.github.insanusmokrassar.TelegramBotAPI.types.inlineKeyboardField -import kotlinx.serialization.* -import kotlinx.serialization.internal.ArrayListSerializer +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable @Serializable data class InlineKeyboardMarkup( diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt index d8ca32c75d..4538d225ad 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt @@ -1,7 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions import com.github.insanusmokrassar.TelegramBotAPI.types.update.* -import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter @Deprecated(