From 1ab285eff8326293265fb316d6823265df977a33 Mon Sep 17 00:00:00 2001 From: nerzh Date: Wed, 14 Aug 2024 15:55:43 +0200 Subject: [PATCH] Bot API 7.9 --- Sources/SwiftTelegramSdk/Bot/TGBotPrtcl.swift | 10 +++ .../CreateChatSubscriptionInviteLink.swift | 63 +++++++++++++++++++ .../EditChatSubscriptionInviteLink.swift | 58 +++++++++++++++++ .../Bot/Telegram/Methods/EditForumTopic.swift | 4 +- .../Methods/EditGeneralForumTopic.swift | 4 +- .../Bot/Telegram/Methods/SendPaidMedia.swift | 13 ++-- .../Telegram/Methods/SetMessageReaction.swift | 6 +- .../Telegram/Models/TGChatMemberMember.swift | 7 ++- .../Bot/Telegram/Models/TGMessage.swift | 4 +- .../Bot/Telegram/Models/TGReactionType.swift | 6 ++ .../Telegram/Models/TGReactionTypePaid.swift | 22 +++++++ .../Models/TGReactionTypePaidType.swift | 22 +++++++ .../Models/TGTransactionPartnerUser.swift | 7 ++- 13 files changed, 211 insertions(+), 15 deletions(-) create mode 100644 Sources/SwiftTelegramSdk/Bot/Telegram/Methods/CreateChatSubscriptionInviteLink.swift create mode 100644 Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditChatSubscriptionInviteLink.swift create mode 100644 Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionTypePaid.swift create mode 100644 Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionTypePaidType.swift diff --git a/Sources/SwiftTelegramSdk/Bot/TGBotPrtcl.swift b/Sources/SwiftTelegramSdk/Bot/TGBotPrtcl.swift index 30a8858..0e107d3 100644 --- a/Sources/SwiftTelegramSdk/Bot/TGBotPrtcl.swift +++ b/Sources/SwiftTelegramSdk/Bot/TGBotPrtcl.swift @@ -222,6 +222,16 @@ public protocol TGBotPrtcl { + @discardableResult + func createChatSubscriptionInviteLink(params: TGCreateChatSubscriptionInviteLinkParams) async throws -> Bool + + + + @discardableResult + func editChatSubscriptionInviteLink(params: TGEditChatSubscriptionInviteLinkParams) async throws -> Bool + + + @discardableResult func revokeChatInviteLink(params: TGRevokeChatInviteLinkParams) async throws -> Bool diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/CreateChatSubscriptionInviteLink.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/CreateChatSubscriptionInviteLink.swift new file mode 100644 index 0000000..4d51cd7 --- /dev/null +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/CreateChatSubscriptionInviteLink.swift @@ -0,0 +1,63 @@ +// Swift Telegram SDK - Telegram Bot Swift SDK. + +import Foundation + +/// DESCRIPTION: +/// Use this method to create a subscription invite link for a channel chat. The bot must have the can_invite_users administrator rights. The link can be edited using the method editChatSubscriptionInviteLink or revoked using the method revokeChatInviteLink. Returns the new invite link as a ChatInviteLink object. + + +/// Parameters container struct for `createChatSubscriptionInviteLink` method +public struct TGCreateChatSubscriptionInviteLinkParams: Encodable { + + /// Unique identifier for the target channel chat or username of the target channel (in the format @channelusername) + public var chatId: TGChatId + + /// Invite link name; 0-32 characters + public var name: String? + + /// The number of seconds the subscription will be active for before the next payment. Currently, it must always be 2592000 (30 days). + public var subscriptionPeriod: Int + + /// The amount of Telegram Stars a user must pay initially and after each subsequent subscription period to be a member of the chat; 1-2500 + public var subscriptionPrice: Int + + /// Custom keys for coding/decoding `CreateChatSubscriptionInviteLinkParams` struct + public enum CodingKeys: String, CodingKey { + case chatId = "chat_id" + case name = "name" + case subscriptionPeriod = "subscription_period" + case subscriptionPrice = "subscription_price" + } + + public init(chatId: TGChatId, name: String? = nil, subscriptionPeriod: Int, subscriptionPrice: Int) { + self.chatId = chatId + self.name = name + self.subscriptionPeriod = subscriptionPeriod + self.subscriptionPrice = subscriptionPrice + } +} + + +public extension TGBot { + +/** + Use this method to create a subscription invite link for a channel chat. The bot must have the can_invite_users administrator rights. The link can be edited using the method editChatSubscriptionInviteLink or revoked using the method revokeChatInviteLink. Returns the new invite link as a ChatInviteLink object. + + SeeAlso Telegram Bot API Reference: + [CreateChatSubscriptionInviteLinkParams](https://core.telegram.org/bots/api#createchatsubscriptioninvitelink) + + - Parameters: + - params: Parameters container, see `CreateChatSubscriptionInviteLinkParams` struct + - Throws: Throws on errors + - Returns: `Bool` + */ + + @discardableResult + func createChatSubscriptionInviteLink(params: TGCreateChatSubscriptionInviteLinkParams) async throws -> Bool { + guard let methodURL: URL = .init(string: getMethodURL("createChatSubscriptionInviteLink")) else { + throw BotError("Bad URL: \(getMethodURL("createChatSubscriptionInviteLink"))") + } + let result: Bool = try await tgClient.post(methodURL, params: params, as: nil) + return result + } +} diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditChatSubscriptionInviteLink.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditChatSubscriptionInviteLink.swift new file mode 100644 index 0000000..f5ddcd1 --- /dev/null +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditChatSubscriptionInviteLink.swift @@ -0,0 +1,58 @@ +// Swift Telegram SDK - Telegram Bot Swift SDK. + +import Foundation + +/// DESCRIPTION: +/// Use this method to edit a subscription invite link created by the bot. The bot must have the can_invite_users administrator rights. Returns the edited invite link as a ChatInviteLink object. + + +/// Parameters container struct for `editChatSubscriptionInviteLink` method +public struct TGEditChatSubscriptionInviteLinkParams: Encodable { + + /// Unique identifier for the target chat or username of the target channel (in the format @channelusername) + public var chatId: TGChatId + + /// The invite link to edit + public var inviteLink: String + + /// Invite link name; 0-32 characters + public var name: String? + + /// Custom keys for coding/decoding `EditChatSubscriptionInviteLinkParams` struct + public enum CodingKeys: String, CodingKey { + case chatId = "chat_id" + case inviteLink = "invite_link" + case name = "name" + } + + public init(chatId: TGChatId, inviteLink: String, name: String? = nil) { + self.chatId = chatId + self.inviteLink = inviteLink + self.name = name + } +} + + +public extension TGBot { + +/** + Use this method to edit a subscription invite link created by the bot. The bot must have the can_invite_users administrator rights. Returns the edited invite link as a ChatInviteLink object. + + SeeAlso Telegram Bot API Reference: + [EditChatSubscriptionInviteLinkParams](https://core.telegram.org/bots/api#editchatsubscriptioninvitelink) + + - Parameters: + - params: Parameters container, see `EditChatSubscriptionInviteLinkParams` struct + - Throws: Throws on errors + - Returns: `Bool` + */ + + @discardableResult + func editChatSubscriptionInviteLink(params: TGEditChatSubscriptionInviteLinkParams) async throws -> Bool { + guard let methodURL: URL = .init(string: getMethodURL("editChatSubscriptionInviteLink")) else { + throw BotError("Bad URL: \(getMethodURL("editChatSubscriptionInviteLink"))") + } + let result: Bool = try await tgClient.post(methodURL, params: params, as: nil) + return result + } +} diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditForumTopic.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditForumTopic.swift index 8b55f0b..cd54ef2 100644 --- a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditForumTopic.swift +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditForumTopic.swift @@ -3,7 +3,7 @@ import Foundation /// DESCRIPTION: -/// Use this method to edit name and icon of a topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights, unless it is the creator of the topic. Returns True on success. +/// Use this method to edit name and icon of a topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights, unless it is the creator of the topic. Returns True on success. /// Parameters container struct for `editForumTopic` method @@ -41,7 +41,7 @@ public struct TGEditForumTopicParams: Encodable { public extension TGBot { /** - Use this method to edit name and icon of a topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights, unless it is the creator of the topic. Returns True on success. + Use this method to edit name and icon of a topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights, unless it is the creator of the topic. Returns True on success. SeeAlso Telegram Bot API Reference: [EditForumTopicParams](https://core.telegram.org/bots/api#editforumtopic) diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditGeneralForumTopic.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditGeneralForumTopic.swift index 1a246a2..2842347 100644 --- a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditGeneralForumTopic.swift +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/EditGeneralForumTopic.swift @@ -3,7 +3,7 @@ import Foundation /// DESCRIPTION: -/// Use this method to edit the name of the 'General' topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights. Returns True on success. +/// Use this method to edit the name of the 'General' topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. Returns True on success. /// Parameters container struct for `editGeneralForumTopic` method @@ -31,7 +31,7 @@ public struct TGEditGeneralForumTopicParams: Encodable { public extension TGBot { /** - Use this method to edit the name of the 'General' topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights. Returns True on success. + Use this method to edit the name of the 'General' topic in a forum supergroup chat. The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. Returns True on success. SeeAlso Telegram Bot API Reference: [EditGeneralForumTopicParams](https://core.telegram.org/bots/api#editgeneralforumtopic) diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/SendPaidMedia.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/SendPaidMedia.swift index 1b1949e..b9e0cf3 100644 --- a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/SendPaidMedia.swift +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/SendPaidMedia.swift @@ -3,13 +3,16 @@ import Foundation /// DESCRIPTION: -/// Use this method to send paid media to channel chats. On success, the sent Message is returned. +/// Use this method to send paid media. On success, the sent Message is returned. /// Parameters container struct for `sendPaidMedia` method public struct TGSendPaidMediaParams: Encodable { - /// Unique identifier for the target chat or username of the target channel (in the format @channelusername) + /// Unique identifier of the business connection on behalf of which the message will be sent + public var businessConnectionId: String? + + /// Unique identifier for the target chat or username of the target channel (in the format @channelusername). If the chat is a channel, all Telegram Star proceeds from this media will be credited to the chat's balance. Otherwise, they will be credited to the bot's balance. public var chatId: TGChatId /// The number of Telegram Stars that must be paid to buy access to the media @@ -44,6 +47,7 @@ public struct TGSendPaidMediaParams: Encodable { /// Custom keys for coding/decoding `SendPaidMediaParams` struct public enum CodingKeys: String, CodingKey { + case businessConnectionId = "business_connection_id" case chatId = "chat_id" case starCount = "star_count" case media = "media" @@ -57,7 +61,8 @@ public struct TGSendPaidMediaParams: Encodable { case replyMarkup = "reply_markup" } - public init(chatId: TGChatId, starCount: Int, media: [TGInputPaidMedia], caption: String? = nil, parseMode: TGParseMode? = nil, captionEntities: [TGMessageEntity]? = nil, showCaptionAboveMedia: Bool? = nil, disableNotification: Bool? = nil, protectContent: Bool? = nil, replyParameters: TGReplyParameters? = nil, replyMarkup: TGReplyMarkup? = nil) { + public init(businessConnectionId: String? = nil, chatId: TGChatId, starCount: Int, media: [TGInputPaidMedia], caption: String? = nil, parseMode: TGParseMode? = nil, captionEntities: [TGMessageEntity]? = nil, showCaptionAboveMedia: Bool? = nil, disableNotification: Bool? = nil, protectContent: Bool? = nil, replyParameters: TGReplyParameters? = nil, replyMarkup: TGReplyMarkup? = nil) { + self.businessConnectionId = businessConnectionId self.chatId = chatId self.starCount = starCount self.media = media @@ -76,7 +81,7 @@ public struct TGSendPaidMediaParams: Encodable { public extension TGBot { /** - Use this method to send paid media to channel chats. On success, the sent Message is returned. + Use this method to send paid media. On success, the sent Message is returned. SeeAlso Telegram Bot API Reference: [SendPaidMediaParams](https://core.telegram.org/bots/api#sendpaidmedia) diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/SetMessageReaction.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/SetMessageReaction.swift index 1114850..4452476 100644 --- a/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/SetMessageReaction.swift +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Methods/SetMessageReaction.swift @@ -3,7 +3,7 @@ import Foundation /// DESCRIPTION: -/// Use this method to change the chosen reactions on a message. Service messages can't be reacted to. Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel. Returns True on success. +/// Use this method to change the chosen reactions on a message. Service messages can't be reacted to. Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel. Bots can't use paid reactions. Returns True on success. /// Parameters container struct for `setMessageReaction` method @@ -15,7 +15,7 @@ public struct TGSetMessageReactionParams: Encodable { /// Identifier of the target message. If the message belongs to a media group, the reaction is set to the first non-deleted message in the group instead. public var messageId: Int - /// A JSON-serialized list of reaction types to set on the message. Currently, as non-premium users, bots can set up to one reaction per message. A custom emoji reaction can be used if it is either already present on the message or explicitly allowed by chat administrators. + /// A JSON-serialized list of reaction types to set on the message. Currently, as non-premium users, bots can set up to one reaction per message. A custom emoji reaction can be used if it is either already present on the message or explicitly allowed by chat administrators. Paid reactions can't be used by bots. public var reaction: [TGReactionType]? /// Pass True to set the reaction with a big animation @@ -41,7 +41,7 @@ public struct TGSetMessageReactionParams: Encodable { public extension TGBot { /** - Use this method to change the chosen reactions on a message. Service messages can't be reacted to. Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel. Returns True on success. + Use this method to change the chosen reactions on a message. Service messages can't be reacted to. Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel. Bots can't use paid reactions. Returns True on success. SeeAlso Telegram Bot API Reference: [SetMessageReactionParams](https://core.telegram.org/bots/api#setmessagereaction) diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGChatMemberMember.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGChatMemberMember.swift index 33e1ca7..28d9470 100644 --- a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGChatMemberMember.swift +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGChatMemberMember.swift @@ -12,6 +12,7 @@ public final class TGChatMemberMember: Codable { public enum CodingKeys: String, CodingKey { case status = "status" case user = "user" + case untilDate = "until_date" } /// The member's status in the chat, always “member” @@ -20,8 +21,12 @@ public final class TGChatMemberMember: Codable { /// Information about the user public var user: TGUser - public init (status: String, user: TGUser) { + /// Optional. Date when the user's subscription will expire; Unix time + public var untilDate: Int? + + public init (status: String, user: TGUser, untilDate: Int? = nil) { self.status = status self.user = user + self.untilDate = untilDate } } diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGMessage.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGMessage.swift index 16f3d3a..6572469 100644 --- a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGMessage.swift +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGMessage.swift @@ -103,10 +103,10 @@ public final class TGMessage: Codable { /// Optional. Unique identifier of a message thread to which the message belongs; for supergroups only public var messageThreadId: Int? - /// Optional. Sender of the message; empty for messages sent to channels. For backward compatibility, the field contains a fake sender user in non-channel chats, if the message was sent on behalf of a chat. + /// Optional. Sender of the message; may be empty for messages sent to channels. For backward compatibility, if the message was sent on behalf of a chat, the field contains a fake sender user in non-channel chats public var from: TGUser? - /// Optional. Sender of the message, sent on behalf of a chat. For example, the channel itself for channel posts, the supergroup itself for messages from anonymous group administrators, the linked channel for messages automatically forwarded to the discussion group. For backward compatibility, the field from contains a fake sender user in non-channel chats, if the message was sent on behalf of a chat. + /// Optional. Sender of the message when sent on behalf of a chat. For example, the supergroup itself for messages sent by its anonymous administrators or a linked channel for messages automatically forwarded to the channel's discussion group. For backward compatibility, if the message was sent on behalf of a chat, the field from contains a fake sender user in non-channel chats. public var senderChat: TGChat? /// Optional. If the sender of the message boosted the chat, the number of boosts added by the user diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionType.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionType.swift index 1842025..18690ce 100644 --- a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionType.swift +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionType.swift @@ -4,6 +4,7 @@ This object describes the type of a reaction. Currently, it can be one of ReactionTypeEmoji ReactionTypeCustomEmoji + ReactionTypePaid SeeAlso Telegram Bot API Reference: [ReactionType](https://core.telegram.org/bots/api#reactiontype) @@ -11,6 +12,7 @@ public enum TGReactionType: Codable { case reactionTypeEmoji(TGReactionTypeEmoji) case reactionTypeCustomEmoji(TGReactionTypeCustomEmoji) + case reactionTypePaid(TGReactionTypePaid) public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() @@ -18,6 +20,8 @@ public enum TGReactionType: Codable { self = .reactionTypeEmoji(value) } else if let value = try? container.decode(TGReactionTypeCustomEmoji.self) { self = .reactionTypeCustomEmoji(value) + } else if let value = try? container.decode(TGReactionTypePaid.self) { + self = .reactionTypePaid(value) } else { throw BotError("Failed! Can't decode ANY_TYPE ReactionType.") } @@ -30,6 +34,8 @@ public enum TGReactionType: Codable { try container.encode(value) case let .reactionTypeCustomEmoji(value): try container.encode(value) + case let .reactionTypePaid(value): + try container.encode(value) } } } diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionTypePaid.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionTypePaid.swift new file mode 100644 index 0000000..41459e9 --- /dev/null +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionTypePaid.swift @@ -0,0 +1,22 @@ +// Swift Telegram SDK - Telegram Bot Swift SDK. + +/** + The reaction is paid. + + SeeAlso Telegram Bot API Reference: + [ReactionTypePaid](https://core.telegram.org/bots/api#reactiontypepaid) + **/ +public final class TGReactionTypePaid: Codable { + + /// Custom keys for coding/decoding `ReactionTypePaid` struct + public enum CodingKeys: String, CodingKey { + case type = "type" + } + + /// Type of the reaction, always “paid” + public var type: TGReactionTypePaidType + + public init (type: TGReactionTypePaidType) { + self.type = type + } +} diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionTypePaidType.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionTypePaidType.swift new file mode 100644 index 0000000..0151655 --- /dev/null +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGReactionTypePaidType.swift @@ -0,0 +1,22 @@ +// Swift Telegram SDK - Telegram Bot Swift SDK. + +/** + The reaction is paid. + + SeeAlso Telegram Bot API Reference: + [ReactionTypePaid](https://core.telegram.org/bots/api#reactiontypepaid) + */ + +public enum TGReactionTypePaidType: String, Codable { + case paid = "paid" + case undefined + + public init(from decoder: Decoder) throws { + let value = try decoder.singleValueContainer().decode(String.self) + guard let type = TGReactionTypePaidType(rawValue: value) else { + self = .undefined + return + } + self = type + } +} \ No newline at end of file diff --git a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGTransactionPartnerUser.swift b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGTransactionPartnerUser.swift index 1aec81d..dcf1726 100644 --- a/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGTransactionPartnerUser.swift +++ b/Sources/SwiftTelegramSdk/Bot/Telegram/Models/TGTransactionPartnerUser.swift @@ -13,6 +13,7 @@ public final class TGTransactionPartnerUser: Codable { case type = "type" case user = "user" case invoicePayload = "invoice_payload" + case paidMedia = "paid_media" } /// Type of the transaction partner, always “user” @@ -24,9 +25,13 @@ public final class TGTransactionPartnerUser: Codable { /// Optional. Bot-specified invoice payload public var invoicePayload: String? - public init (type: TGTransactionPartnerUserType, user: TGUser, invoicePayload: String? = nil) { + /// Optional. Information about the paid media bought by the user + public var paidMedia: [TGPaidMedia]? + + public init (type: TGTransactionPartnerUserType, user: TGUser, invoicePayload: String? = nil, paidMedia: [TGPaidMedia]? = nil) { self.type = type self.user = user self.invoicePayload = invoicePayload + self.paidMedia = paidMedia } }