iOS quick reply (finished)

This commit is contained in:
2024-12-21 22:19:27 +08:00
parent be98fe133d
commit 7182336a0d
7 changed files with 34 additions and 56 deletions

View File

@ -7,6 +7,7 @@
import Foundation
import home_widget
import Alamofire
class NotifyDelegate: UIResponder, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
@ -19,21 +20,34 @@ class NotifyDelegate: UIResponder, UNUserNotificationCenterDelegate {
let channelId = metadata["channel_id"] as? Int
let eventId = metadata["event_id"] as? Int
let replyToken = metadata["reply_token"] as? String
if #available(iOS 17, *) {
Task {
await HomeWidgetBackgroundWorker.run(
url: URL(string: "solink:///chat/reply")?.appending(queryItems: [
URLQueryItem(name: "channel_id", value: String(channelId ?? 0)),
URLQueryItem(name: "event_id", value: String(eventId ?? 0)),
URLQueryItem(name: "text", value: textResponse.userText)
]),
appGroup: "group.solsynth.solian"
)
}
} else {
// Fallback on earlier versions
if (channelId == nil || eventId == nil || replyToken == nil) {
return;
}
let serverUrl = "https://api.sn.solsynth.dev"
let url = "\(serverUrl)/cgi/im/quick/\(channelId!)/reply/\(eventId!)?replyToken=\(replyToken!)"
let parameters: [String: Any] = [
"type": "messages.new",
"body": [
"text": textResponse.userText,
"algorithm": "plain"
]
]
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.validate()
.responseString { response in
switch response.result {
case .success(_):
break
case .failure(let error):
print("Failed to send chat reply message: \(error)")
break
}
}
}
completionHandler()