1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 00:03:48 +00:00

add waiters for mentions

This commit is contained in:
InsanusMokrassar 2022-08-23 13:12:22 +06:00
parent 41e6c52369
commit 766b7ca205
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.bot.GetMe
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.types.chat.User
import dev.inmo.tgbotapi.types.message.content.MessageContent
import kotlinx.coroutines.flow.*
suspend fun BehaviourContext.waitMentionContents(
chatId: ChatId? = null,
username: Username? = null,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }
): Flow<MessageContent> = waitMentionMessages(chatId, username, initRequest, includeMediaGroups, errorFactory).map { it.content }
suspend fun BehaviourContext.waitMentionContents(
user: User,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }
): Flow<MessageContent> = waitMentionContents(user.id, user.username, initRequest, includeMediaGroups, errorFactory)
/**
* This expectation will get bot info and then
* Currently it is expected that the bot username will not be changed in runtime
*/
suspend fun BehaviourContext.waitBotMentions(
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }
): Flow<MessageContent> = waitMentionContents(execute(GetMe), initRequest, includeMediaGroups, errorFactory)

View File

@ -0,0 +1,48 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.abstracts.TextedInput
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.bot.GetMe
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.types.chat.User
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.MessageContent
import dev.inmo.tgbotapi.types.message.textsources.MentionTextSource
import dev.inmo.tgbotapi.types.message.textsources.TextMentionTextSource
import kotlinx.coroutines.flow.*
suspend fun BehaviourContext.waitMentionMessages(
chatId: ChatId? = null,
username: Username? = null,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }
): Flow<CommonMessage<MessageContent>> {
return waitContentMessage<MessageContent>(initRequest, includeMediaGroups, errorFactory).filter {
val content = (it.content as? TextedInput) ?: return@filter false
content.textSources.any {
(it is MentionTextSource && (username == null || it.username == username))
|| (it is TextMentionTextSource && (chatId == null || it.user.id == chatId))
}
}
}
suspend fun BehaviourContext.waitMentionMessages(
user: User,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }
): Flow<CommonMessage<MessageContent>> = waitMentionMessages(user.id, user.username, initRequest, includeMediaGroups, errorFactory)
/**
* This expectation will get bot info and then
* Currently it is expected that the bot username will not be changed in runtime
*/
suspend fun BehaviourContext.waitBotMentionMessages(
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null }
): Flow<CommonMessage<MessageContent>> = waitMentionMessages(execute(GetMe), initRequest, includeMediaGroups, errorFactory)