From cde8fdeffdacf493c90df0415f9955ccae02a22c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 15:22:34 +0600 Subject: [PATCH 1/9] start 2.1.0 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de906bdf97..20aeea7539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 2.1.0 + ## 2.0.3 * `Core`: diff --git a/gradle.properties b/gradle.properties index 7f7862a783..0c92262296 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,6 +20,6 @@ javax_activation_version=1.1.1 dokka_version=1.6.21 library_group=dev.inmo -library_version=2.0.3 +library_version=2.1.0 github_release_plugin_version=2.4.1 From f2e9bf6bd8dc225615bc7afbec2248a887da2a5c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 16:27:53 +0600 Subject: [PATCH 2/9] update webapps support --- CHANGELOG.md | 2 + .../tgbotapi/types/chat/ChatSerializers.kt | 11 +++- .../dev/inmo/tgbotapi/types/chat/Extended.kt | 4 +- .../tgbotapi/types/chat/UnknownChatType.kt | 5 +- .../dev/inmo/tgbotapi/webapps/BackButton.kt | 11 ++++ .../dev/inmo/tgbotapi/webapps/Colors.kt | 20 +++++++ .../dev/inmo/tgbotapi/webapps/EventHandler.kt | 3 + .../dev/inmo/tgbotapi/webapps/EventType.kt | 3 + .../dev/inmo/tgbotapi/webapps/HEXColor.kt | 3 + .../dev/inmo/tgbotapi/webapps/MainButton.kt | 1 + .../dev/inmo/tgbotapi/webapps/ThemeParams.kt | 29 ++++++++-- .../dev/inmo/tgbotapi/webapps/WebApp.kt | 55 ++++++++++++++++++- .../dev/inmo/tgbotapi/webapps/WebAppChat.kt | 14 +++++ .../inmo/tgbotapi/webapps/WebAppInitData.kt | 5 ++ .../tgbotapi/webapps/haptic/HapticFeedback.kt | 7 +++ .../webapps/haptic/HapticFeedbackStyle.kt | 17 ++++++ .../webapps/haptic/HapticFeedbackType.kt | 15 +++++ .../webapps/invoice/InvoiceClosedInfo.kt | 16 ++++++ .../tgbotapi/webapps/invoice/InvoiceStatus.kt | 10 ++++ 19 files changed, 217 insertions(+), 14 deletions(-) create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/BackButton.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/Colors.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/HEXColor.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppChat.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedback.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackStyle.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackType.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceClosedInfo.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceStatus.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 20aeea7539..979a83195a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 2.1.0 +* Add support of functionality for `WebApp`s from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) + ## 2.0.3 * `Core`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt index 30adce6407..3d887f8ffe 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt @@ -65,7 +65,8 @@ object PreviewChatSerializer : KSerializer { ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) is ChatType.UnknownChatType -> UnknownChatType( formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), - decodedJson.toString() + decodedJson.toString(), + decodedJson ) } } @@ -77,6 +78,10 @@ object PreviewChatSerializer : KSerializer { is GroupChatImpl -> GroupChatImpl.serializer().serialize(encoder, value) is SupergroupChatImpl -> SupergroupChatImpl.serializer().serialize(encoder, value) is ChannelChatImpl -> ChannelChatImpl.serializer().serialize(encoder, value) + is CommonBot -> CommonBot.serializer().serialize(encoder, value) + is ExtendedBot -> ExtendedBot.serializer().serialize(encoder, value) + is CommonUser -> CommonUser.serializer().serialize(encoder, value) + is UnknownChatType -> JsonObject.serializer().serialize(encoder, value.rawJson) } } } @@ -99,7 +104,8 @@ object ExtendedChatSerializer : KSerializer { ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ExtendedChannelChatImpl.serializer(), decodedJson) is ChatType.UnknownChatType -> UnknownExtendedChat( formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), - decodedJson.toString() + decodedJson.toString(), + decodedJson ) } } @@ -110,6 +116,7 @@ object ExtendedChatSerializer : KSerializer { is ExtendedGroupChatImpl -> ExtendedGroupChatImpl.serializer().serialize(encoder, value) is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, value) is ExtendedChannelChatImpl -> ExtendedChannelChatImpl.serializer().serialize(encoder, value) + is UnknownExtendedChat -> JsonObject.serializer().serialize(encoder, value.rawJson) } } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt index 0dc5c4a1ee..484300e31f 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt @@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonObject @Serializable data class ExtendedChannelChatImpl( @@ -119,7 +120,8 @@ data class ExtendedBot( data class UnknownExtendedChat( override val id: ChatId, - val raw: String + val raw: String, + val rawJson: JsonObject ) : ExtendedChat { override val chatPhoto: ChatPhoto? = null } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt index 01f146c96c..5e9e9f4f01 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt @@ -1,9 +1,10 @@ package dev.inmo.tgbotapi.types.chat import dev.inmo.tgbotapi.types.ChatId -import dev.inmo.tgbotapi.types.chat.Chat +import kotlinx.serialization.json.JsonObject data class UnknownChatType( override val id: ChatId, - val raw: String + val raw: String, + val rawJson: JsonObject ) : Chat diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/BackButton.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/BackButton.kt new file mode 100644 index 0000000000..ccc12b1408 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/BackButton.kt @@ -0,0 +1,11 @@ +package dev.inmo.tgbotapi.webapps + +external interface BackButton { + val isVisible: Boolean + + fun onClick(callback: () -> Unit) + fun offClick(callback: () -> Unit) + + fun show() + fun hide() +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/Colors.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/Colors.kt new file mode 100644 index 0000000000..7c61897267 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/Colors.kt @@ -0,0 +1,20 @@ +package dev.inmo.tgbotapi.webapps + +import kotlinx.serialization.Serializable + +sealed interface Color { + val value: String + @Serializable + value class BackgroundColor(override val value: String) : Color + + @Serializable + value class Hex(override val value: String) : Color + + companion object { + val BackgroundColor = BackgroundColor("bg_color") + val SecondaryBackgroundColor = BackgroundColor("secondary_bg_color") + + @Suppress("NOTHING_TO_INLINE") + inline operator fun invoke(value: String) = Hex(value) + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt index 4d4e82a125..e0e5dc7f37 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt @@ -1,4 +1,7 @@ package dev.inmo.tgbotapi.webapps +import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo + typealias EventHandler = WebApp.() -> Unit typealias ViewportChangedEventHandler = WebApp.(ViewportChangedData) -> Unit +typealias InvoiceClosedEventHandler = WebApp.(InvoiceClosedInfo) -> Unit diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt index 1771966db3..a75b20218c 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt @@ -4,4 +4,7 @@ sealed class EventType(val typeName: String) { object ThemeChanged : EventType("themeChanged") object ViewportChanged : EventType("viewportChanged") object MainButtonClicked : EventType("mainButtonClicked") + object BackButtonClicked : EventType("backButtonClicked") + object SettingsButtonClicked : EventType("settingsButtonClicked") + object InvoiceClosed : EventType("invoiceClosed") } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/HEXColor.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/HEXColor.kt new file mode 100644 index 0000000000..d4d6e9bf56 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/HEXColor.kt @@ -0,0 +1,3 @@ +package dev.inmo.tgbotapi.webapps + +typealias HEXColor = String diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/MainButton.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/MainButton.kt index 3fabb5a2e7..2cb4ab06dc 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/MainButton.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/MainButton.kt @@ -23,6 +23,7 @@ external class MainButton { fun hideProgress(): MainButton internal fun onClick(eventHandler: () -> Unit): MainButton + internal fun offClick(eventHandler: () -> Unit): MainButton internal fun setParams(params: Json): MainButton } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ThemeParams.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ThemeParams.kt index a8db758c55..b72c7b5f40 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ThemeParams.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ThemeParams.kt @@ -2,15 +2,32 @@ package dev.inmo.tgbotapi.webapps external interface ThemeParams { @JsName("bg_color") - val backgroundColor: String? + val backgroundColor: HEXColor? + @JsName("secondary_bg_color") + val secondaryBackgroundColor: HEXColor? @JsName("text_color") - val textColor: String? + val textColor: HEXColor? @JsName("hint_color") - val hintColor: String? + val hintColor: HEXColor? @JsName("link_color") - val linkColor: String? + val linkColor: HEXColor? @JsName("button_color") - val buttonColor: String? + val buttonColor: HEXColor? @JsName("button_text_color") - val buttonTextColor: String? + val buttonTextColor: HEXColor? + + @JsName("bg_color") + val backgroundColorHex: Color.Hex? + @JsName("secondary_bg_color") + val secondaryBackgroundColorHex: Color.Hex? + @JsName("text_color") + val textColorHex: Color.Hex? + @JsName("hint_color") + val hintColorHex: Color.Hex? + @JsName("link_color") + val linkColorHex: Color.Hex? + @JsName("button_color") + val buttonColorHex: Color.Hex? + @JsName("button_text_color") + val buttonTextColorHex: Color.Hex? } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt index 356282cf93..365d07af4b 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt @@ -1,11 +1,21 @@ package dev.inmo.tgbotapi.webapps import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper +import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback +import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo external class WebApp { + val version: String + val initData: String val initDataUnsafe: WebAppInitData + val headerColor: HEXColor? + fun setHeaderColor(color: Color.BackgroundColor) + val backgroundColor: HEXColor? + fun setBackgroundColor(color: Color.Hex) + fun setBackgroundColor(color: Color.BackgroundColor) + @JsName("colorScheme") val colorSchemeRaw: String val themeParams: ThemeParams @@ -17,19 +27,34 @@ external class WebApp { @JsName("MainButton") val mainButton: MainButton + @JsName("BackButton") + val backButton: BackButton + + @JsName("HapticFeedback") + val hapticFeedback: HapticFeedback + internal fun onEvent(type: String, callback: () -> Unit) @JsName("onEvent") - internal fun onEventWithBoolean(type: String, callback: (ViewportChangedData) -> Unit) + internal fun onEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit) + @JsName("onEvent") + internal fun onEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit) fun offEvent(type: String, callback: () -> Unit) @JsName("offEvent") - fun offEventWithBoolean(type: String, callback: (ViewportChangedData) -> Unit) + fun offEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit) + @JsName("offEvent") + fun offEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit) fun sendData(data: String) fun ready() fun expand() fun close() + + fun isVersionAtLeast(version: String): Boolean + fun openLink(url: String) + fun openTelegramLink(url: String) + fun openInvoice(url: String, callback: (InvoiceClosedInfo) -> Unit = definedExternally) } val WebApp.colorScheme: ColorScheme @@ -57,7 +82,19 @@ fun WebApp.onEvent(type: EventType, eventHandler: EventHandler) = { fun WebApp.onEvent(type: EventType.ViewportChanged, eventHandler: ViewportChangedEventHandler) = { it: ViewportChangedData -> eventHandler(js("this").unsafeCast(), it) }.also { - onEventWithBoolean( + onEventWithViewportChangedData( + type.typeName, + callback = it + ) +} + +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onEvent(type: EventType.InvoiceClosed, eventHandler: InvoiceClosedEventHandler) = { it: InvoiceClosedInfo -> + eventHandler(js("this").unsafeCast(), it) +}.also { + onEventWithInvoiceClosedInfo( type.typeName, callback = it ) @@ -75,6 +112,18 @@ fun WebApp.onMainButtonClicked(eventHandler: EventHandler) = onEvent(EventType.M * @return The callback which should be used in case you want to turn off events handling */ fun WebApp.onViewportChanged(eventHandler: ViewportChangedEventHandler) = onEvent(EventType.ViewportChanged, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onBackButtonClicked(eventHandler: EventHandler) = onEvent(EventType.BackButtonClicked, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onSettingsButtonClicked(eventHandler: EventHandler) = onEvent(EventType.SettingsButtonClicked, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onInvoiceClosed(eventHandler: InvoiceClosedEventHandler) = onEvent(EventType.InvoiceClosed, eventHandler) fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppData( initData, diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppChat.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppChat.kt new file mode 100644 index 0000000000..e0dfa35de9 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppChat.kt @@ -0,0 +1,14 @@ +package dev.inmo.tgbotapi.webapps + +import dev.inmo.tgbotapi.types.ChatIdentifier +import dev.inmo.tgbotapi.types.Username +import dev.inmo.tgbotapi.types.chat.PublicChat + +external interface WebAppChat { + val id: ChatIdentifier + val type: String + val title: String + val username: Username? + @JsName("photo_url") + val photoUrl: String? +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppInitData.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppInitData.kt index a561cfb89a..78c7528233 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppInitData.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppInitData.kt @@ -15,5 +15,10 @@ external interface WebAppInitData { @JsName("auth_date") val authDate: MilliSeconds + @JsName("can_send_after") + val canSendAfter: MilliSeconds + + val chat: WebAppChat + val hash: String } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedback.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedback.kt new file mode 100644 index 0000000000..dad3bae8af --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedback.kt @@ -0,0 +1,7 @@ +package dev.inmo.tgbotapi.webapps.haptic + +external interface HapticFeedback { + fun impactOccurred(style: HapticFeedbackStyle) + fun notificationOccurred(type: HapticFeedbackType) + fun selectionChanged() +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackStyle.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackStyle.kt new file mode 100644 index 0000000000..bee1ea0c2f --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackStyle.kt @@ -0,0 +1,17 @@ +package dev.inmo.tgbotapi.webapps.haptic + +import dev.inmo.micro_utils.common.Warning +import kotlinx.serialization.Serializable + +@Serializable +value class HapticFeedbackStyle @Warning("Do not use this constructor if available objects from companion cover your needs") constructor( + val name: String +) { + companion object { + val Light = HapticFeedbackStyle("light") + val Medium = HapticFeedbackStyle("medium") + val Heavy = HapticFeedbackStyle("heavy") + val Rigid = HapticFeedbackStyle("rigid") + val Soft = HapticFeedbackStyle("soft") + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackType.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackType.kt new file mode 100644 index 0000000000..940b20d518 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/haptic/HapticFeedbackType.kt @@ -0,0 +1,15 @@ +package dev.inmo.tgbotapi.webapps.haptic + +import dev.inmo.micro_utils.common.Warning +import kotlinx.serialization.Serializable + +@Serializable +value class HapticFeedbackType @Warning("Do not use this constructor if available objects from companion cover your needs") constructor( + val name: String +) { + companion object { + val Error = HapticFeedbackType("error") + val Success = HapticFeedbackType("success") + val Warning = HapticFeedbackType("warning") + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceClosedInfo.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceClosedInfo.kt new file mode 100644 index 0000000000..39feb3087a --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceClosedInfo.kt @@ -0,0 +1,16 @@ +package dev.inmo.tgbotapi.webapps.invoice + +external interface InvoiceClosedInfo { + val url: String + val status: String +} + +val InvoiceClosedInfo.statusTyped + get() = when (status) { + InvoiceStatus.Paid.name -> InvoiceStatus.Paid + InvoiceStatus.Cancelled.name -> InvoiceStatus.Cancelled + InvoiceStatus.Failed.name -> InvoiceStatus.Failed + InvoiceStatus.Pending.name -> InvoiceStatus.Pending + else -> InvoiceStatus.Unknown(status) + } + diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceStatus.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceStatus.kt new file mode 100644 index 0000000000..6f685021d7 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/invoice/InvoiceStatus.kt @@ -0,0 +1,10 @@ +package dev.inmo.tgbotapi.webapps.invoice + +sealed interface InvoiceStatus { + val name: String + object Paid : InvoiceStatus { override val name: String = "paid" } + object Cancelled : InvoiceStatus { override val name: String = "cancelled" } + object Failed : InvoiceStatus { override val name: String = "failed" } + object Pending : InvoiceStatus { override val name: String = "pending" } + value class Unknown(override val name: String) : InvoiceStatus +} From 04db76831f55b388451b50be6915a96f6e85bcd9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 17:34:14 +0600 Subject: [PATCH 3/9] add support of premium fields --- CHANGELOG.md | 1 + .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 4 ++++ .../dev/inmo/tgbotapi/types/chat/Abstracts.kt | 5 +++++ .../kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt | 6 +++++- .../inmo/tgbotapi/types/chat/ExtendedAbstracts.kt | 10 ++++++++++ .../kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt | 6 ++++-- .../kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt | 13 ++++++++++++- 7 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 979a83195a..60577f3ea3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 2.1.0 * Add support of functionality for `WebApp`s from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) +* Add support of functionality for premium feature from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) ## 2.0.3 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index ccebdf3348..d0611ec4f3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -117,6 +117,7 @@ const val isBotField = "is_bot" const val firstNameField = "first_name" const val lastNameField = "last_name" const val languageCodeField = "language_code" +const val isPremiumField = "is_premium" const val hasPrivateForwardsField = "has_private_forwards" const val canJoinGroupsField = "can_join_groups" const val canReadAllGroupMessagesField = "can_read_all_group_messages" @@ -124,6 +125,7 @@ const val supportInlineQueriesField = "supports_inline_queries" const val textEntitiesField = "text_entities" const val entitiesField = "entities" const val stickerSetNameField = "set_name" +const val premiumAnimationField = "premium_animation" const val stickerSetNameFullField = "sticker_set_name" const val slowModeDelayField = "slow_mode_delay" const val maskPositionField = "mask_position" @@ -180,6 +182,8 @@ const val customTitleField = "custom_title" const val optionIdsField = "option_ids" const val ipAddressField = "ip_address" const val linkedChatIdField = "linked_chat_id" +const val joinToSendMessagesField = "join_to_send_messages" +const val joinByRequestField = "join_by_request" const val horizontalAccuracyField = "horizontal_accuracy" const val revokeMessagesField = "revoke_messages" const val messageAutoDeleteTimeField = "message_auto_delete_time" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt index 3391dc1668..aff6417c10 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt @@ -32,6 +32,11 @@ sealed interface UsernameChat : Chat { val username: Username? } +@Serializable(PreviewChatSerializer::class) +sealed interface PremiumChat : Chat { + val isPremium: Boolean +} + @Serializable(PreviewChatSerializer::class) sealed interface Chat { val id: ChatId diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt index 484300e31f..957623cebd 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt @@ -95,7 +95,11 @@ data class ExtendedSupergroupChatImpl( @SerialName(linkedChatIdField) override val linkedChannelChatId: ChatId? = null, @SerialName(locationField) - override val location: ChatLocation? = null + override val location: ChatLocation? = null, + @SerialName(joinToSendMessagesField) + override val requireToJoinForMessaging: Boolean = false, + @SerialName(joinByRequestField) + override val requireAdminApproveOnJoin: Boolean = false ) : ExtendedSupergroupChat @Serializable diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt index 91af9e1e33..6cb3e3979a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt @@ -39,6 +39,16 @@ sealed interface ExtendedSupergroupChat : SupergroupChat, ExtendedGroupChat { val canSetStickerSet: Boolean val linkedChannelChatId: ChatId? val location: ChatLocation? + + /** + * This field represents field "join_to_send_messages" from API + */ + val requireToJoinForMessaging: Boolean + + /** + * This field represents field "join_by_request" from API + */ + val requireAdminApproveOnJoin: Boolean } @Serializable(ExtendedChatSerializer::class) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt index 81c4fd2d5b..9f833a3c8e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt @@ -80,8 +80,10 @@ data class CommonUser( override val username: Username? = null, @SerialName(languageCodeField) @Serializable(IetfLanguageCodeSerializer::class) - override val ietfLanguageCode: IetfLanguageCode? = null -) : User(), WithOptionalLanguageCode { + override val ietfLanguageCode: IetfLanguageCode? = null, + @SerialName(isPremiumField) + override val isPremium: Boolean = false +) : User(), WithOptionalLanguageCode, PremiumChat { constructor( id: UserId, firstName: String, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt index e9832c00c2..d1f8e484b9 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/Sticker.kt @@ -22,7 +22,8 @@ data class StickerSurrogate( val emoji: String? = null, val set_name: StickerSetName? = null, val mask_position: MaskPosition? = null, - val file_size: Long? = null + val file_size: Long? = null, + val premium_animation: File? = null ) // TODO:: Serializer @@ -31,6 +32,7 @@ sealed interface Sticker : TelegramMediaFile, SizedMediaFile, ThumbedMediaFile { val emoji: String? val maskPosition: MaskPosition? val stickerSetName: StickerSetName? + val premiumAnimationFile: File? val isAnimated get() = this is AnimatedSticker @@ -53,6 +55,7 @@ object StickerSerializer : KSerializer { surrogate.thumb, surrogate.emoji, surrogate.set_name, + surrogate.premium_animation, surrogate.mask_position, surrogate.file_size ) @@ -64,6 +67,7 @@ object StickerSerializer : KSerializer { surrogate.thumb, surrogate.emoji, surrogate.set_name, + surrogate.premium_animation, surrogate.mask_position, surrogate.file_size ) @@ -75,6 +79,7 @@ object StickerSerializer : KSerializer { surrogate.thumb, surrogate.emoji, surrogate.set_name, + surrogate.premium_animation, surrogate.mask_position, surrogate.file_size ) @@ -103,6 +108,8 @@ data class SimpleSticker( override val emoji: String? = null, @SerialName(stickerSetNameField) override val stickerSetName: StickerSetName? = null, + @SerialName(premiumAnimationField) + override val premiumAnimationFile: File?, @SerialName(maskPositionField) override val maskPosition: MaskPosition? = null, @SerialName(fileSizeField) @@ -124,6 +131,8 @@ data class AnimatedSticker( override val emoji: String? = null, @SerialName(stickerSetNameField) override val stickerSetName: StickerSetName? = null, + @SerialName(premiumAnimationField) + override val premiumAnimationFile: File?, @SerialName(maskPositionField) override val maskPosition: MaskPosition? = null, @SerialName(fileSizeField) @@ -145,6 +154,8 @@ data class VideoSticker( override val emoji: String? = null, @SerialName(stickerSetNameField) override val stickerSetName: StickerSetName? = null, + @SerialName(premiumAnimationField) + override val premiumAnimationFile: File?, @SerialName(maskPositionField) override val maskPosition: MaskPosition? = null, @SerialName(fileSizeField) From 8dfaca76482805f3e294f749ab05ddf02710df29 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 17:52:16 +0600 Subject: [PATCH 4/9] CommonUser#addedToAttachmentMenu --- CHANGELOG.md | 1 + .../src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt | 1 + .../kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt | 5 +++++ .../commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt | 6 ++++-- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60577f3ea3..b07993269b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Add support of functionality for `WebApp`s from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) * Add support of functionality for premium feature from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) +* Add support of `addedToAttachmentMenu` in `CommonUser` from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) ## 2.0.3 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index d0611ec4f3..4d2b181554 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -117,6 +117,7 @@ const val isBotField = "is_bot" const val firstNameField = "first_name" const val lastNameField = "last_name" const val languageCodeField = "language_code" +const val addedToAttachmentMenuField = "added_to_attachment_menu" const val isPremiumField = "is_premium" const val hasPrivateForwardsField = "has_private_forwards" const val canJoinGroupsField = "can_join_groups" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt index aff6417c10..5ac0c94c8c 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt @@ -37,6 +37,11 @@ sealed interface PremiumChat : Chat { val isPremium: Boolean } +@Serializable(PreviewChatSerializer::class) +sealed interface AbleToAddInAttachmentMenuChat : Chat { + val addedToAttachmentMenu: Boolean +} + @Serializable(PreviewChatSerializer::class) sealed interface Chat { val id: ChatId diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt index 9f833a3c8e..aec27aa903 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt @@ -82,8 +82,10 @@ data class CommonUser( @Serializable(IetfLanguageCodeSerializer::class) override val ietfLanguageCode: IetfLanguageCode? = null, @SerialName(isPremiumField) - override val isPremium: Boolean = false -) : User(), WithOptionalLanguageCode, PremiumChat { + override val isPremium: Boolean = false, + @SerialName(addedToAttachmentMenuField) + override val addedToAttachmentMenu: Boolean = false +) : User(), WithOptionalLanguageCode, PremiumChat, AbleToAddInAttachmentMenuChat { constructor( id: UserId, firstName: String, From 1df653f42890890e22b92736bc31f758e70c1d01 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 17:55:21 +0600 Subject: [PATCH 5/9] SetWebhook#secretToken --- CHANGELOG.md | 1 + .../extensions/api/webhook/SetWebhookInfo.kt | 15 ++++---- .../tgbotapi/requests/webhook/SetWebhook.kt | 35 ++++++++++++------- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 1 + 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b07993269b..0daed1b25d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Add support of functionality for `WebApp`s from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) * Add support of functionality for premium feature from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) * Add support of `addedToAttachmentMenu` in `CommonUser` from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) +* Add support of `secret_token` in `SetWebhook` request from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) ## 2.0.3 diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/webhook/SetWebhookInfo.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/webhook/SetWebhookInfo.kt index bf2730daae..a0e9dcb195 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/webhook/SetWebhookInfo.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/webhook/SetWebhookInfo.kt @@ -13,10 +13,11 @@ suspend fun TelegramBot.setWebhookInfo( ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, - dropPendingUpdates: Boolean? = null + dropPendingUpdates: Boolean? = null, + secretToken: String? = null ) = execute( SetWebhook( - url, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates + url, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates, secretToken ) ) @@ -29,10 +30,11 @@ suspend fun TelegramBot.setWebhookInfo( ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, - dropPendingUpdates: Boolean? = null + dropPendingUpdates: Boolean? = null, + secretToken: String? = null ) = execute( SetWebhook( - url, certificate, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates + url, certificate, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates, secretToken ) ) @@ -45,9 +47,10 @@ suspend fun TelegramBot.setWebhookInfo( ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, - dropPendingUpdates: Boolean? = null + dropPendingUpdates: Boolean? = null, + secretToken: String? = null ) = execute( SetWebhook( - url, certificate, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates + url, certificate, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates, secretToken ) ) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt index 087ec7723b..39482247c5 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt @@ -20,7 +20,8 @@ class MultipartSetWebhookRequest( ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, - dropPendingUpdates: Boolean? = null + dropPendingUpdates: Boolean? = null, + secretToken: String? = null ) : SetWebhookRequest(), MultipartRequest by MultipartRequestImpl( SetWebhook( correctWebhookUrl(url), @@ -28,7 +29,8 @@ class MultipartSetWebhookRequest( ipAddress, maxAllowedConnections, allowedUpdates, - dropPendingUpdates + dropPendingUpdates, + secretToken ), mapOf(certificateField to certificate) ) @@ -39,14 +41,16 @@ fun SetWebhook( ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, - dropPendingUpdates: Boolean? = null + dropPendingUpdates: Boolean? = null, + secretToken: String? = null ): MultipartSetWebhookRequest = MultipartSetWebhookRequest( correctWebhookUrl(url), certificate, ipAddress, maxAllowedConnections, allowedUpdates, - dropPendingUpdates + dropPendingUpdates, + secretToken ) fun SetWebhook( @@ -55,14 +59,16 @@ fun SetWebhook( ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, - dropPendingUpdates: Boolean? = null + dropPendingUpdates: Boolean? = null, + secretToken: String? = null ): SetWebhook = SetWebhook( correctWebhookUrl(url), certificate.fileId, ipAddress, maxAllowedConnections, allowedUpdates, - dropPendingUpdates + dropPendingUpdates, + secretToken ) /** @@ -79,10 +85,11 @@ fun SetWebhook( ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, - dropPendingUpdates: Boolean? = null + dropPendingUpdates: Boolean? = null, + secretToken: String? = null ) = when (certificate) { - is MultipartFile -> SetWebhook(correctWebhookUrl(url), certificate as MultipartFile, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates) - is FileId -> SetWebhook(correctWebhookUrl(url), certificate as FileId, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates) + is MultipartFile -> SetWebhook(correctWebhookUrl(url), certificate as MultipartFile, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates, secretToken) + is FileId -> SetWebhook(correctWebhookUrl(url), certificate as FileId, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates, secretToken) } /** @@ -98,14 +105,16 @@ fun SetWebhook( ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, - dropPendingUpdates: Boolean? = null + dropPendingUpdates: Boolean? = null, + secretToken: String? = null ) = SetWebhook( correctWebhookUrl(url), null, ipAddress, maxAllowedConnections, allowedUpdates, - dropPendingUpdates + dropPendingUpdates, + secretToken ) /** @@ -128,7 +137,9 @@ data class SetWebhook internal constructor( @SerialName(allowedUpdatesField) val allowedUpdates: List? = null, @SerialName(dropPendingUpdatesField) - val dropPendingUpdates: Boolean? = null + val dropPendingUpdates: Boolean? = null, + @SerialName(secretTokenField) + val secretToken: String? = null ) : SetWebhookRequest(), DataRequest { override fun method(): String = "setWebhook" override val resultDeserializer: DeserializationStrategy diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 4d2b181554..33fa728067 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -158,6 +158,7 @@ const val switchPmParameterField = "switch_pm_parameter" const val maxAllowedConnectionsField = "max_connections" const val allowedUpdatesField = "allowed_updates" const val dropPendingUpdatesField = "drop_pending_updates" +const val secretTokenField = "secret_token" const val hasCustomCertificateField = "has_custom_certificate" const val pendingUpdateCountField = "pending_update_count" const val lastErrorDateField = "last_error_date" From b0c6b80d8bb87fae96bd43a3647e2d999ca59f3b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 18:30:31 +0600 Subject: [PATCH 6/9] small improvements --- .../dev/inmo/tgbotapi/types/chat/Extended.kt | 4 ++-- .../tgbotapi/types/chat/ExtendedAbstracts.kt | 5 ++--- .../tgbotapi/extensions/utils/ClassCasts.kt | 18 ++++++++++++++++++ .../extensions/utils/extensions/raw/Sticker.kt | 2 ++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt index 957623cebd..fed027677d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt @@ -97,9 +97,9 @@ data class ExtendedSupergroupChatImpl( @SerialName(locationField) override val location: ChatLocation? = null, @SerialName(joinToSendMessagesField) - override val requireToJoinForMessaging: Boolean = false, + override val requiresJoinForMessaging: Boolean = false, @SerialName(joinByRequestField) - override val requireAdminApproveOnJoin: Boolean = false + override val requireAdminApproveToJoin: Boolean = false ) : ExtendedSupergroupChat @Serializable diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt index 6cb3e3979a..26cf8115ae 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ExtendedAbstracts.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.types.chat import dev.inmo.tgbotapi.types.* -import dev.inmo.tgbotapi.types.chat.ExtendedChat import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializer import kotlinx.serialization.Serializable @@ -43,12 +42,12 @@ sealed interface ExtendedSupergroupChat : SupergroupChat, ExtendedGroupChat { /** * This field represents field "join_to_send_messages" from API */ - val requireToJoinForMessaging: Boolean + val requiresJoinForMessaging: Boolean /** * This field represents field "join_by_request" from API */ - val requireAdminApproveOnJoin: Boolean + val requireAdminApproveToJoin: Boolean } @Serializable(ExtendedChatSerializer::class) diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index 9751afd056..0ea185d5b1 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -235,6 +235,24 @@ inline fun Chat.asExtendedSupergroupChat(): ExtendedSupergroupChat? = this as? E @PreviewFeature inline fun Chat.requireExtendedSupergroupChat(): ExtendedSupergroupChat = this as ExtendedSupergroupChat +@PreviewFeature +inline fun Chat.whenPremiumChat(block: (PremiumChat) -> T) = asPremiumChat() ?.let(block) + +@PreviewFeature +inline fun Chat.asPremiumChat(): PremiumChat? = this as? PremiumChat + +@PreviewFeature +inline fun Chat.requirePremiumChat(): PremiumChat = this as PremiumChat + +@PreviewFeature +inline fun Chat.whenAbleToAddInAttachmentMenuChat(block: (AbleToAddInAttachmentMenuChat) -> T) = asAbleToAddInAttachmentMenuChat() ?.let(block) + +@PreviewFeature +inline fun Chat.asAbleToAddInAttachmentMenuChat(): AbleToAddInAttachmentMenuChat? = this as? AbleToAddInAttachmentMenuChat + +@PreviewFeature +inline fun Chat.requireAbleToAddInAttachmentMenuChat(): AbleToAddInAttachmentMenuChat = this as AbleToAddInAttachmentMenuChat + @PreviewFeature inline fun CallbackQuery.whenDataCallbackQuery(block: (DataCallbackQuery) -> T) = asDataCallbackQuery() ?.let(block) diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Sticker.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Sticker.kt index 4a399695be..fadb73ea4d 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Sticker.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Sticker.kt @@ -19,3 +19,5 @@ inline val Sticker.mask_position: MaskPosition? get() = maskPosition inline val Sticker.file_size: Long? get() = fileSize +inline val Sticker.premium_animation: File? + get() = premiumAnimationFile From f86a095421a83975ad3cbf818d4a832ba2f134ee Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 18:52:55 +0600 Subject: [PATCH 7/9] CreateInvoiceLink --- CHANGELOG.md | 1 + .../api/send/payments/CreateInvoiceLink.kt | 31 ++++++ .../send/payments/CreateInvoiceLink.kt | 105 ++++++++++++++++++ .../tgbotapi/extensions/utils/ClassCasts.kt | 12 +- 4 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/CreateInvoiceLink.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/CreateInvoiceLink.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 0daed1b25d..86a05f8b6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Add support of functionality for premium feature from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) * Add support of `addedToAttachmentMenu` in `CommonUser` from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) * Add support of `secret_token` in `SetWebhook` request from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) +* Add support of `createInvoiceLink` request from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) ## 2.0.3 diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/CreateInvoiceLink.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/CreateInvoiceLink.kt new file mode 100644 index 0000000000..00ae87149c --- /dev/null +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/payments/CreateInvoiceLink.kt @@ -0,0 +1,31 @@ +package dev.inmo.tgbotapi.extensions.api.send.payments + +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.requests.send.payments.CreateInvoiceLink +import dev.inmo.tgbotapi.requests.send.payments.SendInvoice +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup +import dev.inmo.tgbotapi.types.chat.CommonUser +import dev.inmo.tgbotapi.types.payments.LabeledPrice +import dev.inmo.tgbotapi.types.payments.abstracts.Currency + +suspend fun TelegramBot.createInvoiceLink( + title: String, + description: String, + payload: String, + providerToken: String, + currency: Currency, + prices: List, + maxTipAmount: Int? = null, + suggestedTipAmounts: List? = null, + providerData: String? = null, + requireName: Boolean = false, + requirePhoneNumber: Boolean = false, + requireEmail: Boolean = false, + requireShippingAddress: Boolean = false, + shouldSendPhoneNumberToProvider: Boolean = false, + shouldSendEmailToProvider: Boolean = false, + priceDependOnShipAddress: Boolean = false +) = execute( + CreateInvoiceLink(title, description, payload, providerToken, currency, prices, maxTipAmount, suggestedTipAmounts ?.sorted(), providerData, requireName, requirePhoneNumber, requireEmail, requireShippingAddress, shouldSendPhoneNumberToProvider, shouldSendEmailToProvider, priceDependOnShipAddress) +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/CreateInvoiceLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/CreateInvoiceLink.kt new file mode 100644 index 0000000000..19a932e9d6 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/payments/CreateInvoiceLink.kt @@ -0,0 +1,105 @@ +package dev.inmo.tgbotapi.requests.send.payments + +import dev.inmo.tgbotapi.abstracts.CommonSendInvoiceData +import dev.inmo.tgbotapi.abstracts.types.* +import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest +import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup +import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage +import dev.inmo.tgbotapi.types.message.content.InvoiceContent +import dev.inmo.tgbotapi.types.payments.LabeledPrice +import dev.inmo.tgbotapi.types.payments.LabeledPricesSerializer +import dev.inmo.tgbotapi.types.payments.abstracts.Currency +import kotlinx.serialization.* +import kotlinx.serialization.builtins.serializer + +/** + * @param providerData - JSON-ENCODED FIELD + */ +@Serializable +data class CreateInvoiceLink( + @SerialName(titleField) + override val title: String, + @SerialName(descriptionField) + override val description: String, + @SerialName(payloadField) + override val payload: String, + @SerialName(providerTokenField) + override val providerToken: String, + @SerialName(currencyField) + override val currency: Currency, + @Serializable(LabeledPricesSerializer::class) + @SerialName(pricesField) + override val prices: List, + @SerialName(maxTipAmountField) + override val maxTipAmount: Int? = null, + @SerialName(suggestedTipAmountsField) + override val suggestedTipAmounts: List? = null, + @SerialName(providerDataField) + override val providerData: String? = null, + @SerialName(requireNameField) + override val requireName: Boolean = false, + @SerialName(requirePhoneNumberField) + override val requirePhoneNumber: Boolean = false, + @SerialName(requireEmailField) + override val requireEmail: Boolean = false, + @SerialName(requireShippingAddressField) + override val requireShippingAddress: Boolean = false, + @SerialName(shouldSendPhoneNumberToProviderField) + override val shouldSendPhoneNumberToProvider: Boolean = false, + @SerialName(shouldSendEmailToProviderField) + override val shouldSendEmailToProvider: Boolean = false, + @SerialName(priceDependOnShipAddressField) + override val priceDependOnShipAddress: Boolean = false +) : CommonSendInvoiceData, SimpleRequest { + override fun method(): String = "sendInvoice" + override val resultDeserializer: DeserializationStrategy + get() = String.serializer() + override val requestSerializer: SerializationStrategy<*> + get() = serializer() + + @SerialName(photoUrlField) + override var photoUrl: String? = null + private set + @SerialName(photoSizeField) + override var photoSize: Long? = null + private set + + @SerialName(photoWidthField) + override var photoWidth: Int? = null + private set + @SerialName(photoHeightField) + override var photoHeight: Int? = null + private set + + init { + suggestedTipAmounts ?.let { _ -> + require(suggestedTipAmounts.size in suggestedTipAmountsLimit) + maxTipAmount ?.let { _ -> + require( + suggestedTipAmounts.none { it > maxTipAmount } + ) + } + } + } + + override fun setPhoto( + photoUrl: String, + photoSize: Long?, + photoWidth: Int?, + photoHeight: Int? + ) { + this.photoUrl = photoUrl + this.photoSize = photoSize + this.photoWidth = photoWidth + this.photoHeight = photoHeight + } + + override fun unsetPhoto() { + photoUrl = null + photoSize = null + photoWidth = null + photoHeight = null + } +} diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index 0ea185d5b1..b887e80a70 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.extensions.utils import dev.inmo.tgbotapi.abstracts.* +import dev.inmo.tgbotapi.requests.send.payments.CreateInvoiceLink import dev.inmo.tgbotapi.requests.send.payments.SendInvoice import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.queries.callback.* @@ -3273,7 +3274,16 @@ inline fun CommonSendInvoiceData.whenSendInvoice(block: (SendInvoice) -> T) inline fun CommonSendInvoiceData.asSendInvoice(): SendInvoice? = this as? SendInvoice @PreviewFeature -inline fun CommonSendInvoiceData.requireVoiceChatParticipantsInvited(): SendInvoice = this as SendInvoice +inline fun CommonSendInvoiceData.requireSendInvoice(): SendInvoice = this as SendInvoice + +@PreviewFeature +inline fun CommonSendInvoiceData.whenCreateInvoiceLink(block: (CreateInvoiceLink) -> T) = asCreateInvoiceLink() ?.let(block) + +@PreviewFeature +inline fun CommonSendInvoiceData.asCreateInvoiceLink(): CreateInvoiceLink? = this as? CreateInvoiceLink + +@PreviewFeature +inline fun CommonSendInvoiceData.requireCreateInvoiceLink(): CreateInvoiceLink = this as CreateInvoiceLink @PreviewFeature inline fun CommonSendInvoiceData.whenInputInvoiceMessageContent(block: (InputInvoiceMessageContent) -> T) = asInputInvoiceMessageContent() ?.let(block) From 9f3cc6eafb856017f86fae95c752b1fbbccd6386 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 18:58:01 +0600 Subject: [PATCH 8/9] add mention about 6.1 telegram bot api support --- CHANGELOG.md | 2 ++ README.md | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86a05f8b6a..73a142aab0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 2.1.0 +__This update contains including of [Telegram Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022)__ + * Add support of functionality for `WebApp`s from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) * Add support of functionality for premium feature from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) * Add support of `addedToAttachmentMenu` in `CommonUser` from [Bot API 6.1](https://core.telegram.org/bots/api-changelog#june-20-2022) diff --git a/README.md b/README.md index 9da7c4e6e6..f1ec59bad9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# TelegramBotAPI [![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi) [![Supported version](https://img.shields.io/badge/Telegram%20Bot%20API-6.0-blue)](https://core.telegram.org/bots/api-changelog#april-16-2022) +# TelegramBotAPI [![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi) [![Supported version](https://img.shields.io/badge/Telegram%20Bot%20API-6.1-blue)](https://core.telegram.org/bots/api-changelog#june-20-2022) | [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Build Status](https://github.com/InsanusMokrassar/TelegramBotAPI/workflows/Build/badge.svg)](https://github.com/InsanusMokrassar/TelegramBotAPI/actions) [![Small survey](https://img.shields.io/static/v1?label=Google&message=Survey&color=blue&logo=google-sheets)](https://docs.google.com/forms/d/e/1FAIpQLSctdJHT_aEniyYT0-IUAEfo1hsIlezX2owlkEAYX4KPl2V2_A/viewform?usp=sf_link) [![Chat in Telegram](https://img.shields.io/static/v1?label=Telegram&message=Chat&color=blue&logo=telegram)](https://t.me/InMoTelegramBotAPI) | |:---:| From d20b94e52c0d6f57812a61424e60c2841257da7e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 23:06:36 +0600 Subject: [PATCH 9/9] rename PremiumChat -> PossiblyPremiumChat --- .../kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt | 2 +- .../commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt | 2 +- .../kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt index 5ac0c94c8c..0d6bf752e1 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt @@ -33,7 +33,7 @@ sealed interface UsernameChat : Chat { } @Serializable(PreviewChatSerializer::class) -sealed interface PremiumChat : Chat { +sealed interface PossiblyPremiumChat : Chat { val isPremium: Boolean } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt index aec27aa903..ac61c30301 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt @@ -85,7 +85,7 @@ data class CommonUser( override val isPremium: Boolean = false, @SerialName(addedToAttachmentMenuField) override val addedToAttachmentMenu: Boolean = false -) : User(), WithOptionalLanguageCode, PremiumChat, AbleToAddInAttachmentMenuChat { +) : User(), WithOptionalLanguageCode, PossiblyPremiumChat, AbleToAddInAttachmentMenuChat { constructor( id: UserId, firstName: String, diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index b887e80a70..a8464c167e 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -237,13 +237,13 @@ inline fun Chat.asExtendedSupergroupChat(): ExtendedSupergroupChat? = this as? E inline fun Chat.requireExtendedSupergroupChat(): ExtendedSupergroupChat = this as ExtendedSupergroupChat @PreviewFeature -inline fun Chat.whenPremiumChat(block: (PremiumChat) -> T) = asPremiumChat() ?.let(block) +inline fun Chat.whenPossiblyPremiumChat(block: (PossiblyPremiumChat) -> T) = asPossiblyPremiumChat() ?.let(block) @PreviewFeature -inline fun Chat.asPremiumChat(): PremiumChat? = this as? PremiumChat +inline fun Chat.asPossiblyPremiumChat(): PossiblyPremiumChat? = this as? PossiblyPremiumChat @PreviewFeature -inline fun Chat.requirePremiumChat(): PremiumChat = this as PremiumChat +inline fun Chat.requirePossiblyPremiumChat(): PossiblyPremiumChat = this as PossiblyPremiumChat @PreviewFeature inline fun Chat.whenAbleToAddInAttachmentMenuChat(block: (AbleToAddInAttachmentMenuChat) -> T) = asAbleToAddInAttachmentMenuChat() ?.let(block)