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?