Skip to content

Commit

Permalink
Merge branch 'feature/swift-5' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneQi committed Aug 29, 2019
2 parents 83855fe + c41bcd3 commit 7faf639
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 186 deletions.
2 changes: 2 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disabled_rules:
- identifier_name
1 change: 1 addition & 0 deletions Example/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation

let bot = ZEGBot(token: "TYPE YOUR TOKEN HERE")

// swiftlint:disable force_try
try! bot.run { update, bot in
dump(update)
}
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.2
// swift-tools-version:5.0

import PackageDescription

Expand All @@ -24,7 +24,7 @@ let package = Package(
path: "./Example"),
.testTarget(
name: "ZEGBotTests",
dependencies: ["ZEGBot"]),
dependencies: ["ZEGBot"])
],
swiftLanguageVersions: [.v4_2]
swiftLanguageVersions: [.v5]
)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ZEGBot

[![Build Status](https://travis-ci.org/ShaneQi/ZEGBot.svg?branch=master)](https://travis-ci.org/ShaneQi/ZEGBot) [![Swift Version](https://img.shields.io/badge/Swift-4.2-orange.svg?style=flat)](https://swift.org) ![Platforms](https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-blue.svg?style=flat) ![License](https://img.shields.io/badge/License-Apache-red.svg?style=flat)
[![Build Status](https://travis-ci.org/ShaneQi/ZEGBot.svg?branch=master)](https://travis-ci.org/ShaneQi/ZEGBot) [![Swift Version](https://img.shields.io/badge/Swift-5-orange.svg?style=flat)](https://swift.org) ![Platforms](https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-blue.svg?style=flat) ![License](https://img.shields.io/badge/License-Apache-red.svg?style=flat)

This library wraps the JSON decoding processing, making it easy to decode incoming JSON String to manipulatable objects.

Expand All @@ -11,7 +11,7 @@ This library wraps the processing of converting objects to Telegram Bot API requ
Add this project as a dependency in your Package.swift file.

```swift
.package(url: "https://github.com/shaneqi/ZEGBot.git", from: Version(4, 2, 0))
.package(url: "https://github.com/shaneqi/ZEGBot.git", from: Version(5, 0, 0))
```
## Quick Start

Expand Down
218 changes: 121 additions & 97 deletions Sources/Methods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,153 +15,177 @@ import Dispatch
extension ZEGBot {

@discardableResult
public func send(message text: String, to receiver: Sendable,
parseMode: ParseMode? = nil,
disableWebPagePreview: Bool? = nil,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .message(text: text, parseMode: parseMode, disableWebPagePreview: disableWebPagePreview),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: replyMarkup)
public func send(
message text: String, to receiver: Sendable,
parseMode: ParseMode? = nil,
disableWebPagePreview: Bool? = nil,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .message(text: text, parseMode: parseMode, disableWebPagePreview: disableWebPagePreview),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: replyMarkup)
return try performRequest(ofMethod: "sendMessage", payload: payload)
}

@discardableResult
public func forward(message: Message, to receiver: Sendable,
disableNotification: Bool? = nil) throws -> Message {
let payload = SendingPayload(content: .serverStoredContent(.message(chatId: message.chatId, messageId: message.messageId)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func forward(
message: Message, to receiver: Sendable,
disableNotification: Bool? = nil) throws -> Message {
let payload = SendingPayload(
content: .serverStoredContent(.message(chatId: message.chatId, messageId: message.messageId)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "forwardMessage", payload: payload)

}

@discardableResult
public func send(_ sticker: Sticker, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .serverStoredContent(.sticker(fileId: sticker.fileId)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func send(
_ sticker: Sticker, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .serverStoredContent(.sticker(fileId: sticker.fileId)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendSticker", payload: payload)
}

@discardableResult
public func send(_ photo: PhotoSize, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .serverStoredContent(.photo(fileId: photo.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func send(
_ photo: PhotoSize, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .serverStoredContent(.photo(fileId: photo.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendPhoto", payload: payload)
}

@discardableResult
public func send(_ audio: Audio, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .serverStoredContent(.audio(fileId: audio.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func send(
_ audio: Audio, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .serverStoredContent(.audio(fileId: audio.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendAudio", payload: payload)
}

@discardableResult
public func send(_ document: Document, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .serverStoredContent(.document(fileId: document.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func send(
_ document: Document, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .serverStoredContent(.document(fileId: document.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendDocument", payload: payload)
}


@discardableResult
public func send(_ video: Video, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .serverStoredContent(.video(fileId: video.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func send(
_ video: Video, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .serverStoredContent(.video(fileId: video.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendVideo", payload: payload)
}

@discardableResult
public func send(_ voice: Voice, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .serverStoredContent(.voice(fileId: voice.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func send(
_ voice: Voice, caption: String? = nil, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .serverStoredContent(.voice(fileId: voice.fileId, caption: caption)),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendVoice", payload: payload)
}

@discardableResult
public func sendLocation(latitude: Double, longitude: Double, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .location(latitude: latitude, longitude: longitude),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func sendLocation(
latitude: Double, longitude: Double, to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .location(latitude: latitude, longitude: longitude),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendLocation", payload: payload)
}

@discardableResult
public func sendVenue(latitude: Double, longitude: Double,
title: String, address: String, foursquareId: String? = nil,
to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .venue(latitude: latitude, longitude: longitude, title: title, address: address, foursquareId: foursquareId),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func sendVenue(
latitude: Double, longitude: Double,
title: String, address: String, foursquareId: String? = nil,
to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .venue(
latitude: latitude, longitude: longitude,
title: title, address: address, foursquareId: foursquareId),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendVenue", payload: payload)
}

@discardableResult
public func sendContact(phoneNumber: String, firstName: String, lastName: String? = nil,
to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(content: .contact(phoneNumber: phoneNumber, firstName: firstName, lastName: lastName),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
public func sendContact(
phoneNumber: String, firstName: String, lastName: String? = nil,
to receiver: Sendable,
disableNotification: Bool? = nil,
replyMarkup: InlineKeyboardMarkup? = nil) throws -> Message {
let payload = SendingPayload(
content: .contact(phoneNumber: phoneNumber, firstName: firstName, lastName: lastName),
chatId: receiver.chatId,
replyToMessageId: receiver.replyToMessageId,
disableNotification: disableNotification,
replyMarkup: nil)
return try performRequest(ofMethod: "sendContact", payload: payload)
}

public func send(chatAction: ChatAction, toChat chatId: Int) throws {
let payload = SendingPayload(content: .chatAction(chatAction: chatAction),
chatId: chatId, replyToMessageId: nil, disableNotification: nil,
replyMarkup: nil)
let payload = SendingPayload(
content: .chatAction(chatAction: chatAction),
chatId: chatId, replyToMessageId: nil, disableNotification: nil,
replyMarkup: nil)
let _: Bool = try performRequest(ofMethod: "sendChatAction", payload: payload)
}

public func deleteMessage(inChat chatId: Int, messageId: Int) throws {
let _: Bool = try performRequest(ofMethod: "deleteMessage",
payload: ["chat_id": chatId, "message_id": messageId])
payload: ["chat_id": chatId, "message_id": messageId])
}

public func getFile(ofId fileId: String) throws -> File {
Expand Down
5 changes: 4 additions & 1 deletion Sources/Sending.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct SendingPayload: Encodable {

}

// swiftlint:disable cyclomatic_complexity
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(chatId, forKey: .chatId)
Expand Down Expand Up @@ -115,7 +116,9 @@ struct SendingPayload: Encodable {
case .location(latitude: let latitude, longitude: let longitude):
try container.encode(latitude, forKey: .latitude)
try container.encode(longitude, forKey: .longitude)
case .venue(latitude: let latitude, longitude: let longitude, title: let title, address: let address, foursquareId: let foursquareId):
case .venue(
latitude: let latitude, longitude: let longitude, title: let title,
address: let address, foursquareId: let foursquareId):
try container.encode(latitude, forKey: .latitude)
try container.encode(longitude, forKey: .longitude)
try container.encode(title, forKey: .title)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public class Message: Codable {
public let pinnedMessage: Message?

private enum CodingKeys: String, CodingKey {
case date, chat, from, text, entities, audio, document, photo, sticker, video, voice, caption, contact, location, venue
case date, chat, from, text, entities
case audio, document, photo, sticker, video, voice, caption, contact, location, venue
case messageId = "message_id"
case forwardFrom = "forward_from"
case forwardFromChat = "forward_from_chat"
Expand Down
4 changes: 2 additions & 2 deletions Sources/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public enum Result<T>: Decodable where T: Decodable {
case false:
self = .failure(Error.telegram(try container.decode(String.self, forKey: .description)))
}
} catch(let error) {
} catch {
self = .failure(error)
}
}

static func decode(from data: Data) -> Result {
do {
return try JSONDecoder().decode(Result.self, from: data)
} catch(let error) {
} catch {
return .failure(error)
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/ZEGBot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public struct ZEGBot {
let semaphore = DispatchSemaphore(value: 0)
var encounterError: Swift.Error?
while encounterError == nil {
let task = session.dataTask(with: URL(string: urlPrefix + "getupdates?timeout=60&offset=\(offset)")!) { data, _, error in
let task = session.dataTask(
with: URL(string: urlPrefix + "getupdates?timeout=60&offset=\(offset)")!) { data, _, error in
guard let data = data else {
encounterError = error!
semaphore.signal()
Expand All @@ -38,7 +39,10 @@ public struct ZEGBot {
case .success(let updates):
if let lastUpdate = updates.last {
switch lastUpdate {
case .message(let updateId, _), .editedMessage(let updateId , _), .channelPost(let updateId, _), .callbackQuery(updateId: let updateId, _):
case .message(let updateId, _),
.editedMessage(let updateId, _),
.channelPost(let updateId, _),
.callbackQuery(updateId: let updateId, _):
offset = updateId + 1
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import XCTest
@testable import ZEGBotTests

XCTMain([
testCase(ZEGBotTests.allTests),
testCase(ZEGBotTests.allTests)
])
Loading

0 comments on commit 7faf639

Please sign in to comment.