From 4877b8958ecdb6b7bd23051d03deb8fb4b104f61 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 10:37:05 +0600 Subject: [PATCH 01/14] start 0.26.2 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6642967bf2..0f36474d50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,8 @@ and size of retrieved updates is equal to 100 (max count of retrieved updates) * Extensions `getUpdates` now will receive only not nullable `limit` parameter +### 0.26.2 + ### 0.26.1 * `TelegramBotAPI`: diff --git a/gradle.properties b/gradle.properties index cbb89ee1c5..cd735c2204 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,6 @@ uuid_version=0.1.0 ktor_version=1.3.2 library_group=com.github.insanusmokrassar -library_version=0.26.1 +library_version=0.26.2 gradle_bintray_plugin_version=1.8.4 From 6c39dc4d064be365cac53b16ff28026225fe5c3f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 10:44:44 +0600 Subject: [PATCH 02/14] new startGettingOfUpdates extension --- CHANGELOG.md | 3 +++ .../extensions/api/updates/UpdatesPolling.kt | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f36474d50..3cd1e87da4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,9 @@ ### 0.26.2 +* `TelegramBotAPI-extensions-api`: + * `startGettingOfUpdates` extension which not require filter (but return a new one) was added + ### 0.26.1 * `TelegramBotAPI`: diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt index fe2fba5f2e..bdc3e64455 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt @@ -10,6 +10,7 @@ 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 com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature import io.ktor.client.features.HttpRequestTimeoutException import kotlinx.coroutines.* @@ -63,6 +64,21 @@ fun RequestsExecutor.startGettingOfUpdates( } } +/** + * This method will create a new one [FlowsUpdatesFilter]. This method is unsafe due to the fact that it will start + * getting updates IMMEDIATELY. That means that your bot will be able to skip some of them until you will call + * [kotlinx.coroutines.flow.Flow.collect] on one of [FlowsUpdatesFilter] flows + */ +@PreviewFeature +fun RequestsExecutor.startGettingOfUpdates( + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + flowsUpdatesFilterUpdatesKeeperCount: Int = 64 +): FlowsUpdatesFilter = FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply { + startGettingOfUpdates(timeoutSeconds, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver) +} + fun RequestsExecutor.startGettingOfUpdates( updatesFilter: UpdatesFilter, timeoutSeconds: Seconds = 30, From 10a1d1cb38c9ecdc3796f6f9d2f616a9f0fd7f63 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 11:05:48 +0600 Subject: [PATCH 03/14] add TelegramBotAPI-extensions-utils --- TelegramBotAPI-extensions-utils/build.gradle | 48 ++++++++++++++++ .../maven.publish.gradle | 57 +++++++++++++++++++ .../mpp_publish_template.json | 1 + .../publish.gradle | 55 ++++++++++++++++++ settings.gradle | 1 + 5 files changed, 162 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/build.gradle create mode 100644 TelegramBotAPI-extensions-utils/maven.publish.gradle create mode 100644 TelegramBotAPI-extensions-utils/mpp_publish_template.json create mode 100644 TelegramBotAPI-extensions-utils/publish.gradle diff --git a/TelegramBotAPI-extensions-utils/build.gradle b/TelegramBotAPI-extensions-utils/build.gradle new file mode 100644 index 0000000000..0cbe7c0136 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/build.gradle @@ -0,0 +1,48 @@ +buildscript { + repositories { + mavenLocal() + jcenter() + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" + classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version" + } +} + +plugins { + id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" + id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" +} + +project.version = "$library_version" +project.group = "$library_group" + +apply from: "publish.gradle" + +repositories { + mavenLocal() + jcenter() + mavenCentral() + maven { url "https://kotlin.bintray.com/kotlinx" } +} + +kotlin { + jvm() + js() + + sourceSets { + commonMain { + dependencies { + implementation kotlin('stdlib') + if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") { + api "${project.group}:TelegramBotAPI:$library_version" + } else { + implementation project(":TelegramBotAPI") + } + } + } + } +} diff --git a/TelegramBotAPI-extensions-utils/maven.publish.gradle b/TelegramBotAPI-extensions-utils/maven.publish.gradle new file mode 100644 index 0000000000..1f2fd3ed2a --- /dev/null +++ b/TelegramBotAPI-extensions-utils/maven.publish.gradle @@ -0,0 +1,57 @@ +apply plugin: 'maven-publish' + +task javadocsJar(type: Jar) { + classifier = 'javadoc' +} + +afterEvaluate { + project.publishing.publications.all { + // rename artifacts + groupId "${project.group}" + if (it.name.contains('kotlinMultiplatform')) { + artifactId = "${project.name}" + } else { + artifactId = "${project.name}-$name" + } + } +} + +publishing { + publications.all { + artifact javadocsJar + + pom.withXml { + asNode().children().last() + { + resolveStrategy = Closure.DELEGATE_FIRST + + description "Util extensions for more useful work with updates and other things" + name "Telegram Bot API Utility Extensions" + url "https://insanusmokrassar.github.io/TelegramBotAPI" + + scm { + developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/TelegramBotAPI.git[push=]https://github.com/insanusmokrassar/TelegramBotAPI.git" + url "https://github.com/insanusmokrassar/TelegramBotAPI.git" + } + + developers { + + developer { + id "InsanusMokrassar" + name "Ovsiannikov Aleksei" + email "ovsyannikov.alexey95@gmail.com" + } + + } + + licenses { + + license { + name "Apache Software License 2.0" + url "https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE" + } + + } + } + } + } +} \ No newline at end of file diff --git a/TelegramBotAPI-extensions-utils/mpp_publish_template.json b/TelegramBotAPI-extensions-utils/mpp_publish_template.json new file mode 100644 index 0000000000..d5c147454d --- /dev/null +++ b/TelegramBotAPI-extensions-utils/mpp_publish_template.json @@ -0,0 +1 @@ +{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API Utility Extensions","description":"Util extensions for more useful work with updates and other things","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file diff --git a/TelegramBotAPI-extensions-utils/publish.gradle b/TelegramBotAPI-extensions-utils/publish.gradle new file mode 100644 index 0000000000..2fd72b3b1a --- /dev/null +++ b/TelegramBotAPI-extensions-utils/publish.gradle @@ -0,0 +1,55 @@ +apply plugin: 'com.jfrog.bintray' + +apply from: "maven.publish.gradle" + +bintray { + user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER') + key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY') + filesSpec { + from "${buildDir}/publications/" + eachFile { + String directorySubname = it.getFile().parentFile.name + if (it.getName() == "module.json") { + if (directorySubname == "kotlinMultiplatform") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.module") + } else { + it.setPath("${project.name}-${directorySubname}/${project.version}/${project.name}-${directorySubname}-${project.version}.module") + } + } else { + if (directorySubname == "kotlinMultiplatform" && it.getName() == "pom-default.xml") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.pom") + } else { + it.exclude() + } + } + } + into "${project.group}".replace(".", "/") + } + pkg { + repo = "StandardRepository" + name = "${project.name}" + vcsUrl = "https://github.com/InsanusMokrassar/TelegramBotAPI" + licenses = ["Apache-2.0"] + version { + name = "${project.version}" + released = new Date() + vcsTag = "${project.version}" + gpg { + sign = true + passphrase = project.hasProperty('signing.gnupg.passphrase') ? project.property('signing.gnupg.passphrase') : System.getenv('signing.gnupg.passphrase') + } + } + } +} + +bintrayUpload.doFirst { + publications = publishing.publications.collect { + if (it.name.contains('kotlinMultiplatform')) { + null + } else { + it.name + } + } - null +} + +bintrayUpload.dependsOn publishToMavenLocal \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 251b72d9fc..001fc379e6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ include ":TelegramBotAPI" include ":TelegramBotAPI-extensions-api" +include ":TelegramBotAPI-extensions-utils" From dfc1fa4d7e78035dbedae2e10554c57e65685b13 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 11:11:31 +0600 Subject: [PATCH 04/14] refill of changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cd1e87da4..207587df79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,8 @@ * `TelegramBotAPI-extensions-api`: * `startGettingOfUpdates` extension which not require filter (but return a new one) was added +* `TelegramBotAPI-extensions-utils`: + * Subproject was added ### 0.26.1 From 51174a13de466fbe18171e5d6edb14f553223d69 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 11:50:47 +0600 Subject: [PATCH 05/14] added some of extensions in TelegramBotAPI-extensions-utils --- CHANGELOG.md | 20 +++++++++++ .../utils/ContentMessageConversations.kt | 36 +++++++++++++++++++ .../utils/updates/CommandsFilters.kt | 22 ++++++++++++ .../SentMessageUpdatesConversations.kt | 18 ++++++++++ .../utils/updates/UpdatesChatFilters.kt | 25 +++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/ContentMessageConversations.kt create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 207587df79..4bbd304af5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,26 @@ * `startGettingOfUpdates` extension which not require filter (but return a new one) was added * `TelegramBotAPI-extensions-utils`: * Subproject was added + * `filterByChat` and `filterByChatId` extensions was added + * `filterExactCommands` and `filterCommandsInsideTextMessages` extensions was added + * `asContentMessages`, `asChatEvents` and `asUnknownMessages` extensions was added + * `withContentType` extension was added + * `onlyAnimationContentMessages` extension was added + * `onlyAudioContentMessages` extension was added + * `onlyContactContentMessages` extension was added + * `onlyDiceContentMessages` extension was added + * `onlyDocumentContentMessages` extension was added + * `onlyGameContentMessages` extension was added + * `onlyInvoiceContentMessages` extension was added + * `onlyLocationContentMessages` extension was added + * `onlyPhotoContentMessages` extension was added + * `onlyPollContentMessages` extension was added + * `onlyStickerContentMessages` extension was added + * `onlyTextContentMessages` extension was added + * `onlyVenueContentMessages` extension was added + * `onlyVideoContentMessages` extension was added + * `onlyVideoNoteContentMessages` extension was added + * `onlyVoiceContentMessages` extension was added ### 0.26.1 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/ContentMessageConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/ContentMessageConversations.kt new file mode 100644 index 0000000000..d4752d67e2 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/ContentMessageConversations.kt @@ -0,0 +1,36 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.payments.InvoiceContent +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.mapNotNull +import kotlin.reflect.KClass + +fun Flow>.withContentType(contentType: KClass) = mapNotNull { + if (contentType.isInstance(it.content)) { + @Suppress("UNCHECKED_CAST") + it as ContentMessage + } else { + null + } +} + +fun Flow>.onlyAnimationContentMessages() = withContentType(AnimationContent::class) +fun Flow>.onlyAudioContentMessages() = withContentType(AudioContent::class) +fun Flow>.onlyContactContentMessages() = withContentType(ContactContent::class) +fun Flow>.onlyDiceContentMessages() = withContentType(DiceContent::class) +fun Flow>.onlyDocumentContentMessages() = withContentType(DocumentContent::class) +fun Flow>.onlyGameContentMessages() = withContentType(GameContent::class) +fun Flow>.onlyInvoiceContentMessages() = withContentType(InvoiceContent::class) +fun Flow>.onlyLocationContentMessages() = withContentType(LocationContent::class) +fun Flow>.onlyPhotoContentMessages() = withContentType(PhotoContent::class) +fun Flow>.onlyPollContentMessages() = withContentType(PollContent::class) +fun Flow>.onlyStickerContentMessages() = withContentType(StickerContent::class) +fun Flow>.onlyTextContentMessages() = withContentType(TextContent::class) +fun Flow>.onlyVenueContentMessages() = withContentType(VenueContent::class) +fun Flow>.onlyVideoContentMessages() = withContentType(VideoContent::class) +fun Flow>.onlyVideoNoteContentMessages() = withContentType(VideoNoteContent::class) +fun Flow>.onlyVoiceContentMessages() = withContentType(VoiceContent::class) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt new file mode 100644 index 0000000000..5d1fad103e --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt @@ -0,0 +1,22 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.onlyTextContentMessages +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filter + +fun Flow.filterExactCommands( + commandRegex: Regex +) = asContentMessages().onlyTextContentMessages().filter { contentMessage -> + (contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true +} + +fun Flow.filterCommandsInsideTextMessages( + commandRegex: Regex +) = asContentMessages().onlyTextContentMessages().filter { contentMessage -> + contentMessage.content.fullEntitiesList().any { + (it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true + } +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt new file mode 100644 index 0000000000..8415123400 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt @@ -0,0 +1,18 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.mapNotNull + +fun Flow.asContentMessages() = mapNotNull { + it.data as? ContentMessage<*> +} + +fun Flow.asChatEvents() = mapNotNull { + it.data as? ChatEventMessage +} + +fun Flow.asUnknownMessages() = mapNotNull { + it.data as? UnknownMessageType +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt new file mode 100644 index 0000000000..146bb2e63a --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt @@ -0,0 +1,25 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat +import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filter + +fun Flow.filterByChatId(chatId: ChatId): Flow = filter { + it.data.chat.id == chatId +} +fun Flow.filterByChat(chat: Chat): Flow = filterByChatId(chat.id) + + +fun Flow.filterByChatId(chatId: ChatId): Flow = filter { + it.data.first().chat.id == chatId +} +fun Flow.filterByChatId(chat: Chat): Flow = filterByChatId(chat.id) + + +fun Flow.filterByChatId(chatId: ChatId): Flow = filter { + it.data.chat.id == chatId +} +fun Flow.filterByChatId(chat: Chat): Flow = filterByChatId(chat.id) From bcf2325be8e7683fbd21bc9b89c33a0fcfe0ad39 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 13:19:19 +0600 Subject: [PATCH 06/14] make new startGettingOfUpdates more useful --- .../extensions/api/updates/UpdatesPolling.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt index bdc3e64455..d186de7222 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt @@ -65,17 +65,20 @@ fun RequestsExecutor.startGettingOfUpdates( } /** - * This method will create a new one [FlowsUpdatesFilter]. This method is unsafe due to the fact that it will start + * This method will create a new one [FlowsUpdatesFilter]. This method could be unsafe due to the fact that it will start * getting updates IMMEDIATELY. That means that your bot will be able to skip some of them until you will call - * [kotlinx.coroutines.flow.Flow.collect] on one of [FlowsUpdatesFilter] flows + * [kotlinx.coroutines.flow.Flow.collect] on one of [FlowsUpdatesFilter] flows. To avoid it, you can pass + * [flowUpdatesPreset] lambda - it will be called BEFORE starting updates getting */ @PreviewFeature fun RequestsExecutor.startGettingOfUpdates( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), exceptionsHandler: (suspend (Exception) -> Unit)? = null, - flowsUpdatesFilterUpdatesKeeperCount: Int = 64 + flowsUpdatesFilterUpdatesKeeperCount: Int = 64, + flowUpdatesPreset: FlowsUpdatesFilter.() -> Unit = {} ): FlowsUpdatesFilter = FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply { + flowUpdatesPreset() startGettingOfUpdates(timeoutSeconds, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver) } From 0db85232d379d6dc025c4521c5ce87efdf67debd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 13:20:48 +0600 Subject: [PATCH 07/14] fixes in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bbd304af5..2e9c979ceb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,7 +41,7 @@ ### 0.26.2 * `TelegramBotAPI-extensions-api`: - * `startGettingOfUpdates` extension which not require filter (but return a new one) was added + * `startGettingOfUpdates` extension which do not require filter (but return a new one) was added * `TelegramBotAPI-extensions-utils`: * Subproject was added * `filterByChat` and `filterByChatId` extensions was added From d8492ae1687eda764dd1b66107fd3187900f661c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 14:02:55 +0600 Subject: [PATCH 08/14] fixes, new readme --- CHANGELOG.md | 2 +- TelegramBotAPI-extensions-api/README.md | 20 ++++- TelegramBotAPI-extensions-utils/README.md | 81 +++++++++++++++++++ .../utils/CallbackQueryConversations.kt | 12 +++ .../CallbackQueryUpdatesConversations.kt | 16 ++++ .../utils/updates/CommandsFilters.kt | 4 +- .../SentMessageUpdatesConversations.kt | 6 +- TelegramBotAPI/README.md | 2 - 8 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 TelegramBotAPI-extensions-utils/README.md create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CallbackQueryConversations.kt create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CallbackQueryUpdatesConversations.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e9c979ceb..4d7cdffd7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,7 +46,7 @@ * Subproject was added * `filterByChat` and `filterByChatId` extensions was added * `filterExactCommands` and `filterCommandsInsideTextMessages` extensions was added - * `asContentMessages`, `asChatEvents` and `asUnknownMessages` extensions was added + * `asContentMessagesFlow`, `asChatEventsFlow` and `asUnknownMessagesFlow` extensions was added * `withContentType` extension was added * `onlyAnimationContentMessages` extension was added * `onlyAudioContentMessages` extension was added diff --git a/TelegramBotAPI-extensions-api/README.md b/TelegramBotAPI-extensions-api/README.md index 275b475f9c..05ef1d3fda 100644 --- a/TelegramBotAPI-extensions-api/README.md +++ b/TelegramBotAPI-extensions-api/README.md @@ -1,9 +1,7 @@ # TelegramBotAPI extensions -[![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/images/download.svg) ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api) -[![Build Status](https://jenkins.insanusmokrassar.com/buildStatus/icon?job=TelegramBotAPI-extensions-api_master__publishing)](https://jenkins.insanusmokrassar.com/job/TelegramBotAPI-extensions-api_master__publishing/) ## What is it? @@ -99,3 +97,21 @@ filter.messageFlow.mapNotNull { CoroutineScope(Dispatchers.Default) ) ``` + +### Alternative way + +There is an alternative way to get updates. In fact it is almost the same, but could be more useful for some cases: + +```kotlin +val filter = bot.startGettingOfUpdates( + scope = CoroutineScope(Dispatchers.Default) +) { // Here as reveiver will be FlowsUpdatesFilter + messageFlow.mapNotNull { + it.data as? ContentMessage<*> + }.onEach { + println(it) + }.launchIn( + CoroutineScope(Dispatchers.Default) + ) +} +``` diff --git a/TelegramBotAPI-extensions-utils/README.md b/TelegramBotAPI-extensions-utils/README.md new file mode 100644 index 0000000000..5ebc8c9488 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/README.md @@ -0,0 +1,81 @@ +# TelegramBotAPI Util Extensions + +[![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/images/download.svg) ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils) + +## What is it? + +It is wrapper library for [TelegramBotAPI](../TelegramBotAPI/README.md). Currently, this library contains some usefull filters for commands, updates types and different others. + +## How to implement library? + +Common ways to implement this library are presented here. In some cases it will require additional steps +like inserting of additional libraries (like `kotlin stdlib`). In the examples will be used variable +`telegrambotapi-extensions-utils_version`, which must be set up by developer. Available versions are presented on +[bintray](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils), next version is last published: + +[![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/images/download.svg) ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion) + +### Maven + +Dependency config presented here: + +```xml + + com.github.insanusmokrassar + TelegramBotAPI-extensions-utils + ${telegrambotapi-extensions-utils_version} + +``` + +### Gradle + +To use last versions you will need to add one line in repositories block of your `build.gradle`: + +`jcenter()` or `mavenCentral()` + +And add next line to your dependencies block: + +```groovy +implementation "com.github.insanusmokrassar:TelegramBotAPI-extensions-utils:$telegrambotapi-extensions-utils_version" +``` + +or for old gradle: + +```groovy +compile "com.github.insanusmokrassar:TelegramBotAPI-extensions-utils:$telegrambotapi-extensions-utils_version" +``` + +## How to use? + +Here will be presented several examples of usage. In all cases it is expected that you have created your bot and filter: + +```kotlin +val bot: RequestsExecutor = KtorRequestsExecutor( + TelegramAPIUrlsKeeper(BOT_TOKEN) +) +val filter = FlowsUpdatesFilter(64) +``` + +Alternative way to use the things below: + +```kotlin +val filter = bot.startGettingUpdates( + scope = CoroutineScope(Dispatchers.Default) +) { + // place code from examples here with replacing of `filter` by `this` +} +``` + +### Getting of only text incoming messages + +```kotlin +filter.asContentMessagesFlow().onlyTextContentMessages().onEach { + println(it.content) + println(it.fullEntitiesList()) +}.launchIn( + CoroutineScope(Dispatchers.Default) +) +``` + +As a result, each received message which will be just text message will be printed out with full list of its internal entities \ No newline at end of file diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CallbackQueryConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CallbackQueryConversations.kt new file mode 100644 index 0000000000..584ce57d79 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CallbackQueryConversations.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.* +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.mapNotNull + +fun Flow.onlyMessageDataCallbackQueries() = mapNotNull { + it as? MessageDataCallbackQuery +} +fun Flow.onlyInlineMessageIdDataCallbackQueries() = mapNotNull { + it as? InlineMessageIdDataCallbackQuery +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CallbackQueryUpdatesConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CallbackQueryUpdatesConversations.kt new file mode 100644 index 0000000000..4aff11eefa --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CallbackQueryUpdatesConversations.kt @@ -0,0 +1,16 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.CallbackQueryUpdate +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.mapNotNull + +fun Flow.asDataCallbackQueryFlow() = mapNotNull { + it.data as? DataCallbackQuery +} +fun Flow.asGameShortNameCallbackQueryFlow() = mapNotNull { + it.data as? GameShortNameCallbackQuery +} +fun Flow.asUnknownCallbackQueryFlow() = mapNotNull { + it.data as? UnknownCallbackQueryType +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt index 5d1fad103e..08d6fa9fc2 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt @@ -9,13 +9,13 @@ import kotlinx.coroutines.flow.filter fun Flow.filterExactCommands( commandRegex: Regex -) = asContentMessages().onlyTextContentMessages().filter { contentMessage -> +) = asContentMessagesFlow().onlyTextContentMessages().filter { contentMessage -> (contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true } fun Flow.filterCommandsInsideTextMessages( commandRegex: Regex -) = asContentMessages().onlyTextContentMessages().filter { contentMessage -> +) = asContentMessagesFlow().onlyTextContentMessages().filter { contentMessage -> contentMessage.content.fullEntitiesList().any { (it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true } diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt index 8415123400..78d52d8274 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt @@ -5,14 +5,14 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSen import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.mapNotNull -fun Flow.asContentMessages() = mapNotNull { +fun Flow.asContentMessagesFlow() = mapNotNull { it.data as? ContentMessage<*> } -fun Flow.asChatEvents() = mapNotNull { +fun Flow.asChatEventsFlow() = mapNotNull { it.data as? ChatEventMessage } -fun Flow.asUnknownMessages() = mapNotNull { +fun Flow.asUnknownMessagesFlow() = mapNotNull { it.data as? UnknownMessageType } diff --git a/TelegramBotAPI/README.md b/TelegramBotAPI/README.md index d255e585bf..0667e85fba 100644 --- a/TelegramBotAPI/README.md +++ b/TelegramBotAPI/README.md @@ -1,9 +1,7 @@ # TelegramBotAPI -[![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI/images/download.svg) ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI) -[![Build Status](https://jenkins.insanusmokrassar.com/buildStatus/icon?job=TelegramBotAPI_master__publishing)](https://jenkins.insanusmokrassar.com/job/TelegramBotAPI_master__publishing/) ## What is it? From ba76eaeb905f5e058dc1a6747568d88db22fdcbd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 14:34:55 +0600 Subject: [PATCH 09/14] update readmes --- README.md | 24 +++++++++++++---------- TelegramBotAPI-extensions-utils/README.md | 4 ++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f9b84a5750..8fbaa6d699 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,31 @@ # TelegramBotAPI -| Common info | [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Chat in Telegram](badges/chat.svg)](https://t.me/InMoTelegramBotAPI) [![Build Status](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI.svg?branch=master)](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI) | -| -----------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| TelegramBotAPI status | [![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI/images/download.svg)](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI) | -| TelegramBotAPI Extensions status | [![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/images/download.svg)](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api) | - +| Common info | [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Chat in Telegram](badges/chat.svg)](https://t.me/InMoTelegramBotAPI) [![Build Status](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI.svg?branch=master)](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI) | +| -------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| TelegramBotAPI status | [![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI/images/download.svg)](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI) | +| TelegramBotAPI Extensions status | [![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/images/download.svg)](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api) | +| TelegramBotAPI Util Extensions status | [![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/images/download.svg)](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils) | It is a complex of libraries for working with `TelegramBotAPI` in type-safe and strict way as much as it possible. In the list of this complex currently next projects: * [TelegramBotAPI](TelegramBotAPI/README.md) - core of library. In fact it is independent library and can be used alone -without any additional library + without any additional library * [TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) - contains extensions (mostly for -`RequestsExecutor`), which allows to use the core library in more pleasant way + `RequestsExecutor`), which allows to use the core library in more pleasant way +* [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) - contains extensions (mostly for + `RequestsExecutor`), which allows to use the core library in more pleasant way Most part of some specific solves or unuseful moments are describing by official [Telegram Bot API](https://core.telegram.org/bots/api). ## Ok, where should I start? -Firstly, look at the [TelegramBotAPI](TelegramBotAPI/README.md). Here you can find all information about currently -covered Telegram Bot API and other things. After this you can look at the -[TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md). +In most cases, the most simple way will be to implement +[TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) and +[TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) for the reason that they contains more +simple tools. If you want to dive deeper in the core of library or develop something for it - welcome to +[TelegramBotAPI](TelegramBotAPI/README.md). Anyway, all libraries are very typical inside of them. For example, any request in TelegramBotAPI look like `requestsExecutor.execute(SomeRequest())`. diff --git a/TelegramBotAPI-extensions-utils/README.md b/TelegramBotAPI-extensions-utils/README.md index 5ebc8c9488..a1dc75fdb6 100644 --- a/TelegramBotAPI-extensions-utils/README.md +++ b/TelegramBotAPI-extensions-utils/README.md @@ -1,7 +1,7 @@ # TelegramBotAPI Util Extensions -[![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/images/download.svg) ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion) -[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils) +[![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/images/download.svg) ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils) ## What is it? From bd87938e9c4608b4fffe8a0c006baab60af9da6c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 14:51:26 +0600 Subject: [PATCH 10/14] fixes in UpdatesChatFilters --- CHANGELOG.md | 4 +++- .../extensions/utils/updates/UpdatesChatFilters.kt | 14 ++++---------- .../update/MediaGroupUpdates/MediaGroupUpdate.kt | 5 ++--- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d7cdffd7e..4ea9c3df80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,11 +40,13 @@ ### 0.26.2 +* `TelegramBotAPI`: + * Now `EditMediaGroupUpdate` also extends `BaseEditMessageUpdate` * `TelegramBotAPI-extensions-api`: * `startGettingOfUpdates` extension which do not require filter (but return a new one) was added * `TelegramBotAPI-extensions-utils`: * Subproject was added - * `filterByChat` and `filterByChatId` extensions was added + * `filterBaseMessageUpdates`, `filterSentMediaGroupUpdates` and `filterEditMediaGroupUpdates` extensions was added * `filterExactCommands` and `filterCommandsInsideTextMessages` extensions was added * `asContentMessagesFlow`, `asChatEventsFlow` and `asUnknownMessagesFlow` extensions was added * `withContentType` extension was added diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt index 146bb2e63a..d4a95cd1c3 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt @@ -7,19 +7,13 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMes import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.filter -fun Flow.filterByChatId(chatId: ChatId): Flow = filter { +fun Flow.filterBaseMessageUpdates(chatId: ChatId): Flow = filter { it.data.chat.id == chatId } -fun Flow.filterByChat(chat: Chat): Flow = filterByChatId(chat.id) +fun Flow.filterBaseMessageUpdates(chat: Chat): Flow = filterBaseMessageUpdates(chat.id) -fun Flow.filterByChatId(chatId: ChatId): Flow = filter { +fun Flow.filterSentMediaGroupUpdates(chatId: ChatId): Flow = filter { it.data.first().chat.id == chatId } -fun Flow.filterByChatId(chat: Chat): Flow = filterByChatId(chat.id) - - -fun Flow.filterByChatId(chatId: ChatId): Flow = filter { - it.data.chat.id == chatId -} -fun Flow.filterByChatId(chat: Chat): Flow = filterByChatId(chat.id) +fun Flow.filterSentMediaGroupUpdates(chat: Chat): Flow = filterSentMediaGroupUpdates(chat.id) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt index c61ae5b674..5724d2d9b6 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt @@ -1,8 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates 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.types.update.abstracts.* interface MediaGroupUpdate : Update @@ -11,7 +10,7 @@ interface SentMediaGroupUpdate: MediaGroupUpdate { val origins: List } -interface EditMediaGroupUpdate : MediaGroupUpdate { +interface EditMediaGroupUpdate : BaseEditMessageUpdate, MediaGroupUpdate { override val data: MediaGroupMessage val origin: BaseMessageUpdate } From cf9f27065155a6c449b13e6fd60ea0b422edf871 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 14:56:56 +0600 Subject: [PATCH 11/14] rename new getting updates extension to avoid JVM signature collisions --- CHANGELOG.md | 2 +- .../TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ea9c3df80..67ed0b4ffd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,7 @@ * `TelegramBotAPI`: * Now `EditMediaGroupUpdate` also extends `BaseEditMessageUpdate` * `TelegramBotAPI-extensions-api`: - * `startGettingOfUpdates` extension which do not require filter (but return a new one) was added + * `startGettingFlowsUpdates` extension which do not require filter (but return a new one) was added * `TelegramBotAPI-extensions-utils`: * Subproject was added * `filterBaseMessageUpdates`, `filterSentMediaGroupUpdates` and `filterEditMediaGroupUpdates` extensions was added diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt index d186de7222..d74b77c559 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt @@ -71,7 +71,7 @@ fun RequestsExecutor.startGettingOfUpdates( * [flowUpdatesPreset] lambda - it will be called BEFORE starting updates getting */ @PreviewFeature -fun RequestsExecutor.startGettingOfUpdates( +fun RequestsExecutor.startGettingFlowsUpdates( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), exceptionsHandler: (suspend (Exception) -> Unit)? = null, From 7183634fd6374d214a8f4eab400d92d09db751a4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 15:17:13 +0600 Subject: [PATCH 12/14] experimentally make source string available inside of text sources --- CHANGELOG.md | 1 + .../TelegramBotAPI/CommonAbstracts/TextSource.kt | 1 + .../types/MessageEntity/textsources/BoldTextSource.kt | 2 +- .../types/MessageEntity/textsources/BotCommandTextSource.kt | 2 +- .../types/MessageEntity/textsources/CashTagTextSource.kt | 2 +- .../types/MessageEntity/textsources/CodeTextSource.kt | 2 +- .../types/MessageEntity/textsources/EMailTextSource.kt | 2 +- .../types/MessageEntity/textsources/HashTagTextSource.kt | 2 +- .../types/MessageEntity/textsources/ItalicTextSource.kt | 2 +- .../types/MessageEntity/textsources/MentionTextSource.kt | 2 +- .../types/MessageEntity/textsources/PhoneNumberTextSource.kt | 2 +- .../types/MessageEntity/textsources/PreTextSource.kt | 2 +- .../types/MessageEntity/textsources/RegularTextSource.kt | 2 +- .../types/MessageEntity/textsources/StrikethroughTextSource.kt | 2 +- .../types/MessageEntity/textsources/TextLinkTextSource.kt | 2 +- .../types/MessageEntity/textsources/TextMentionTextSource.kt | 2 +- .../types/MessageEntity/textsources/URLTextSource.kt | 2 +- .../types/MessageEntity/textsources/UnderlineTextSource.kt | 2 +- 18 files changed, 18 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67ed0b4ffd..7380bee314 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ * `TelegramBotAPI`: * Now `EditMediaGroupUpdate` also extends `BaseEditMessageUpdate` + * **EXPERIMENTALLY** Now all `TextSource` realisations will contain `source` field as a property inside of them * `TelegramBotAPI-extensions-api`: * `startGettingFlowsUpdates` extension which do not require filter (but return a new one) was added * `TelegramBotAPI-extensions-utils`: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt index be70f1032e..2a084edbe3 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt @@ -4,6 +4,7 @@ interface TextSource { val asMarkdownSource: String val asMarkdownV2Source: String val asHtmlSource: String + val source: String } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt index a491b5953f..7d3f83497a 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class BoldTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { source.fullListOfSubSource(textParts) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt index a8dfaa4c52..b0fa9a1fa6 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt @@ -7,7 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.* private val commandRegex = Regex("[/!][^@\\s]*") class BotCommandTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { val command: String by lazy { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CashTagTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CashTagTextSource.kt index f0fb0d8a44..0186c60e9d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CashTagTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CashTagTextSource.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class CashTagTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { source.fullListOfSubSource(textParts) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt index 5b2359a458..66616d090d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt @@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.* class CodeTextSource( - source: String + override val source: String ) : TextSource { override val asMarkdownSource: String by lazy { source.codeMarkdown() } override val asMarkdownV2Source: String by lazy { source.codeMarkdownV2() } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt index f87d899953..c6704af3ed 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class EMailTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { source.fullListOfSubSource(textParts) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt index 1daabab4d7..ed3887ea59 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt @@ -12,7 +12,7 @@ private val String.withoutSharp } class HashTagTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt index 5043cd19d8..49ae39363f 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class ItalicTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { source.fullListOfSubSource(textParts) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt index 398ae8c388..d8f29065fe 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt @@ -12,7 +12,7 @@ private val String.withoutCommercialAt } class MentionTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt index f83e41be12..f7f4735e6f 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class PhoneNumberTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { source.fullListOfSubSource(textParts) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt index d83896ba3f..53fbafca59 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt @@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.* class PreTextSource( - source: String, + override val source: String, val language: String? = null ) : TextSource { override val asMarkdownSource: String by lazy { source.preMarkdown(language) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt index dca8f5aedc..a3fefb0de3 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt @@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.* class RegularTextSource( - source: String + override val source: String ) : TextSource { override val asMarkdownSource: String by lazy { source.regularMarkdown() } override val asMarkdownV2Source: String by lazy { source.regularMarkdownV2() } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt index 17c79618bf..4fb9156f74 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class StrikethroughTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { source.fullListOfSubSource(textParts) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt index 73d3ff8803..a4ba378bad 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt @@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.* class TextLinkTextSource( - source: String, + override val source: String, url: String ) : TextSource { override val asMarkdownSource: String by lazy { source.linkMarkdown(url) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt index b335806c6f..061b5f1fb8 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt @@ -6,7 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateCh import com.github.insanusmokrassar.TelegramBotAPI.utils.* class TextMentionTextSource( - source: String, + override val source: String, privateChat: PrivateChat, textParts: List ) : MultilevelTextSource { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt index bd572b097b..dfa2cfeca0 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt @@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.* class URLTextSource( - source: String + override val source: String ) : TextSource { override val asMarkdownSource: String by lazy { source.linkMarkdown(source) } override val asMarkdownV2Source: String by lazy { source.linkMarkdownV2(source) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt index 6cc142ec25..13f4c87395 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class UnderlineTextSource( - source: String, + override val source: String, textParts: List ) : MultilevelTextSource { override val textParts: List by lazy { source.fullListOfSubSource(textParts) } From 08d9d183f4158f80e143391e4562b6a7ffcd5a78 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 15:23:12 +0600 Subject: [PATCH 13/14] add filterCommandsWithArgs --- CHANGELOG.md | 2 +- .../utils/updates/CommandsFilters.kt | 35 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7380bee314..aabf659514 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,7 +48,7 @@ * `TelegramBotAPI-extensions-utils`: * Subproject was added * `filterBaseMessageUpdates`, `filterSentMediaGroupUpdates` and `filterEditMediaGroupUpdates` extensions was added - * `filterExactCommands` and `filterCommandsInsideTextMessages` extensions was added + * `filterCommandsWithArgs`, `filterExactCommands` and `filterCommandsInsideTextMessages` extensions was added * `asContentMessagesFlow`, `asChatEventsFlow` and `asUnknownMessagesFlow` extensions was added * `withContentType` extension was added * `onlyAnimationContentMessages` extension was added diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt index 08d6fa9fc2..1d991b0c0d 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt @@ -1,11 +1,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.onlyTextContentMessages import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.RegularTextSource import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.* fun Flow.filterExactCommands( commandRegex: Regex @@ -20,3 +21,33 @@ fun Flow.filterCommandsInsideTextMessages( (it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true } } + +/** + * @return Result [Flow] will emit all [TextSource]s to the collector ONLY IN CASE if first [TextSource] is + * [BotCommandTextSource] and its [BotCommandTextSource.command] is [Regex.matches] to incoming [commandRegex]. Internal + * behaviour contains next rules: all incoming text sources will be passed as is, [RegularTextSource] will be divided + * by " " for several [RegularTextSource] which will contains not empty args without spaces + */ +fun Flow.filterCommandsWithArgs( + commandRegex: Regex +): Flow> = asContentMessagesFlow().onlyTextContentMessages().mapNotNull { contentMessage -> + val allEntities = contentMessage.content.fullEntitiesList() + (allEntities.firstOrNull() as? BotCommandTextSource) ?.let { + if (commandRegex.matches(it.command)) { + allEntities.flatMap { + when (it) { + is RegularTextSource -> it.source.split(" ").mapNotNull { regularTextSourcePart -> + if (regularTextSourcePart.isNotBlank()) { + RegularTextSource(regularTextSourcePart) + } else { + null + } + } + else -> listOf(it) + } + } + } else { + null + } + } +} From 71b5e33dbcaf5cb55aeeace764a14ac41e8beb21 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Apr 2020 15:28:03 +0600 Subject: [PATCH 14/14] update common README --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8fbaa6d699..367836fe4b 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ the list of this complex currently next projects: without any additional library * [TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) - contains extensions (mostly for `RequestsExecutor`), which allows to use the core library in more pleasant way -* [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) - contains extensions (mostly for - `RequestsExecutor`), which allows to use the core library in more pleasant way +* [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) - contains extensions for more comfortable +work with commands, updates and other different things Most part of some specific solves or unuseful moments are describing by official [Telegram Bot API](https://core.telegram.org/bots/api). @@ -27,5 +27,9 @@ In most cases, the most simple way will be to implement simple tools. If you want to dive deeper in the core of library or develop something for it - welcome to [TelegramBotAPI](TelegramBotAPI/README.md). -Anyway, all libraries are very typical inside of them. For example, any request in TelegramBotAPI look like -`requestsExecutor.execute(SomeRequest())`. +Anyway, all libraries are very typical inside of them. Examples: + +* In `TelegramBotAPI` common request look like `requestsExecutor.execute(SomeRequest())` +* `TelegramBotAPI-extensions-api` typical syntax look like `requestsExecutor.someRequest()` (in most cases it would be +better to understand to use `bot` name instead of `requestsExecutor`) +* `TelegramBotAPI-extensions-utils` will look like `filter.filterBaseMessageUpdates(chatId).filterExactCommands(Regex("^.*$"))...`