From fd6e4b05226956caea05a20e164d21e47e066c48 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 28 Jun 2021 11:10:09 +0600 Subject: [PATCH] final refactor, fixes and upfilling of changelog --- CHANGELOG.md | 18 +++++++++++---- .../types/commands/BotCommandScope.kt | 23 +++++++++++-------- .../updateshandlers/FlowsUpdatesFilter.kt | 2 +- .../behaviour_builder/utils/WrapWithLaunch.kt | 21 ----------------- .../MessageMarkerFactories.kt | 10 +++++++- .../utils/formatting/EntitiesBuilder.kt | 2 +- 6 files changed, 39 insertions(+), 37 deletions(-) delete mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/WrapWithLaunch.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 35d813280c..2042b9ccb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,15 +6,25 @@ * `Version`: * `Kotlin`: `1.5.10` -> `1.5.20` * `MicroUtils`: `0.5.6` -> `0.5.15` +* `Core`: + * New interface `MyCommandsRequest` (also see `Bot API 5.3` below) * `Behaviour Builder`: + * ❗️ All triggers (`on*` extensions) have been modified to work in parallel by some marker by default (new parameter + `markerFactory`, in most cases will work async for different chats) * New extensions `telegramBotWithBehaviour` - * All triggers (`on*` extensions) have been modified to work in parallel by default (new parameter - `performInParallel`, by default `true`) * All behaviour builder extensions got new parameter `defaultExceptionsHandler` * Class `BehaviourContext` was rewritten as an interface with default realization `DefaultBehaviourContext` and - factory `BehaviourContext(TelegramBot, CoroutineScope, FlowsUpdatesFilter)` + factory `BehaviourContext(TelegramBot, CoroutineScope, FlowsUpdatesFilter)` + * Extension `buildBehaviour` (and all related extensions/functions) for opportunity to pass + `defaultExceptionsHandler` + * Trigger `onContentMessage` now may include media groups * `API`: - * All `reply` and subsequent extension have been replaced in send package + * All `reply` and subsequent extensions have been replaced in send package +* `Utils`: + * With class casts like `as*` and `require*` now you may use `when*` with parameter callback + * Methods of `EntitiesBuilder` now will return builder itself, so you may create sequences like + `buildEntities { bold("Hello,") + italic(" world") }` directly in `buildEntities` body + * New extension `TelegramBot#longPollingFlow` has been added with returning value `Flow` with updates * `Bot API 5.3`: * Add type `BotCommandScope`, its serializer `BotCommandScopeSerializer` and all its children * New request `DeleteMyCommands` and updates in `GetMyCommands` and `SetMyCommands` diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt index dcb606e335..fe2f02f8e1 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt @@ -18,14 +18,14 @@ private class SurrogateBotCommandScope( val userId: UserId? = null ) { fun asBotCommandScope() = when (type) { - "default" -> BotCommandScopeDefault - "all_private_chats" -> BotCommandScopeAllPrivateChats - "all_group_chats" -> BotCommandScopeAllGroupChats - "all_chat_administrators" -> BotCommandScopeAllChatAdministrators - "chat_administrators" -> BotCommandScopeChatAdministrators( + BotCommandScopeDefault.type -> BotCommandScopeDefault + BotCommandScopeAllPrivateChats.type -> BotCommandScopeAllPrivateChats + BotCommandScopeAllGroupChats.type -> BotCommandScopeAllGroupChats + BotCommandScopeAllChatAdministrators.type -> BotCommandScopeAllChatAdministrators + BotCommandScopeChatAdministrators.type -> BotCommandScopeChatAdministrators( chatId ?: error("chat_administrators type must have $chatIdField field, but have no") ) - "chat_member" -> BotCommandScopeChatMember( + BotCommandScopeChatMember.type -> BotCommandScopeChatMember( chatId ?: error("chat_administrators type must have $chatIdField field, but have no"), userId ?: error("chat_administrators type must have $userIdField field, but have no") ) @@ -89,7 +89,10 @@ data class BotCommandScopeChatAdministrators( override val chatId: ChatIdentifier ) : ChatBotCommandScope { @Required - override val type: String = "chat_administrators" + override val type: String = BotCommandScopeChatAdministrators.type + companion object { + const val type = "chat_administrators" + } } @Serializable @@ -98,13 +101,15 @@ data class BotCommandScopeChatMember( val userId: UserId ) : ChatBotCommandScope { @Required - override val type: String = "chat_member" + override val type: String = BotCommandScopeChatMember.type + companion object { + const val type = "chat_member" + } } object BotCommandScopeSerializer : KSerializer { - @RiskFeature override val descriptor: SerialDescriptor = SurrogateBotCommandScope.serializer().descriptor override fun deserialize( diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt index 35e565c1d1..de735db1c4 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt @@ -42,7 +42,7 @@ fun FlowsUpdatesFilter( broadcastChannelsSize: Int = 100 ) = DefaultFlowsUpdatesFilter(broadcastChannelsSize) -@Suppress("EXPERIMENTAL_API_USAGE", "unused") +@Suppress("unused") class DefaultFlowsUpdatesFilter( broadcastChannelsSize: Int = 100, onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/WrapWithLaunch.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/WrapWithLaunch.kt deleted file mode 100644 index 3a2f27ba20..0000000000 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/WrapWithLaunch.kt +++ /dev/null @@ -1,21 +0,0 @@ -package dev.inmo.tgbotapi.extensions.behaviour_builder.utils - -import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions -import kotlinx.coroutines.CoroutineScope - -internal fun CoroutineScope.wrapWithLaunch( - block: suspend (T) -> Unit -): suspend (T) -> Unit = { - launchSafelyWithoutExceptions { - block(it) - } -} - -internal fun CoroutineScope.optionallyWrapWithLaunch( - wrap: Boolean, - block: suspend (T) -> Unit -): suspend (T) -> Unit = if (wrap) { - wrapWithLaunch(block) -} else { - block -} diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MessageMarkerFactories.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MessageMarkerFactories.kt index 1b98924dc9..1beb82c59d 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MessageMarkerFactories.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MessageMarkerFactories.kt @@ -1,7 +1,15 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories -import dev.inmo.tgbotapi.types.message.abstracts.Message +import dev.inmo.tgbotapi.types.message.abstracts.* object ByChatMessageMarkerFactory : MarkerFactory { override suspend fun invoke(data: Message) = data.chat } + +object ByUserMessageMarkerFactory : MarkerFactory { + override suspend fun invoke(data: Message) = when (data) { + is FromUserMessage -> data.user + is FromChannelGroupContentMessage<*> -> data.channel + else -> data.chat // including anonymous + } +} diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/EntitiesBuilder.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/EntitiesBuilder.kt index 1898f62f3f..24f7fd1ca7 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/EntitiesBuilder.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/EntitiesBuilder.kt @@ -52,7 +52,7 @@ class EntitiesBuilder internal constructor( operator fun plus(sources: Iterable) = addAll(sources) operator fun plus(other: EntitiesBuilder) = if (other == this) { - // do nothing; assume user + // do nothing; assume user is using something like regular("Hello, ") + bold("world") in buildEntities this } else { addAll(other.entitiesList)