diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d0739c5cb..d5f815e739 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.35.1 +* `Behaviour Builder`: + * New extensions `telegramBotWithBehaviour` + ## 0.35.0 **ALL PREVIOUS DEPRECATIONS HAVE BEEN REMOVED** diff --git a/gradle.properties b/gradle.properties index 95df6f6f73..9f1b558a6a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ klock_version=2.1.2 uuid_version=0.3.0 ktor_version=1.6.0 -micro_utils_version=0.5.6 +micro_utils_version=0.5.12 javax_activation_version=1.1.1 diff --git a/tgbotapi.extensions.behaviour_builder/README.md b/tgbotapi.extensions.behaviour_builder/README.md index 70296ce190..c618a07ce8 100644 --- a/tgbotapi.extensions.behaviour_builder/README.md +++ b/tgbotapi.extensions.behaviour_builder/README.md @@ -15,7 +15,7 @@ bot.startGettingFlowsUpdatesByLongPolling { This library offer other way to do a lot of routine in more simple way: ```kotlin -telegramBot(token) { +telegramBotWithBehaviour(token) { onCommand("start".regex) { execute(SendTextMessage(it.chat.id, "This bot can ...")) // replaceable with reply(it, "This bot can ...") when you are using `tgbotapi.extensions.api` } @@ -28,7 +28,7 @@ In terminology of this project the `Triggers` are things which have no initial m messages and filter messages for context which will be used in subcontext. Full syntax with `onText` as an example: ```kotlin -telegramBot(TOKEN) { +telegramBotWithBehaviour(TOKEN) { onText( includeFilterByChatInBehaviourSubContext = true, // if false - last lambda will receive all messages instead of filtered by chat messages additionalFilter = { message: CommonMessage -> @@ -45,7 +45,7 @@ telegramBot(TOKEN) { Waiters targeted to get some content "here and now", they must be used inside some trigger main lambda: ```kotlin -telegramBot(TOKEN) { +telegramBotWithBehaviour(TOKEN) { onCommand("start") { message: CommonMessage -> val userPhotos = waitPhoto( SendTextMessage(it.chat.id, "Ok, send me some photo, please"), // init request, can be any `Request` object diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt new file mode 100644 index 0000000000..aec72211c6 --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt @@ -0,0 +1,75 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder + +import dev.inmo.tgbotapi.bot.Ktor.KtorRequestsExecutorBuilder +import dev.inmo.tgbotapi.bot.Ktor.telegramBot +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling +import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter +import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl +import kotlinx.coroutines.* +import kotlin.coroutines.coroutineContext + +/** + * Create bot using [telegramBot] and start listening for updates using [buildBehaviour]. + * Use this method in case you wish to make some additional actions with [flowsUpdatesFilter]. + * + * **WARNING** This method WILL NOT launch any listening of updates. Use something like + * [startGettingOfUpdatesByLongPolling] or tools for work with webhooks + * + * @return Created bot which has been used to create [BehaviourContext] via [buildBehaviour] + * + * @see [BehaviourContext] + * @see [buildBehaviour] + * @see startGettingOfUpdatesByLongPolling + */ +suspend fun telegramBotWithBehaviour( + token: String, + flowsUpdatesFilter: FlowsUpdatesFilter, + scope: CoroutineScope? = null, + apiUrl: String = telegramBotAPIDefaultUrl, + builder: KtorRequestsExecutorBuilder.() -> Unit = {}, + block: BehaviourContextReceiver +): TelegramBot = telegramBot( + token, + apiUrl, + builder +).apply { + buildBehaviour( + scope ?: CoroutineScope(coroutineContext), + flowsUpdatesFilter, + block + ) +} + +/** + * Create bot using [telegramBot] and start listening for updates using [buildBehaviour]. + * Use this method in case you wish to make some additional actions with [flowsUpdatesFilter]. + * + * **WARNING** This method WILL NOT launch any listening of updates. Use something like + * [startGettingOfUpdatesByLongPolling] or tools for work with webhooks + * + * @return Pair of [TelegramBot] and [Job]. This [Job] can be used to stop listening updates in your [block] you passed + * here + * + * @see [BehaviourContext] + * @see [buildBehaviour] + * @see startGettingOfUpdatesByLongPolling + */ +suspend fun telegramBotWithBehaviour( + token: String, + scope: CoroutineScope? = null, + apiUrl: String = telegramBotAPIDefaultUrl, + builder: KtorRequestsExecutorBuilder.() -> Unit = {}, + block: BehaviourContextReceiver +): Pair { + return telegramBot( + token, + apiUrl, + builder + ).let { + it to it.buildBehaviour( + scope ?: CoroutineScope(coroutineContext), + block + ) + } +}