-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from swiftde/add-summary-command
add summarize command
- Loading branch information
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
Sources/SwiftDEBot/Command/Message Commands/Summarize.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters