Skip to content

Commit

Permalink
Merge pull request #31 from swiftde/add-summary-command
Browse files Browse the repository at this point in the history
add summarize command
  • Loading branch information
kiliankoe authored Oct 22, 2023
2 parents 1d4ef60 + 8392494 commit 6af988b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
73 changes: 73 additions & 0 deletions Sources/SwiftDEBot/Command/Message Commands/Summarize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Foundation
import DiscordBM

struct SummarizeCommand: MessageCommand {
let helpText = "`!summarize`: Fasse einen Link im Reply zusammen."

func run(client: DiscordClient, message: Gateway.MessageCreate) async throws {
guard message.content == "!summarize" else { return }

guard let replyContent = message.referenced_message?.value.content else {
try await client.send(
"Schicke bitte `!summarize` als Reply auf eine Nachricht mit einem Link.",
to: message.channel_id
)
return
}
guard let url = replyContent.firstURL else {
try await client.send(
"In der referenzierten Nachricht sehe ich leider keine URL 🤨",
to: message.channel_id
)
return
}
log.info("Summarizing \(url)")

try await client.setTyping(in: message.channel_id)

guard let encodedURL = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
log.error("Unable to URL encode \(url)")
return
}

guard let apiToken = ProcessInfo.processInfo.environment["KAGI_API_TOKEN"] else {
log.error("Necessary env var not found, please set KAGI_API_TOKEN.")
return
}

let response = try await httpClient.get(
"https://kagi.com/api/v0/summarize?target_language=DE&url=\(encodedURL)",
headers: ["Authorization": "Bot \(apiToken)"],
response: KagiResponse.self
)

let summary = response.data.output

guard !summary.isEmpty else {
try await client.send(
"Das kann ich leider nicht zusammenfassen 🫥",
to: message.channel_id
)
return
}

try await client.send(summary, to: message.channel_id)
}
}

private extension String {
var firstURL: String? {
if let found = self.firstMatch(of: #/(https?://\S+)/#) {
return String(found.output.1)
}
return nil
}
}

private struct KagiResponse: Decodable {
let data: ResponseData

struct ResponseData: Decodable {
let output: String
}
}
1 change: 1 addition & 0 deletions Sources/SwiftDEBot/Commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let messageCommands: [MessageCommand] = [
SwiftEvolutionCommand(),
AppleStatusCommand(),
SPICommand(),
SummarizeCommand(),

CowsCommand(),

Expand Down
11 changes: 9 additions & 2 deletions Sources/SwiftDEBot/Utils/HTTPClient+get.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import AsyncHTTPClient
import Foundation

extension HTTPClient {
func get<Response>(_ url: String, response: Response.Type) async throws -> Response where Response: Decodable {
let request = HTTPClientRequest(url: url)
func get<Response>(
_ url: String,
headers: [String: String]? = nil,
response: Response.Type
) async throws -> Response where Response: Decodable {
var request = HTTPClientRequest(url: url)
if let headers {
request.headers = .init(headers.map{ ($0.key, $0.value) })
}
let response = try await self.execute(request, timeout: .seconds(30))
if response.status == .ok {
let body = try await response.body.collect(upTo: 50 * 1024 * 1024) // 50 MB
Expand Down

0 comments on commit 6af988b

Please sign in to comment.