Skip to content

Commit

Permalink
Add CallbackQuery to TelegramUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Koray Koska committed Aug 16, 2017
1 parent 4118dd2 commit cab8092
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
95 changes: 95 additions & 0 deletions Sources/VaporTelegramBot/Api/TelegramCallbackQuery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// Created by Koray Koska on 16/08/2017.
//

import Foundation
import JSON

public final class TelegramCallbackQuery: TelegramJSONConvertible {

// MARK: - Keys

public struct Keys: TelegramJSONConvertibleKeys {

public static let idKey = "id"
public static let inlineMessageIdKey = "inline_message_id"
public static let chatInstanceKey = "chat_instance"
public static let dataKey = "data"
public static let gameShortNameKey = "game_short_name"

public static let fromKey = "from"
public static let messageKey = "message"
}

// MARK: - Primitive types

/// Unique identifier for this query
public var id: String

/// Optional. Identifier of the message sent via the bot in inline mode,
/// that originated the query.
public var inlineMessageId: String?

/// Global identifier, uniquely corresponding to the chat to which
/// the message with the callback button was sent.
/// Useful for high scores in games.
public var chatInstance: String

/// Optional. Data associated with the callback button.
/// Be aware that a bad client can send arbitrary data in this field.
public var data: String?

/// Optional. Short name of a Game to be returned, serves as the unique identifier for the game
public var gameShortName: String?

// MARK: - Object types

/// Sender
public var from: TelegramUser

/// Optional. Message with the callback button that originated the query.
/// Note that message content and message date will not be available if the message is too old
public var message: TelegramMessage?

public init(json: JSON) throws {
self.id = try json.get(Keys.idKey)
self.chatInstance = try json.get(Keys.chatInstanceKey)

self.inlineMessageId = json[Keys.inlineMessageIdKey]?.string
self.data = json[Keys.dataKey]?.string
self.gameShortName = json[Keys.gameShortNameKey]?.string

self.from = try TelegramUser(json: json.get(Keys.fromKey))

if let messageJson = json[Keys.messageKey] {
self.message = try TelegramMessage(json: messageJson)
}
}

public func makeJSON() throws -> JSON {
var json = JSON()

try json.set(Keys.idKey, id)
try json.set(Keys.chatInstanceKey, chatInstance)

if let inlineMessageId = inlineMessageId {
try json.set(Keys.inlineMessageIdKey, inlineMessageId)
}

if let data = data {
try json.set(Keys.dataKey, data)
}

if let gameShortName = gameShortName {
try json.set(Keys.gameShortNameKey, gameShortName)
}

try json.set(Keys.fromKey, from.makeJSON())

if let message = message {
try json.set(Keys.messageKey, message.makeJSON())
}

return json
}
}
12 changes: 12 additions & 0 deletions Sources/VaporTelegramBot/Api/TelegramUpdate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public final class TelegramUpdate: TelegramJSONConvertible {
public static let editedMessageKey = "edited_message"
public static let channelPostKey = "channel_post"
public static let editedChannelPostKey = "channel_post"
public static let callbackQueryKey = "callback_query"
}

// MARK: - Primitive types
Expand All @@ -42,6 +43,9 @@ public final class TelegramUpdate: TelegramJSONConvertible {
/// Optional. New version of a channel post that is known to the bot and was edited
public var editedChannelPost: TelegramMessage?

/// Optional. New incoming callback query
public var callbackQuery: TelegramCallbackQuery?

public init(json: JSON) throws {
self.updateId = try json.get(Keys.updateIdKey)

Expand All @@ -60,6 +64,10 @@ public final class TelegramUpdate: TelegramJSONConvertible {
if let editedChannelPostJson = json[Keys.editedChannelPostKey] {
self.editedChannelPost = try TelegramMessage(json: editedChannelPostJson)
}

if let callbackQueryJson = json[Keys.callbackQueryKey] {
self.callbackQuery = try TelegramCallbackQuery(json: callbackQueryJson)
}
}

public func makeJSON() throws -> JSON {
Expand All @@ -83,6 +91,10 @@ public final class TelegramUpdate: TelegramJSONConvertible {
try json.set(Keys.editedChannelPostKey, editedChannelPost)
}

if let callbackQuery = callbackQuery {
try json.set(Keys.callbackQueryKey, callbackQuery)
}

return json
}
}

0 comments on commit cab8092

Please sign in to comment.