diff --git a/CHANGELOG.md b/CHANGELOG.md index a43e25fe2c..980ff04e18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * `Behaviour Builder`: * All triggers have been changed to use two filters: filter for in subcontext data and filter for incoming data * New waiters for edited content + * New extension `BehaviourContext#followLocation` ## 0.35.8 diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/LiveLocation.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/LiveLocation.kt new file mode 100644 index 0000000000..ac5b3deaba --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/LiveLocation.kt @@ -0,0 +1,30 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.utils + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTypeReceiver +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitEditedLocation +import dev.inmo.tgbotapi.types.location.* +import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage +import dev.inmo.tgbotapi.types.message.content.LiveLocationContent + +/** + * Use this extension when you want to follow [LiveLocation] until it will became [StaticLocation]. This method + * is synchronous. You may use something like [kotlinx.coroutines.launch] or [kotlinx.coroutines.async] to run it + * asynchronously + */ +suspend fun BehaviourContext.followLocation( + message: ContentMessage, + onLocation: BehaviourContextAndTypeReceiver +) { + var currentLocation: Location = message.content.location + onLocation(message.content.location) + + while (currentLocation !is StaticLocation) { + currentLocation = waitEditedLocation( + filter = { + it.messageId == message.messageId && it.chat.id == message.chat.id + } + ).first().location + onLocation(currentLocation) + } +}