1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-12-03 21:05:49 +00:00

Compare commits

..

14 Commits

11 changed files with 198 additions and 15 deletions

1
.github/labeler.yml vendored
View File

@@ -4,6 +4,7 @@ core: "TelegramBotAPI/**" # currently not work
code: "**/*.kt"
gradle: "**/*.gradle"
versions: "**/gradle.properties"
markdown:
- "**/*.md"
- "!CHANGELOG.md"

View File

@@ -49,6 +49,40 @@
* `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`
* Versions updates:
* `Gradle Wrapper`: `6.5-all` -> `6.5.1-bin`
* `Coroutines`: `1.3.7` -> `1.3.8`
* `Klock`: `1.11.3` -> `1.11.14`
* `UUID`: `0.1.0` -> `0.1.1`
### 0.27.8
* `TelegramBotAPI`:

View File

@@ -1,6 +1,6 @@
# TelegramBotAPI
| Common info | [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Build Status](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI.svg?branch=master)](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI) |
| Common info | [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Build Status](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI.svg?branch=master)](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI) [Small survey](https://forms.gle/tnjuExdSKEr32ygKA)|
| -------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Useful links | [![Chat in Telegram](badges/chat.svg)](https://teleg.one/InMoTelegramBotAPI) [![KDocs](badges/kdocs.svg)](https://tgbotapi.inmo.dev/docs/index.html) [Examples](https://github.com/InsanusMokrassar/TelegramBotAPI-examples/), [Mini tutorial](https://bookstack.inmo.dev/books/telegrambotapi/chapter/introduction-tutorial) |
| 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) |

View File

@@ -12,7 +12,7 @@ import io.ktor.client.engine.HttpClientEngine
*/
fun telegramBot(
urlsKeeper: TelegramAPIUrlsKeeper,
client: HttpClient
client: HttpClient = HttpClient()
): RequestsExecutor = KtorRequestsExecutor(
urlsKeeper,
client

View File

@@ -1,14 +1,14 @@
# TelegramBotAPI Util Extensions
# 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

View File

@@ -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()
}

View File

@@ -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)

View File

@@ -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.
*

View File

@@ -2,4 +2,5 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InlineQue
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.Locationed
@Deprecated("Will be removed due to useless")
interface PositionedInlineQueryResult : InlineQueryResult, Locationed

View File

@@ -1,14 +1,14 @@
kotlin.code.style=official
kotlin_version=1.3.72
kotlin_coroutines_version=1.3.7
kotlin_coroutines_version=1.3.8
kotlin_serialisation_runtime_version=0.20.0
klock_version=1.11.3
uuid_version=0.1.0
klock_version=1.11.14
uuid_version=0.1.1
ktor_version=1.3.2
javax_activation_version=1.1.1
library_group=com.github.insanusmokrassar
library_version=0.27.8
library_version=0.27.10
gradle_bintray_plugin_version=1.8.4

View File

@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip