mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-25 19:48:43 +00:00
add ChatJoinRequest
This commit is contained in:
parent
c969d88bf0
commit
c24d536d4c
@ -0,0 +1,51 @@
|
|||||||
|
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.*
|
||||||
|
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||||
|
import dev.inmo.tgbotapi.types.ChatJoinRequest
|
||||||
|
import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery
|
||||||
|
import dev.inmo.tgbotapi.types.payments.ShippingQuery
|
||||||
|
import kotlinx.coroutines.flow.toList
|
||||||
|
|
||||||
|
typealias ChatJoinRequestsMapper = suspend ChatJoinRequest.() -> ChatJoinRequest?
|
||||||
|
|
||||||
|
private suspend fun <O> BehaviourContext.waitChatJoinRequests(
|
||||||
|
count: Int = 1,
|
||||||
|
initRequest: Request<*>? = null,
|
||||||
|
errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
|
filter: SimpleFilter<ChatJoinRequest>? = null,
|
||||||
|
mapper: suspend ChatJoinRequest.() -> O?
|
||||||
|
): List<O> = expectFlow(
|
||||||
|
initRequest,
|
||||||
|
count,
|
||||||
|
errorFactory
|
||||||
|
) {
|
||||||
|
val data = it.asChatJoinRequestUpdate() ?.data
|
||||||
|
if (data != null && (filter == null || filter(data))) {
|
||||||
|
data.mapper().let(::listOfNotNull)
|
||||||
|
} else {
|
||||||
|
emptyList()
|
||||||
|
}
|
||||||
|
}.toList().toList()
|
||||||
|
|
||||||
|
|
||||||
|
suspend fun BehaviourContext.waitChatJoinRequests(
|
||||||
|
initRequest: Request<*>? = null,
|
||||||
|
errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
|
count: Int = 1,
|
||||||
|
filter: SimpleFilter<ChatJoinRequest>? = null,
|
||||||
|
mapper: ChatJoinRequestsMapper? = null
|
||||||
|
) : List<ChatJoinRequest> = waitChatJoinRequests(
|
||||||
|
count,
|
||||||
|
initRequest,
|
||||||
|
errorFactory,
|
||||||
|
filter
|
||||||
|
) {
|
||||||
|
if (mapper == null) {
|
||||||
|
this
|
||||||
|
} else {
|
||||||
|
mapper(this)
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.filters
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver
|
||||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
|
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
|
||||||
import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery
|
import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery
|
||||||
|
import dev.inmo.tgbotapi.types.ChatJoinRequest
|
||||||
import dev.inmo.tgbotapi.types.ChatMemberUpdated
|
import dev.inmo.tgbotapi.types.ChatMemberUpdated
|
||||||
import dev.inmo.tgbotapi.types.InlineQueries.query.InlineQuery
|
import dev.inmo.tgbotapi.types.InlineQueries.query.InlineQuery
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
||||||
@ -54,3 +55,9 @@ val InlineQueryFilterByUser: BehaviourContextAndTwoTypesReceiver<Boolean, Inline
|
|||||||
val ChatMemberUpdatedFilterByChat: BehaviourContextAndTwoTypesReceiver<Boolean, ChatMemberUpdated, Update> = { updated, update ->
|
val ChatMemberUpdatedFilterByChat: BehaviourContextAndTwoTypesReceiver<Boolean, ChatMemberUpdated, Update> = { updated, update ->
|
||||||
update.sourceChat() ?.id == updated.chat.id
|
update.sourceChat() ?.id == updated.chat.id
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Allow only events from the same chat as base [ChatMemberUpdated]
|
||||||
|
*/
|
||||||
|
val ChatJoinRequestFilterByChat: BehaviourContextAndTwoTypesReceiver<Boolean, ChatJoinRequest, Update> = { updated, update ->
|
||||||
|
update.sourceChat() ?.id == updated.chat.id
|
||||||
|
}
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.asChatJoinRequestUpdate
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.asShippingQueryUpdate
|
||||||
|
import dev.inmo.tgbotapi.types.ChatJoinRequest
|
||||||
|
import dev.inmo.tgbotapi.types.payments.ShippingQuery
|
||||||
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Please, remember that your bot must have `can_invite_users` to receive these requests
|
||||||
|
*
|
||||||
|
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
|
||||||
|
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
|
||||||
|
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
|
||||||
|
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
|
||||||
|
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
|
||||||
|
* to combinate several filters
|
||||||
|
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
|
||||||
|
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
|
||||||
|
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
|
||||||
|
* data
|
||||||
|
*/
|
||||||
|
suspend fun <BC : BehaviourContext> BC.onChatJoinRequest(
|
||||||
|
initialFilter: SimpleFilter<ChatJoinRequest>? = null,
|
||||||
|
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatJoinRequest, Update>? = ChatJoinRequestFilterByChat,
|
||||||
|
markerFactory: MarkerFactory<in ChatJoinRequest, Any> = ByChatChatJoinRequestMarkerFactory,
|
||||||
|
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatJoinRequest>
|
||||||
|
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
|
||||||
|
(it.asChatJoinRequestUpdate() ?.data) ?.let(::listOfNotNull)
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories
|
package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery
|
import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery
|
||||||
|
import dev.inmo.tgbotapi.types.ChatJoinRequest
|
||||||
import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery
|
import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery
|
||||||
import dev.inmo.tgbotapi.types.payments.ShippingQuery
|
import dev.inmo.tgbotapi.types.payments.ShippingQuery
|
||||||
|
|
||||||
@ -8,6 +9,10 @@ object ByUserCallbackQueryMarkerFactory : MarkerFactory<CallbackQuery, Any> {
|
|||||||
override suspend fun invoke(data: CallbackQuery) = data.user
|
override suspend fun invoke(data: CallbackQuery) = data.user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object ByChatChatJoinRequestMarkerFactory : MarkerFactory<ChatJoinRequest, Any> {
|
||||||
|
override suspend fun invoke(data: ChatJoinRequest) = data.chat
|
||||||
|
}
|
||||||
|
|
||||||
object ByUserShippingQueryMarkerFactory : MarkerFactory<ShippingQuery, Any> {
|
object ByUserShippingQueryMarkerFactory : MarkerFactory<ShippingQuery, Any> {
|
||||||
override suspend fun invoke(data: ShippingQuery) = data.user
|
override suspend fun invoke(data: ShippingQuery) = data.user
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package dev.inmo.tgbotapi.types
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.CommonAbstracts.FromUser
|
||||||
|
import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class ChatJoinRequest(
|
||||||
|
@SerialName(chatField)
|
||||||
|
val chat: PublicChat,
|
||||||
|
@SerialName(userField)
|
||||||
|
override val from: User,
|
||||||
|
@SerialName(dateField)
|
||||||
|
val date: TelegramDate,
|
||||||
|
@SerialName(bioField)
|
||||||
|
val bio: String,
|
||||||
|
@SerialName(inviteLinkField)
|
||||||
|
val inviteLink: ChatInviteLink
|
||||||
|
) : FromUser
|
@ -0,0 +1,10 @@
|
|||||||
|
package dev.inmo.tgbotapi.types.update
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.types.ChatJoinRequest
|
||||||
|
import dev.inmo.tgbotapi.types.UpdateIdentifier
|
||||||
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
|
|
||||||
|
data class ChatJoinRequestUpdate(
|
||||||
|
override val updateId: UpdateIdentifier,
|
||||||
|
override val data: ChatJoinRequest
|
||||||
|
) : Update
|
@ -35,7 +35,8 @@ internal data class RawUpdate constructor(
|
|||||||
private val poll: Poll? = null,
|
private val poll: Poll? = null,
|
||||||
private val poll_answer: PollAnswer? = null,
|
private val poll_answer: PollAnswer? = null,
|
||||||
private val my_chat_member: ChatMemberUpdated? = null,
|
private val my_chat_member: ChatMemberUpdated? = null,
|
||||||
private val chat_member: ChatMemberUpdated? = null
|
private val chat_member: ChatMemberUpdated? = null,
|
||||||
|
private val chat_join_request: ChatJoinRequest? = null
|
||||||
) {
|
) {
|
||||||
private var initedUpdate: Update? = null
|
private var initedUpdate: Update? = null
|
||||||
/**
|
/**
|
||||||
@ -61,6 +62,7 @@ internal data class RawUpdate constructor(
|
|||||||
poll_answer != null -> PollAnswerUpdate(updateId, poll_answer)
|
poll_answer != null -> PollAnswerUpdate(updateId, poll_answer)
|
||||||
my_chat_member != null -> MyChatMemberUpdatedUpdate(updateId, my_chat_member)
|
my_chat_member != null -> MyChatMemberUpdatedUpdate(updateId, my_chat_member)
|
||||||
chat_member != null -> CommonChatMemberUpdatedUpdate(updateId, chat_member)
|
chat_member != null -> CommonChatMemberUpdatedUpdate(updateId, chat_member)
|
||||||
|
chat_join_request != null -> ChatJoinRequestUpdate(updateId, chat_join_request)
|
||||||
else -> UnknownUpdate(
|
else -> UnknownUpdate(
|
||||||
updateId,
|
updateId,
|
||||||
raw.toString(),
|
raw.toString(),
|
||||||
|
@ -32,6 +32,7 @@ interface FlowsUpdatesFilter : UpdatesFilter {
|
|||||||
val pollAnswersFlow: Flow<PollAnswerUpdate>
|
val pollAnswersFlow: Flow<PollAnswerUpdate>
|
||||||
val chatMemberUpdatesFlow: Flow<CommonChatMemberUpdatedUpdate>
|
val chatMemberUpdatesFlow: Flow<CommonChatMemberUpdatedUpdate>
|
||||||
val myChatMemberUpdatesFlow: Flow<MyChatMemberUpdatedUpdate>
|
val myChatMemberUpdatesFlow: Flow<MyChatMemberUpdatedUpdate>
|
||||||
|
val chatJoinRequestUpdateFlow: Flow<ChatJoinRequestUpdate>
|
||||||
val unknownUpdatesFlow: Flow<UnknownUpdate>
|
val unknownUpdatesFlow: Flow<UnknownUpdate>
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +63,7 @@ abstract class AbstractFlowsUpdatesFilter : FlowsUpdatesFilter {
|
|||||||
override val pollAnswersFlow: Flow<PollAnswerUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
override val pollAnswersFlow: Flow<PollAnswerUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
||||||
override val chatMemberUpdatesFlow: Flow<CommonChatMemberUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
override val chatMemberUpdatesFlow: Flow<CommonChatMemberUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
||||||
override val myChatMemberUpdatesFlow: Flow<MyChatMemberUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
override val myChatMemberUpdatesFlow: Flow<MyChatMemberUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
||||||
|
override val chatJoinRequestUpdateFlow: Flow<ChatJoinRequestUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
||||||
override val unknownUpdatesFlow: Flow<UnknownUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
override val unknownUpdatesFlow: Flow<UnknownUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2204,6 +2204,15 @@ inline fun Update.asChatMemberUpdatedUpdate(): ChatMemberUpdatedUpdate? = this a
|
|||||||
@PreviewFeature
|
@PreviewFeature
|
||||||
inline fun Update.requireChatMemberUpdatedUpdate(): ChatMemberUpdatedUpdate = this as ChatMemberUpdatedUpdate
|
inline fun Update.requireChatMemberUpdatedUpdate(): ChatMemberUpdatedUpdate = this as ChatMemberUpdatedUpdate
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun <T> Update.whenChatJoinRequestUpdate(block: (ChatJoinRequestUpdate) -> T) = asChatJoinRequestUpdate() ?.let(block)
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun Update.asChatJoinRequestUpdate(): ChatJoinRequestUpdate? = this as? ChatJoinRequestUpdate
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun Update.requireChatJoinRequestUpdate(): ChatJoinRequestUpdate = this as ChatJoinRequestUpdate
|
||||||
|
|
||||||
@PreviewFeature
|
@PreviewFeature
|
||||||
inline fun <T> TelegramMediaFile.whenAnimationFile(block: (AnimationFile) -> T) = asAnimationFile() ?.let(block)
|
inline fun <T> TelegramMediaFile.whenAnimationFile(block: (AnimationFile) -> T) = asAnimationFile() ?.let(block)
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.CommonAbstracts.WithUser
|
|||||||
import dev.inmo.tgbotapi.extensions.utils.asFromUser
|
import dev.inmo.tgbotapi.extensions.utils.asFromUser
|
||||||
import dev.inmo.tgbotapi.extensions.utils.asUser
|
import dev.inmo.tgbotapi.extensions.utils.asUser
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat
|
||||||
|
import dev.inmo.tgbotapi.types.ChatJoinRequest
|
||||||
import dev.inmo.tgbotapi.types.User
|
import dev.inmo.tgbotapi.types.User
|
||||||
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
||||||
import dev.inmo.tgbotapi.types.update.*
|
import dev.inmo.tgbotapi.types.update.*
|
||||||
@ -14,12 +15,13 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
|
|||||||
import dev.inmo.tgbotapi.utils.PreviewFeature
|
import dev.inmo.tgbotapi.utils.PreviewFeature
|
||||||
|
|
||||||
@PreviewFeature
|
@PreviewFeature
|
||||||
fun Update.sourceChat(): Chat? = when {
|
fun Update.sourceChat(): Chat? = when (this) {
|
||||||
this is MediaGroupUpdate -> when (this) {
|
is MediaGroupUpdate -> when (this) {
|
||||||
is SentMediaGroupUpdate -> data.chat
|
is SentMediaGroupUpdate -> data.chat
|
||||||
is EditMediaGroupUpdate -> data.chat
|
is EditMediaGroupUpdate -> data.chat
|
||||||
}
|
}
|
||||||
this is BaseMessageUpdate -> data.chat
|
is BaseMessageUpdate -> data.chat
|
||||||
|
is ChatJoinRequestUpdate -> data.chat
|
||||||
else -> {
|
else -> {
|
||||||
when (val data = data) {
|
when (val data = data) {
|
||||||
is FromUser -> data.from
|
is FromUser -> data.from
|
||||||
|
Loading…
Reference in New Issue
Block a user