Skip to content

Commit

Permalink
try fix decode ANY_TYPE models
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzh committed Jan 17, 2024
1 parent d97d159 commit c4b8db2
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Api/generate_wrappers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def generate_fucking_telegram_any_type(type_name, var_protocol, description)
# throw Er(des: "")
# }
# }
out << "#{ONE}init(from decoder: Decoder) throws {\n"
out << "#{TWO}var container = try decoder.singleValueContainer()\n#{TWO}"
out << "#{ONE}public init(from decoder: Decoder) throws {\n"
out << "#{TWO}let container = try decoder.singleValueContainer()\n#{TWO}"
start_trigger = false
else_trigger = false
description.each_line do |line|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ public enum TGChatBoostSource: Codable {
case chatBoostSourcePremium(TGChatBoostSourcePremium)
case chatBoostSourceGiftCode(TGChatBoostSourceGiftCode)
case chatBoostSourceGiveaway(TGChatBoostSourceGiveaway)

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let value = try? container.decode(TGChatBoostSourcePremium.self) {
self = .chatBoostSourcePremium(value)
} else if let value = try? container.decode(TGChatBoostSourceGiftCode.self) {
self = .chatBoostSourceGiftCode(value)
} else if let value = try? container.decode(TGChatBoostSourceGiveaway.self) {
self = .chatBoostSourceGiveaway(value)
} else {
throw BotError("Failed! Can't decode ANY_TYPE ChatBoostSource.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class TGForceReply: Codable {
/// Optional. The placeholder to be shown in the input field when the reply is active; 1-64 characters
public var inputFieldPlaceholder: String?

/// Optional. Use this parameter if you want to force reply from specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
/// Optional. Use this parameter if you want to force reply from specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply to a message in the same chat and forum topic, sender of the original message.
public var selective: Bool?

public init (forceReply: Bool = true, inputFieldPlaceholder: String? = nil, selective: Bool? = nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class TGInlineKeyboardButton: Codable {
/// Label text on the button
public var text: String

/// Optional. HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id=<user_id> can be used to mention a user by their ID without using a username, if this is allowed by their privacy settings.
/// Optional. HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id=<user_id> can be used to mention a user by their identifier without using a username, if this is allowed by their privacy settings.
public var url: String?

/// Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@
public enum TGMaybeInaccessibleMessage: Codable {
case message(TGMessage)
case inaccessibleMessage(TGInaccessibleMessage)

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let value = try? container.decode(TGMessage.self) {
self = .message(value)
} else if let value = try? container.decode(TGInaccessibleMessage.self) {
self = .inaccessibleMessage(value)
} else {
throw BotError("Failed! Can't decode ANY_TYPE MaybeInaccessibleMessage.")
}
}
}
15 changes: 15 additions & 0 deletions Sources/TelegramVaporBot/Bot/Telegram/Models/TGMessageOrigin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ public enum TGMessageOrigin: Codable {
case messageOriginHiddenUser(TGMessageOriginHiddenUser)
case messageOriginChat(TGMessageOriginChat)
case messageOriginChannel(TGMessageOriginChannel)

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let value = try? container.decode(TGMessageOriginUser.self) {
self = .messageOriginUser(value)
} else if let value = try? container.decode(TGMessageOriginHiddenUser.self) {
self = .messageOriginHiddenUser(value)
} else if let value = try? container.decode(TGMessageOriginChat.self) {
self = .messageOriginChat(value)
} else if let value = try? container.decode(TGMessageOriginChannel.self) {
self = .messageOriginChannel(value)
} else {
throw BotError("Failed! Can't decode ANY_TYPE MessageOrigin.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class TGReplyKeyboardMarkup: Codable {
/// Optional. The placeholder to be shown in the input field when the keyboard is active; 1-64 characters
public var inputFieldPlaceholder: String?

/// Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
/// Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply to a message in the same chat and forum topic, sender of the original message.
///
/// Example: A user requests to change the bot's language, bot replies to the request with a keyboard to select the new language. Other users in the group don't see the keyboard.
public var selective: Bool?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class TGReplyKeyboardRemove: Codable {
/// Requests clients to remove the custom keyboard (user will not be able to summon this keyboard; if you want to hide the keyboard from sight but keep it accessible, use one_time_keyboard in ReplyKeyboardMarkup)
public var removeKeyboard: Bool = true

/// Optional. Use this parameter if you want to remove the keyboard for specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
/// Optional. Use this parameter if you want to remove the keyboard for specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply to a message in the same chat and forum topic, sender of the original message.
///
/// Example: A user votes in a poll, bot returns confirmation message in reply to the vote and removes the keyboard for that user, while still showing the keyboard with poll options to users who haven't voted yet.
public var selective: Bool?
Expand Down
8 changes: 4 additions & 4 deletions Sources/TelegramVaporBot/Bot/Telegram/Models/TGUpdate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ public final class TGUpdate: Codable {
case removedChatBoost = "removed_chat_boost"
}

/// The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you're using webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. If there are no new updates for at least a week, then identifier of the next update will be chosen randomly instead of sequentially.
/// The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This identifier becomes especially handy if you're using webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. If there are no new updates for at least a week, then identifier of the next update will be chosen randomly instead of sequentially.
public var updateId: Int

/// Optional. New incoming message of any kind - text, photo, sticker, etc.
public var message: TGMessage?

/// Optional. New version of a message that is known to the bot and was edited
/// Optional. New version of a message that is known to the bot and was edited. This update may at times be triggered by changes to message fields that are either unavailable or not actively used by your bot.
public var editedMessage: TGMessage?

/// Optional. New incoming channel post of any kind - text, photo, sticker, etc.
public var channelPost: TGMessage?

/// Optional. New version of a channel post that is known to the bot and was edited
/// Optional. New version of a channel post that is known to the bot and was edited. This update may at times be triggered by changes to message fields that are either unavailable or not actively used by your bot.
public var editedChannelPost: TGMessage?

/// Optional. A reaction to a message was changed by a user. The bot must be an administrator in the chat and must explicitly specify "message_reaction" in the list of allowed_updates to receive these updates. The update isn't received for reactions set by bots.
Expand All @@ -68,7 +68,7 @@ public final class TGUpdate: Codable {
/// Optional. New incoming pre-checkout query. Contains full information about checkout
public var preCheckoutQuery: TGPreCheckoutQuery?

/// Optional. New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot
/// Optional. New poll state. Bots receive only updates about manually stopped polls and polls, which are sent by the bot
public var poll: TGPoll?

/// Optional. A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.
Expand Down

0 comments on commit c4b8db2

Please sign in to comment.