1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

total rework of chats

This commit is contained in:
InsanusMokrassar 2019-08-12 15:15:22 +06:00
parent c47c82ac43
commit d6bb14e342
34 changed files with 240 additions and 117 deletions

View File

@ -2,16 +2,14 @@
## 0.17.0
Libraries updates:
* Kotlin version `1.3.31` -> `1.3.41`
* Kotlin Coroutines version `1.2.1` -> `1.2.2`
* Kotlin Serialization version `0.11.0` -> `0.11.1`
* Joda Time version `2.10.1` -> `2.10.3`
* Ktor version `1.1.4` -> `1.2.2`
* `RequestsExecutor` now is `Closeable`
* `TelegramAPIUrlsKeeper` was added to provide more comfortable work with file urls and other things
like this
Changes according to [July 29, 2019 Telegram Bot API update](https://core.telegram.org/bots/api#july-29-2019):
* `Sticker` and `StickerSet` now have field `isAnimated`
@ -22,6 +20,13 @@ was replaced into `abstracts` package and available permissions was updated
* `RestrictChatMember` request now accept `permissions` object instead of separated permissions
* All `GroupChat` instances have description
Other important changes:
* Totally reworked chats hierarchy. `Extended` abstractions was added for cases when called `GetChat` request
* `RequestsExecutor` now is `Closeable`
* `TelegramAPIUrlsKeeper` was added to provide more comfortable work with file urls and other things
like this
## 0.16.0 Bot API 4.3
* `LoginURL` and `LoginURLInlineKeyboardButton` has been added

View File

@ -0,0 +1,15 @@
package com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.RawChat
sealed class HandleException (
message: String
) : IllegalArgumentException(
message
)
class IllegalChatRawObjectException(
rawChat: RawChat
) : HandleException(
"One of the fields in raw chat object is incorrect"
)

View File

@ -11,7 +11,7 @@ data class SetChatStickerSet(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@SerialName(stickerSetNameField)
val stickerSetName: String
val stickerSetName: StickerSetName
): ChatRequest, SimpleRequest<Boolean> {
override fun method(): String = "setChatStickerSet"
override fun resultSerializer(): KSerializer<Boolean> = BooleanSerializer

View File

@ -17,6 +17,7 @@ typealias ShippingOptionIdentifier = String
typealias StartParameter = String
typealias InlineMessageIdentifier = String
typealias PollIdentifier = String
typealias StickerSetName = String
val callbackQueryAnswerLength = 0 until 200
val captionLength = 0 until 1024

View File

@ -1,13 +1,12 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.UsernameChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PublicChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
data class PrivateChat(
data class ChannelChatImpl(
override val id: ChatId,
override val username: Username? = null,
val firstName: String? = null,
val lastName: String? = null,
override val chatPhoto: ChatPhoto? = null
) : Chat, UsernameChat
override val title: String,
override val username: Username? = null
) : ChannelChat

View File

@ -1,9 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
@Deprecated(
"Replaced into another package",
ReplaceWith("Chat", "com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat")
)
typealias Chat = Chat

View File

@ -1,9 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat
@Deprecated(
"Replaced into another package",
ReplaceWith("GroupChat", "com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat")
)
typealias GroupChat = GroupChat

View File

@ -7,10 +7,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
data class GroupChatImpl(
override val id: ChatId,
override val title: String? = null,
override val description: String? = null,
override val inviteLink: String? = null,
override val chatPhoto: ChatPhoto? = null,
override val pinnedMessage: RawMessage? = null,
override val permissions: ChatPermissions? = null
override val title: String
) : GroupChat

View File

@ -0,0 +1,11 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
data class PrivateChatImpl(
override val id: ChatId,
override val username: Username? = null,
override val firstName: String = "",
override val lastName: String = ""
) : PrivateChat

View File

@ -1,9 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PublicChat
@Deprecated(
"Replaced into another package",
ReplaceWith("PublicChat", "com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PublicChat")
)
typealias PublicChat = PublicChat

View File

@ -1,7 +1,10 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.IllegalChatRawObjectException
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -20,43 +23,75 @@ data class RawChat(
private val sticker_set_name: String? = null,
private val can_set_sticker_set: Boolean? = null,
@SerialName("photo")
override val chatPhoto: ChatPhoto? = null,
private val chatPhoto: ChatPhoto? = null,
private val permissions: ChatPermissions? = null
) : Chat {
fun extractChat(): Chat {
private fun extractExtendedChat(): ExtendedChat {
return when (type) {
"private" -> PrivateChat(id, username, first_name, last_name, chatPhoto)
"group" -> GroupChatImpl(
"private" -> ExtendedPrivateChatImpl(id, username, first_name ?: "", last_name ?: "", chatPhoto!!)
"group" -> ExtendedGroupChatImpl(
id,
title,
description,
title!!,
chatPhoto!!,
description ?: "",
invite_link,
chatPhoto,
pinned_message,
permissions
permissions!!,
pinned_message
)
"supergroup" -> SupergroupChat(
"supergroup" -> ExtendedSupergroupChatImpl(
id,
title,
title!!,
username,
description,
chatPhoto!!,
description ?: "",
invite_link,
chatPhoto,
permissions!!,
pinned_message,
sticker_set_name,
can_set_sticker_set ?: false,
permissions
can_set_sticker_set ?: false
)
"channel" -> ChannelChat(
"channel" -> ExtendedChannelChatImpl(
id,
title,
title!!,
username,
description,
chatPhoto!!,
description ?: "",
invite_link,
chatPhoto,
pinned_message
)
else -> throw IllegalArgumentException("Unknown type of chat")
}
}
private fun extractPreviewChat(): Chat {
return when (type) {
"private" -> PrivateChatImpl(id, username, first_name ?: "", last_name ?: "")
"group" -> GroupChatImpl(
id,
title!!
)
"supergroup" -> SupergroupChatImpl(
id,
title!!,
username
)
"channel" -> ChannelChatImpl(
id,
title!!,
username
)
else -> throw IllegalArgumentException("Unknown type of chat")
}
}
fun extractChat(): Chat {
return try {
when (chatPhoto) {
null -> extractPreviewChat()
else -> extractExtendedChat()
}
} catch (e: NullPointerException) {
throw IllegalChatRawObjectException(this)
}
}
}

View File

@ -1,19 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
data class SupergroupChat(
override val id: ChatId,
override val title: String? = null,
override val username: Username? = null,
override val description: String? = null,
override val inviteLink: String? = null,
override val chatPhoto: ChatPhoto? = null,
override val pinnedMessage: RawMessage? = null,
val stickerSetName: String? = null,
val canSetStickerSet: Boolean = false,
override val permissions: ChatPermissions? = null
) : GroupChat, UsernameChat

View File

@ -2,15 +2,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PublicChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.SupergroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
data class ChannelChat(
data class SupergroupChatImpl(
override val id: ChatId,
override val title: String? = null,
override val username: Username? = null,
override val description: String? = null,
override val inviteLink: String? = null,
override val chatPhoto: ChatPhoto? = null,
override val pinnedMessage: RawMessage?
) : PublicChat, UsernameChat, DescriptionChat
override val title: String,
override val username: Username? = null
) : SupergroupChat

View File

@ -1,5 +1,3 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts
interface DescriptionChat : PublicChat {
val description: String?
}
interface ChannelChat : SuperPublicChat

View File

@ -5,5 +5,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.ChatPhoto
interface Chat {
val id: ChatId
val chatPhoto: ChatPhoto?
}

View File

@ -1,7 +1,3 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ChatPermissions
interface GroupChat : PublicChat, DescriptionChat {
val permissions: ChatPermissions?
}
interface GroupChat : PublicChat

View File

@ -0,0 +1,6 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts
interface PrivateChat : Chat, UsernameChat {
val firstName: String
val lastName: String
}

View File

@ -1,9 +1,5 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
interface PublicChat : Chat {
val title: String?
val inviteLink: String?
val pinnedMessage: RawMessage?
val title: String
}

View File

@ -0,0 +1,3 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts
interface SuperPublicChat : PublicChat, UsernameChat

View File

@ -0,0 +1,3 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts
interface SupergroupChat : GroupChat, SuperPublicChat

View File

@ -0,0 +1,5 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.ChannelChat
interface ExtendedChannelChat : ChannelChat, ExtendedPublicChat

View File

@ -0,0 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatPhoto
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
interface ExtendedChat : Chat {
val chatPhoto: ChatPhoto
}

View File

@ -0,0 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ChatPermissions
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat
interface ExtendedGroupChat : GroupChat, ExtendedPublicChat {
val permissions: ChatPermissions
}

View File

@ -0,0 +1,5 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat
interface ExtendedPrivateChat : PrivateChat, ExtendedChat

View File

@ -0,0 +1,10 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PublicChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
interface ExtendedPublicChat : ExtendedChat, PublicChat {
val description: String
val inviteLink: String?
val pinnedMessage: RawMessage?
}

View File

@ -0,0 +1,9 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.StickerSetName
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.SupergroupChat
interface ExtendedSupergroupChat : SupergroupChat, ExtendedGroupChat {
val stickerSetName: StickerSetName?
val canSetStickerSet: Boolean
}

View File

@ -0,0 +1,15 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChannelChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
data class ExtendedChannelChatImpl(
override val id: ChatId,
override val title: String,
override val username: Username?,
override val chatPhoto: ChatPhoto,
override val description: String,
override val inviteLink: String?,
override val pinnedMessage: RawMessage?
) : ExtendedChannelChat

View File

@ -0,0 +1,17 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatPhoto
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ChatPermissions
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedGroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
data class ExtendedGroupChatImpl(
override val id: ChatId,
override val title: String,
override val chatPhoto: ChatPhoto,
override val description: String,
override val inviteLink: String?,
override val permissions: ChatPermissions,
override val pinnedMessage: RawMessage?
) : ExtendedGroupChat

View File

@ -0,0 +1,12 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedPrivateChat
data class ExtendedPrivateChatImpl(
override val id: ChatId,
override val username: Username? = null,
override val firstName: String = "",
override val lastName: String = "",
override val chatPhoto: ChatPhoto
) : ExtendedPrivateChat

View File

@ -0,0 +1,19 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ChatPermissions
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedSupergroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
data class ExtendedSupergroupChatImpl(
override val id: ChatId,
override val title: String,
override val username: Username? = null,
override val chatPhoto: ChatPhoto,
override val description: String,
override val inviteLink: String?,
override val permissions: ChatPermissions,
override val pinnedMessage: RawMessage?,
override val stickerSetName: StickerSetName?,
override val canSetStickerSet: Boolean
) : ExtendedSupergroupChat

View File

@ -20,7 +20,7 @@ data class Sticker(
@SerialName(emojiField)
val emoji: String? = null,
@SerialName(stickerSetNameField)
val stickerSetName: String? = null,
val stickerSetName: StickerSetName? = null,
@SerialName(isAnimatedField)
val isAnimated: Boolean = false,
@SerialName(maskPositionField)

View File

@ -1,7 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.message
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ChannelChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ChannelChatImpl
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.ChannelChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.abstracts.ChannelEvent
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ChatEventMessage
import org.joda.time.DateTime

View File

@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntitiesSerializer
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.files.*
import com.github.insanusmokrassar.TelegramBotAPI.types.games.Game
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.*
@ -199,18 +199,18 @@ data class RawMessage(
chatEvent ?.let {
chatEvent ->
when (chat) {
is GroupChat -> GroupEventMessage(
messageId,
chat,
chatEvent as? GroupEvent ?: throwWrongChatEvent(GroupChat::class, chatEvent),
date.asDate
)
is SupergroupChat -> SupergroupEventMessage(
messageId,
chat,
chatEvent as? SupergroupEvent ?: throwWrongChatEvent(SupergroupEvent::class, chatEvent),
date.asDate
)
is GroupChat -> GroupEventMessage(
messageId,
chat,
chatEvent as? GroupEvent ?: throwWrongChatEvent(GroupChat::class, chatEvent),
date.asDate
)
is ChannelChat -> ChannelEventMessage(
messageId,
chat,

View File

@ -1,7 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.message
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.SupergroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.SupergroupChatImpl
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.SupergroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.abstracts.SupergroupEvent
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ChatEventMessage
import org.joda.time.DateTime