2021-01-07 18:30:29 +00:00
|
|
|
@file:Suppress("unused", "UNCHECKED_CAST")
|
|
|
|
|
2021-01-07 12:17:50 +00:00
|
|
|
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
|
2021-01-07 06:45:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
|
|
|
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
2021-01-07 12:17:50 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTypeReceiver
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
|
2021-01-07 18:22:19 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.*
|
2021-01-07 11:57:08 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
|
2021-01-07 06:45:30 +00:00
|
|
|
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
|
|
|
|
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
2021-01-07 06:45:54 +00:00
|
|
|
import dev.inmo.tgbotapi.types.message.content.*
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.abstracts.*
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.media.*
|
|
|
|
import dev.inmo.tgbotapi.types.message.payments.InvoiceContent
|
2021-01-07 06:45:30 +00:00
|
|
|
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
|
2021-01-07 18:30:29 +00:00
|
|
|
import dev.inmo.tgbotapi.utils.PreviewFeature
|
2021-01-07 06:45:30 +00:00
|
|
|
import kotlinx.coroutines.flow.filter
|
|
|
|
|
|
|
|
|
2021-01-07 18:30:29 +00:00
|
|
|
@PreviewFeature
|
2021-01-07 12:11:01 +00:00
|
|
|
internal suspend inline fun <reified T : MessageContent> BehaviourContext.onContent(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 18:22:19 +00:00
|
|
|
includeMediaGroups: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
noinline additionalFilter: (suspend (ContentMessage<T>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<T>>
|
2021-01-07 06:45:30 +00:00
|
|
|
) = flowsUpdatesFilter.expectFlow(bot) {
|
2021-01-07 18:22:19 +00:00
|
|
|
if (includeMediaGroups) {
|
|
|
|
it.asSentMediaGroupUpdate() ?.data ?.mapNotNull {
|
|
|
|
if (it.content is T) {
|
|
|
|
val adaptedMessage = it as ContentMessage<T>
|
|
|
|
if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null
|
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
} ?.let {
|
|
|
|
return@expectFlow it
|
|
|
|
}
|
|
|
|
}
|
2021-01-07 06:45:30 +00:00
|
|
|
it.asMessageUpdate() ?.data ?.asContentMessage() ?.let { message ->
|
|
|
|
if (message.content is T) {
|
|
|
|
val adaptedMessage = message as ContentMessage<T>
|
|
|
|
if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null
|
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
2021-01-07 18:22:19 +00:00
|
|
|
}.let(::listOfNotNull)
|
2021-01-07 06:45:30 +00:00
|
|
|
}.subscribeSafelyWithoutExceptions(scope) { triggerMessage ->
|
2021-01-07 12:11:01 +00:00
|
|
|
val (jobToCancel, scenario) = if (includeFilterByChatInBehaviourSubContext) {
|
2021-01-07 06:45:30 +00:00
|
|
|
val subFilter = FlowsUpdatesFilter()
|
2021-01-07 12:11:01 +00:00
|
|
|
val subBehaviourContext = copy(flowsUpdatesFilter = subFilter)
|
2021-01-07 06:45:30 +00:00
|
|
|
|
|
|
|
flowsUpdatesFilter.allUpdatesFlow.filter {
|
2021-01-07 11:57:08 +00:00
|
|
|
val chat = it.sourceChat() ?: return@filter false
|
|
|
|
chat.id.chatId == triggerMessage.chat.id.chatId
|
2021-01-07 12:11:01 +00:00
|
|
|
}.subscribeSafelyWithoutExceptions(scope, subFilter.asUpdateReceiver) to subBehaviourContext
|
2021-01-07 06:45:30 +00:00
|
|
|
} else {
|
|
|
|
null to this
|
|
|
|
}
|
|
|
|
safelyWithoutExceptions { scenario.scenarioReceiver(triggerMessage) }
|
|
|
|
jobToCancel ?.cancel()
|
|
|
|
}
|
|
|
|
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onContact(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<ContactContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<ContactContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onDice(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<DiceContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<DiceContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onGame(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<GameContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<GameContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onLocation(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<LocationContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<LocationContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onPoll(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<PollContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<PollContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onText(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<TextContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<TextContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onVenue(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<VenueContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<VenueContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onAudioMediaGroup(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<AudioMediaGroupContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<AudioMediaGroupContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onDocumentMediaGroup(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<DocumentMediaGroupContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<DocumentMediaGroupContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onMediaCollection(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<MediaCollectionContent<TelegramMediaFile>>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<MediaCollectionContent<TelegramMediaFile>>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onMedia(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<MediaContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<MediaContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onMediaGroup(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<MediaGroupContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<MediaGroupContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onVisualMediaGroup(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<VisualMediaGroupContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<VisualMediaGroupContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onAnimation(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<AnimationContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<AnimationContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onAudio(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<AudioContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<AudioContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onDocument(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<DocumentContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<DocumentContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onPhoto(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<PhotoContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<PhotoContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onSticker(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<StickerContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<StickerContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onVideo(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<VideoContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<VideoContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onVideoNote(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<VideoNoteContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<VideoNoteContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onVoice(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<VoiceContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<VoiceContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|
2021-01-07 12:11:01 +00:00
|
|
|
suspend fun BehaviourContext.onInvoice(
|
|
|
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
2021-01-07 06:45:30 +00:00
|
|
|
additionalFilter: (suspend (ContentMessage<InvoiceContent>) -> Boolean)? = null,
|
2021-01-07 12:11:01 +00:00
|
|
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, ContentMessage<InvoiceContent>>
|
2021-01-07 18:22:19 +00:00
|
|
|
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, scenarioReceiver)
|