mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 00:03:48 +00:00
inline queries extensions
This commit is contained in:
parent
46c33d5ec4
commit
acd9505a5d
11
CHANGELOG.md
11
CHANGELOG.md
@ -52,6 +52,17 @@
|
||||
* `TelegramBotAPI-core`:
|
||||
* All `InlineQueryResult` has changed their type of id for more obvious relation between `InlineQueryResult#id` and
|
||||
`ChosenInlineResult#resultId`: `String` -> `InlineQueryIdentifier`
|
||||
* `TelegramBotAPI-extensions-utils`:
|
||||
* Several extensions for updates flows based on `InlineQueryUpdate` has been added:
|
||||
* `Flow<InlineQueryUpdate>#onlyBaseInlineQueriesWithUpdates`
|
||||
* `Flow<InlineQueryUpdate>#onlyBaseInlineQueries`
|
||||
* `Flow<InlineQueryUpdate>#onlyLocationInlineQueriesWithUpdates`
|
||||
* `Flow<InlineQueryUpdate>#onlyLocationInlineQueries`
|
||||
* Several extensions for updates flows based on `ChosenInlineResultUpdate` has been added:
|
||||
* `Flow<ChosenInlineResultUpdate>.onlyBaseChosenInlineResultsWithUpdates`
|
||||
* `Flow<ChosenInlineResultUpdate>.onlyBaseChosenInlineResults`
|
||||
* `Flow<ChosenInlineResultUpdate>.onlyLocationChosenInlineResultsWithUpdates`
|
||||
* `Flow<ChosenInlineResultUpdate>.onlyLocationChosenInlineResults`
|
||||
|
||||
### 0.28.2
|
||||
|
||||
|
@ -0,0 +1,16 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.internal_utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
|
||||
internal inline fun <reified T : Any, UT : Update> Flow<UT>.onlySpecifiedTypeOfDataWithUpdates(): Flow<Pair<UpdateIdentifier, T>> {
|
||||
return mapNotNull {
|
||||
it.updateId to (it.data as? T ?: return@mapNotNull null)
|
||||
}
|
||||
}
|
||||
|
||||
internal inline fun <reified T : Any, UT : Update> Flow<UT>.onlySpecifiedTypeOfData(): Flow<T> {
|
||||
return mapNotNull { it as? T }
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.internal_utils.onlySpecifiedTypeOfData
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.internal_utils.onlySpecifiedTypeOfDataWithUpdates
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.ChosenInlineResult.BaseChosenInlineResult
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.ChosenInlineResult.LocationChosenInlineResult
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.ChosenInlineResultUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.InlineQueryUpdate
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* @return Mapped [Flow] with [Pair]s. [Pair.first] in this pair will be [UpdateIdentifier]. It could be useful in
|
||||
* cases you are using [InlineQueryUpdate.updateId] for some reasons. [Pair.second] will always be [BaseChosenInlineResult].
|
||||
*/
|
||||
fun Flow<ChosenInlineResultUpdate>.onlyBaseChosenInlineResultsWithUpdates(): Flow<Pair<UpdateIdentifier, BaseChosenInlineResult>> = onlySpecifiedTypeOfDataWithUpdates()
|
||||
|
||||
/**
|
||||
* @return Filter updates only with [BaseChosenInlineResult] and map it to a [Flow] with values [BaseChosenInlineResult]
|
||||
*
|
||||
* @see onlyBaseChosenInlineResultsWithUpdates
|
||||
*/
|
||||
fun Flow<ChosenInlineResultUpdate>.onlyBaseChosenInlineResults(): Flow<BaseChosenInlineResult> = onlySpecifiedTypeOfData()
|
||||
|
||||
/**
|
||||
* @return Mapped [Flow] with [Pair]s. [Pair.first] in this pair will be [UpdateIdentifier]. It could be useful in
|
||||
* cases you are using [InlineQueryUpdate.updateId] for some reasons. [Pair.second] will always be [LocationChosenInlineResult].
|
||||
*/
|
||||
fun Flow<ChosenInlineResultUpdate>.onlyLocationChosenInlineResultsWithUpdates(): Flow<Pair<UpdateIdentifier, LocationChosenInlineResult>> = onlySpecifiedTypeOfDataWithUpdates()
|
||||
|
||||
/**
|
||||
* @return Filter updates only with [LocationChosenInlineResult] and map it to a [Flow] with values [LocationChosenInlineResult]
|
||||
*
|
||||
* @see onlyLocationChosenInlineResultsWithUpdates
|
||||
*/
|
||||
fun Flow<ChosenInlineResultUpdate>.onlyLocationChosenInlineResults(): Flow<LocationChosenInlineResult> = onlySpecifiedTypeOfData()
|
@ -0,0 +1,35 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.internal_utils.onlySpecifiedTypeOfData
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.internal_utils.onlySpecifiedTypeOfDataWithUpdates
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.query.BaseInlineQuery
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.query.LocationInlineQuery
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.InlineQueryUpdate
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
/**
|
||||
* @return Mapped [Flow] with [Pair]s. [Pair.first] in this pair will be [UpdateIdentifier]. It could be useful in
|
||||
* cases you are using [InlineQueryUpdate.updateId] for some reasons. [Pair.second] will always be [BaseInlineQuery].
|
||||
*/
|
||||
fun Flow<InlineQueryUpdate>.onlyBaseInlineQueriesWithUpdates(): Flow<Pair<UpdateIdentifier, BaseInlineQuery>> = onlySpecifiedTypeOfDataWithUpdates()
|
||||
|
||||
/**
|
||||
* @return Filter updates only with [BaseInlineQuery] and map it to a [Flow] with values [BaseInlineQuery]
|
||||
*
|
||||
* @see onlyBaseInlineQueriesWithUpdates
|
||||
*/
|
||||
fun Flow<InlineQueryUpdate>.onlyBaseInlineQueries(): Flow<BaseInlineQuery> = onlySpecifiedTypeOfData()
|
||||
|
||||
/**
|
||||
* @return Mapped [Flow] with [Pair]s. [Pair.first] in this pair will be [UpdateIdentifier]. It could be useful in
|
||||
* cases you are using [InlineQueryUpdate.updateId] for some reasons. [Pair.second] will always be [LocationInlineQuery].
|
||||
*/
|
||||
fun Flow<InlineQueryUpdate>.onlyLocationInlineQueriesWithUpdates(): Flow<Pair<UpdateIdentifier, LocationInlineQuery>> = onlySpecifiedTypeOfDataWithUpdates()
|
||||
|
||||
/**
|
||||
* @return Filter updates only with [LocationInlineQuery] and map it to a [Flow] with values [LocationInlineQuery]
|
||||
*
|
||||
* @see onlyLocationInlineQueriesWithUpdates
|
||||
*/
|
||||
fun Flow<InlineQueryUpdate>.onlyLocationInlineQueries(): Flow<LocationInlineQuery> = onlySpecifiedTypeOfData()
|
Loading…
Reference in New Issue
Block a user