Skip to content

Commit

Permalink
Merge pull request #6 from loloop/mc/category-list-command
Browse files Browse the repository at this point in the history
Adds a command to the telegram bot to list all available categories
  • Loading branch information
loloop authored Nov 9, 2023
2 parents d01f308 + aa86036 commit 3f36700
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 256 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
telegram/.swiftpm/xcode/xcshareddata/xcschemes/LandinhoBot.xcscheme
telegram/.swiftpm/xcode/xcshareddata/xcschemes/LandinhoBot.xcscheme
14 changes: 7 additions & 7 deletions SuperLandoAdmin/AdminLib/Sources/AdminLib/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public enum UploadRaceEventBundle: Equatable {
switch self {
case .F1Sprint:
[
.init(title: "Practice 1", date: Date()),
.init(title: "Treino Livre", date: Date()),
.init(title: "Qualifying", date: Date()),
.init(title: "Practice 2", date: Date()),
.init(title: "Sprint Shootout", date: Date()),
.init(title: "Sprint", date: Date()),
.init(title: "Race", date: Date()),
.init(title: "Corrida", date: Date()),
]
case .F1Regular:
[
.init(title: "Practice 1", date: Date()),
.init(title: "Practice 2", date: Date()),
.init(title: "Practice 3", date: Date()),
.init(title: "Treino Livre 1", date: Date()),
.init(title: "Treino Livre 2", date: Date()),
.init(title: "Treino Livre 3", date: Date()),
.init(title: "Qualifying", date: Date()),
.init(title: "Race", date: Date()),
.init(title: "Corrida", date: Date()),
]
}
}
Expand Down

This file was deleted.

63 changes: 63 additions & 0 deletions telegram/Sources/LandinhoBot/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// APIClient.swift
//
//
// Created by Mauricio Cardozo on 09/11/23.
//

import Foundation
#if os(Linux)
import FoundationNetworking
#endif

struct APIClientError: LocalizedError {
let message: String

var errorDescription: String? {
message
}
}

struct APIClient<T: Decodable> {

init(endpoint: String) {
self.endpoint = endpoint
}

let endpoint: String

func fetch(arguments: [String: String] = [:]) async throws -> T {
guard let url = buildURL(path: endpoint, args: arguments) else {
throw APIClientError(message: "Couldn't build URL")
}

let data = try await URLSession.shared.data(from: url)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601

do {
let response = try decoder.decode(T.self, from: data.0)
return response
} catch (let error) {
let result = String(data: data.0, encoding: .utf8) ?? "Couldn't decode JSON"
throw APIClientError(message: """
\(error)
––––
\(result)
""")
}
}

func buildURL(path: String, args: [String: String]) -> URL? {
var components = URLComponents()
components.scheme = "http"
components.host = "localhost"
components.path = "/\(path)"
components.port = 8080
components.queryItems = args.map { URLQueryItem(name: $0.key, value: $0.value) }
return components.url
}
}
117 changes: 0 additions & 117 deletions telegram/Sources/LandinhoBot/DefaultVroomBot.swift

This file was deleted.

2 changes: 1 addition & 1 deletion telegram/Sources/LandinhoBot/Models/Race.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct Category: Codable {
let title: String
let tag: String
let comment: String
let races: [Race]
let races: [Race]?
}

struct Race: Codable, Equatable {
Expand Down
12 changes: 12 additions & 0 deletions telegram/Sources/LandinhoBot/SwiftyBot/Bot.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Bot.swift
//
//
// Created by Mauricio Cardozo on 09/11/23.
//

import Foundation

public protocol Bot {
func reply(_ update: ChatUpdate, text: String) async throws
}
25 changes: 4 additions & 21 deletions telegram/Sources/LandinhoBot/SwiftyBot/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,8 @@
// Created by Mauricio Cardozo on 23/10/23.
//

public struct Command {
let command: String
let description: String
let handler: (ChatUpdate) async throws -> Void

public init(
command: String,
description: String,
handler: @escaping (ChatUpdate) async throws -> Void)
{
/// A bot command **must** be lowercased
let sanitizedCommand = command.lowercased()

if description.isEmpty {
fatalError("Command description must be non-empty")
}

self.command = sanitizedCommand
self.description = description
self.handler = handler
}
public protocol Command {
var command: String { get }
var description: String { get }
func handle(update: ChatUpdate, bot: Bot, debugMessage: (String) -> Void) async throws
}
Loading

0 comments on commit 3f36700

Please sign in to comment.