mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-21 15:53:47 +00:00
commit
d60ee7b8a5
25
CHANGELOG.md
25
CHANGELOG.md
@ -49,6 +49,31 @@
|
||||
* `closePollExactAfter`
|
||||
* `closePollAfter`
|
||||
|
||||
### 0.27.10
|
||||
|
||||
* `TelegramBotAPI-extensions-api`:
|
||||
* Function `telegramBot(TelegramAPIUrlsKeeper)` was added
|
||||
* `TelegramBotAPI-extensions-utils`:
|
||||
* Extension `Route#includeWebhookHandlingInRouteWithFlows` was added
|
||||
* A lot of extensions like `FlowsUpdatesFilter#textMessages` were added:
|
||||
* `FlowsUpdatesFilter#animationMessages`
|
||||
* `FlowsUpdatesFilter#audioMessages`
|
||||
* `FlowsUpdatesFilter#contactMessages`
|
||||
* `FlowsUpdatesFilter#diceMessages`
|
||||
* `FlowsUpdatesFilter#documentMessages`
|
||||
* `FlowsUpdatesFilter#gameMessages`
|
||||
* `FlowsUpdatesFilter#invoiceMessages`
|
||||
* `FlowsUpdatesFilter#locationMessages`
|
||||
* `FlowsUpdatesFilter#photoMessages`
|
||||
* `FlowsUpdatesFilter#imageMessages`
|
||||
* `FlowsUpdatesFilter#pollMessages`
|
||||
* `FlowsUpdatesFilter#stickerMessages`
|
||||
* `FlowsUpdatesFilter#textMessages`
|
||||
* `FlowsUpdatesFilter#venueMessages`
|
||||
* `FlowsUpdatesFilter#videoMessages`
|
||||
* `FlowsUpdatesFilter#videoNoteMessages`
|
||||
* `FlowsUpdatesFilter#voiceMessages`
|
||||
|
||||
### 0.27.9
|
||||
|
||||
* `Common`
|
||||
|
@ -12,7 +12,7 @@ import io.ktor.client.engine.HttpClientEngine
|
||||
*/
|
||||
fun telegramBot(
|
||||
urlsKeeper: TelegramAPIUrlsKeeper,
|
||||
client: HttpClient
|
||||
client: HttpClient = HttpClient()
|
||||
): RequestsExecutor = KtorRequestsExecutor(
|
||||
urlsKeeper,
|
||||
client
|
||||
|
@ -1,14 +1,14 @@
|
||||
# TelegramBotAPI Util Extensions
|
||||
|
||||
- [TelegramBotAPI Util Extensions](#telegrambotapi-util--extensions)
|
||||
* [What is it?](#what-is-it-)
|
||||
* [How to implement library?](#how-to-implement-library-)
|
||||
- [TelegramBotAPI Util Extensions](#telegrambotapi-util-extensions)
|
||||
* [What is it?](#what-is-it)
|
||||
* [How to implement library?](#how-to-implement-library)
|
||||
+ [Maven](#maven)
|
||||
+ [Gradle](#gradle)
|
||||
* [How to use?](#how-to-use-)
|
||||
* [How to use?](#how-to-use)
|
||||
+ [Updates](#updates)
|
||||
- [Long polling](#long-polling)
|
||||
- [WebHooks (currently JVM-only)](#webhooks--currently-jvm-only-)
|
||||
- [WebHooks (currently JVM-only)](#webhooks--currently-jvm-only)
|
||||
+ [Filters](#filters)
|
||||
- [Sent messages](#sent-messages)
|
||||
* [Common messages](#common-messages)
|
||||
@ -102,6 +102,9 @@ Anyway, in both of ways it will be useful to know that it is possible to create
|
||||
```kotlin
|
||||
val internalChannelsSizes = 128
|
||||
flowsUpdatesFilter(internalChannelsSizes/* default is 64 */) {
|
||||
textMessages().onEach {
|
||||
println("I have received text message: ${it.content}")
|
||||
}.launchIn(someCoroutineScope)
|
||||
/* ... */
|
||||
}
|
||||
```
|
||||
@ -159,6 +162,8 @@ Besides, there are two additional opportunities:
|
||||
|
||||
* Extension `Route#includeWebhookHandlingInRoute`, which allow you to include webhook processing inside your ktor
|
||||
application without creating of new one server (as it is happening in `startListenWebhooks`)
|
||||
* Also, you can use `Route#includeWebhookHandlingInRouteWithFlows` to use it like `flowUpdatesFilter` fun, but apply
|
||||
`FlowsUpdatesFilter` to the block
|
||||
* Extension `RequestsExecutor#setWebhookInfoAndStartListenWebhooks`. It is allow to set up full server (in fact, with
|
||||
`startListenWebhooks`), but also send `SetWebhook` request before and check that it was successful
|
||||
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.BroadcastChannel
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
fun <T> aggregateFlows(
|
||||
withScope: CoroutineScope,
|
||||
vararg flows: Flow<T>,
|
||||
internalBufferSize: Int = Channel.BUFFERED
|
||||
): Flow<T> {
|
||||
val bc = BroadcastChannel<T>(internalBufferSize)
|
||||
flows.forEach {
|
||||
it.onEach {
|
||||
safely { bc.send(it) }
|
||||
}.launchIn(withScope)
|
||||
}
|
||||
return bc.asFlow()
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.aggregateFlows
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.asContentMessagesFlow
|
||||
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 com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
inline fun <reified T : MessageContent> filterForContentMessage(): suspend (ContentMessage<*>) -> ContentMessage<T>? = {
|
||||
if (it.content is T) {
|
||||
it as ContentMessage<T>
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param scopeToIncludeChannels This parameter is required when you want to include [textMessages] for channels too.
|
||||
* In this case will be created new channel which will agregate messages from [FlowsUpdatesFilter.messageFlow] and
|
||||
* [FlowsUpdatesFilter.channelPostFlow]. In case it is null will be used [Flow]s mapping
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified T: MessageContent> FlowsUpdatesFilter.filterContentMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
): Flow<ContentMessage<T>> {
|
||||
val filter = filterForContentMessage<T>()
|
||||
return scopeToIncludeChannels ?.let { scope ->
|
||||
aggregateFlows(
|
||||
scope,
|
||||
messageFlow.asContentMessagesFlow().mapNotNull(filter),
|
||||
channelPostFlow.asContentMessagesFlow().mapNotNull(filter)
|
||||
)
|
||||
} ?: messageFlow.asContentMessagesFlow().mapNotNull(filter)
|
||||
}
|
||||
|
||||
fun FlowsUpdatesFilter.animationMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<AnimationContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.audioMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<AudioContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.contactMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<ContactContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.diceMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<DiceContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.documentMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<DocumentContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.gameMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<GameContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.invoiceMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<InvoiceContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.locationMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<LocationContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.photoMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<PhotoContent>(scopeToIncludeChannels)
|
||||
/**
|
||||
* Shortcut for [photoMessages]
|
||||
*/
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun FlowsUpdatesFilter.imageMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = photoMessages(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.pollMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<PollContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.stickerMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<StickerContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.textMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<TextContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.venueMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<VenueContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.videoMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<VideoContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.videoNoteMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<VideoNoteContent>(scopeToIncludeChannels)
|
||||
|
||||
fun FlowsUpdatesFilter.voiceMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<VoiceContent>(scopeToIncludeChannels)
|
||||
|
||||
|
@ -2,14 +2,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retr
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.nonstrictJsonFormat
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.flowsUpdatesFilter
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.MultipartRequestImpl
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateDeserializationStrategy
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.ExceptionHandler
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely
|
||||
@ -56,6 +56,16 @@ fun Route.includeWebhookHandlingInRoute(
|
||||
}
|
||||
}
|
||||
|
||||
fun Route.includeWebhookHandlingInRouteWithFlows(
|
||||
scope: CoroutineScope,
|
||||
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
||||
block: FlowsUpdatesFilter.() -> Unit
|
||||
) = includeWebhookHandlingInRoute(
|
||||
scope,
|
||||
exceptionsHandler,
|
||||
flowsUpdatesFilter(block = block).asUpdateReceiver
|
||||
)
|
||||
|
||||
/**
|
||||
* Setting up ktor server, set webhook info via [SetWebhook] request.
|
||||
*
|
||||
|
@ -9,6 +9,6 @@ ktor_version=1.3.2
|
||||
javax_activation_version=1.1.1
|
||||
|
||||
library_group=com.github.insanusmokrassar
|
||||
library_version=0.27.9
|
||||
library_version=0.27.10
|
||||
|
||||
gradle_bintray_plugin_version=1.8.4
|
||||
|
Loading…
Reference in New Issue
Block a user