Skip to content

Commit

Permalink
Merge pull request #21 from loloop/mc/fix-telegram
Browse files Browse the repository at this point in the history
attempts to fix telegram bot
  • Loading branch information
loloop authored Jun 29, 2024
2 parents 80ac8f1 + abfd3fb commit 3bccb32
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ struct NextRaceCommand: Command {

func handle(update: ChatUpdate, bot: Bot, debugMessage: (String) -> Void) async throws {
let categoryTag = update.arguments.first ?? ""
let response = try await api.fetch(arguments: ["argument": categoryTag])
guard let formattedResponse = formatResponse(
response,
showsHelpText: categoryTag.isEmpty)
else {
do {
let response = try await api.fetch(arguments: ["argument": categoryTag])
let formattedResponse = formatResponse(
response,
showsHelpText: categoryTag.isEmpty)
try await bot.reply(update, text: formattedResponse)
} catch {
try await bot.reply(update, text: "Não encontrei a próxima corrida")
return
}
try await bot.reply(update, text: formattedResponse)
}

func formatResponse(_ response: NextRaceResponse, showsHelpText: Bool) -> String? {
func formatResponse(_ response: NextRaceResponse, showsHelpText: Bool) -> String {
return """
\(response.category.title)
\(response.nextRace.title)
\(formatRace(response.nextRace))
\(response.title)
\(formatRace(response))
\(response.category.comment)
\(showsHelpText ? Self.helpText : "")
"""
}

func formatRace(_ race: Race) -> String {
let events = race.events.map(formatEvent(_:)).joined(separator: "\n")
guard !events.isEmpty else { return formatEventlessRace(race: race) }
func formatRace(_ response: NextRaceResponse) -> String {
let events = response.events.map(formatEvent(_:)).joined(separator: "\n")
guard !events.isEmpty else { return formatEventlessRace(race: response) }
return """
🏎️🏎️🏎️🏎️🏎️🏎️🏎️
Expand All @@ -54,7 +54,7 @@ struct NextRaceCommand: Command {
"\(Self.formatter.string(from: event.date))\(event.title)"
}

func formatEventlessRace(race: Race) -> String {
func formatEventlessRace(race: NextRaceResponse) -> String {
"""
🏎️🏎️🏎️🏎️🏎️🏎️🏎️
Expand All @@ -79,7 +79,10 @@ struct NextRaceCommand: Command {
}()

struct NextRaceResponse: Codable, Equatable {
let nextRace: Race
let id: UUID
let title: String
let earliestEventDate: Date
let events: [RaceEvent]
let category: Category
}
}

0 comments on commit 3bccb32

Please sign in to comment.