mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +00:00
updates extensions
This commit is contained in:
parent
ff32fd1dfc
commit
105415873d
@ -51,6 +51,15 @@
|
||||
|
||||
### 0.27.4
|
||||
|
||||
* `TelegramBotAPI-extensions-utils`:
|
||||
* Several extensions for updates was added:
|
||||
* `onlyBaseMessageUpdates`
|
||||
* `onlySentMessageUpdates`
|
||||
* `onlyEditMessageUpdates`
|
||||
* `onlyMediaGroupsUpdates`
|
||||
* `onlySentMediaGroupUpdates`
|
||||
* `onlyEditMediaGroupUpdates`
|
||||
|
||||
### 0.27.3
|
||||
|
||||
* `TelegramBotAPI`:
|
||||
|
@ -166,6 +166,32 @@ application without creating of new one server (as it is happening in `startList
|
||||
|
||||
There are several filters for flows.
|
||||
|
||||
#### Updates
|
||||
|
||||
In the next table it is supposed that you are using some `Flow` with type from `Base type of update` and apply
|
||||
extension `Extension` and will get `Flow` with type from `Result type of update` column.
|
||||
|
||||
| Base type of update | Extension | Result type of update |
|
||||
| ------------------- | --------- | --------------------- |
|
||||
| `Update` | `onlyBaseMessageUpdates` | `BaseMessageUpdate` |
|
||||
| | | |
|
||||
| `BaseMessageUpdate` | `onlySentMessageUpdates` | `BaseSentMessageUpdate` |
|
||||
| `BaseMessageUpdate` | `onlyEditMessageUpdates` | `BaseEditMessageUpdate` |
|
||||
| `BaseMessageUpdate` | `onlyMediaGroupsUpdates` | `MediaGroupUpdate` |
|
||||
| | | |
|
||||
| `MediaGroupUpdate` | `onlySentMediaGroupUpdates` | `SentMediaGroupUpdate` |
|
||||
| `MediaGroupUpdate` | `onlyEditMediaGroupUpdates` | `EditMediaGroupUpdate` |
|
||||
|
||||
All of these extensions was made for more simple work with the others:
|
||||
|
||||
```kotlin
|
||||
val flow: Flow<BaseMessageUpdate> = ...; // here we are getting flow from somewhere,
|
||||
// for example, FlowsUpdatesFilter#messageFlow
|
||||
flow.onlySentMessageUpdates().filterExactCommands(Regex("start"))
|
||||
```
|
||||
|
||||
Here we have used filter `filterExactCommands` which will pass only `ContentMessage` with only one command `start`
|
||||
|
||||
#### Sent messages
|
||||
|
||||
All sent messages can be filtered for three types:
|
||||
|
@ -0,0 +1,49 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
|
||||
fun Flow<Update>.onlyBaseMessageUpdates(): Flow<BaseMessageUpdate> = mapNotNull {
|
||||
it as? BaseMessageUpdate
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts flow to [Flow] of [BaseSentMessageUpdate]
|
||||
*/
|
||||
fun Flow<BaseMessageUpdate>.onlySentMessageUpdates(): Flow<BaseSentMessageUpdate> = mapNotNull {
|
||||
it as? BaseSentMessageUpdate
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts flow to [Flow] of [BaseSentMessageUpdate]
|
||||
*/
|
||||
fun Flow<BaseMessageUpdate>.onlyEditMessageUpdates(): Flow<BaseEditMessageUpdate> = mapNotNull {
|
||||
it as? BaseEditMessageUpdate
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts flow to [Flow] of [MediaGroupUpdate]. Please, remember that it could be either [EditMediaGroupUpdate]
|
||||
* or [SentMediaGroupUpdate]
|
||||
*
|
||||
* @see onlySentMediaGroupUpdates
|
||||
* @see onlyEditMediaGroupUpdates
|
||||
*/
|
||||
fun Flow<BaseMessageUpdate>.onlyMediaGroupsUpdates(): Flow<MediaGroupUpdate> = mapNotNull {
|
||||
it as? MediaGroupUpdate
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts flow to [Flow] of [SentMediaGroupUpdate]
|
||||
*/
|
||||
fun Flow<MediaGroupUpdate>.onlySentMediaGroupUpdates(): Flow<SentMediaGroupUpdate> = mapNotNull {
|
||||
it as? SentMediaGroupUpdate
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts flow to [Flow] of [EditMediaGroupUpdate]
|
||||
*/
|
||||
fun Flow<MediaGroupUpdate>.onlyEditMediaGroupUpdates(): Flow<EditMediaGroupUpdate> = mapNotNull {
|
||||
it as? EditMediaGroupUpdate
|
||||
}
|
Loading…
Reference in New Issue
Block a user