Skip to content

Instantly share code, notes, and snippets.

@rnkyr
Last active September 10, 2022 19:52
Show Gist options
  • Select an option

  • Save rnkyr/ca4ed515e946657b053bfae5ba5cc74f to your computer and use it in GitHub Desktop.

Select an option

Save rnkyr/ca4ed515e946657b053bfae5ba5cc74f to your computer and use it in GitHub Desktop.
//
// MessageIntentHandler.swift
//
// Created by Roman Kyrylenko on 22/02/17.
// Copyright © 2017 Yalantis. All rights reserved.
// https://yalantis.com
//
class MessagesIntentHandler: NSObject, INSendMessageIntentHandling, NetworkClientInjectable {
func resolveRecipients(forSendMessage intent: INSendMessageIntent, with completion: @escaping ([INPersonResolutionResult]) -> Void) {
guard let recipients = intent.recipients else {
completion([])
return
}
let resolutionResults = recipients.map { INPersonResolutionResult.success(with: $0) }
completion(resolutionResults)
}
func resolveContent(forSendMessage intent: INSendMessageIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
if let text = intent.content, !text.isEmpty {
completion(INStringResolutionResult.success(with: text))
} else {
completion(INStringResolutionResult.needsValue())
}
}
func confirm(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
let userActivity = NSUserActivity(activityType: String(describing: INSendMessageIntent.self))
let responseCode: INSendMessageIntentResponseCode
if !NetworkReachability.isReachable {
userActivity.userInfo = ["error": SiriIntentError.noInternetConnection]
responseCode = .failureMessageServiceNotAvailable
} else if !UserSession.hasAuthorizedSession() {
userActivity.userInfo = ["error": SiriIntentError.notAuthorized]
responseCode = .failureRequiringAppLaunch
} else {
responseCode = .success
}
let response = INSendMessageIntentResponse(code: responseCode, userActivity: userActivity)
completion(response)
}
func handle(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
MessageResolver.resolveSiriMessage(with: intent.content) { message in
self.apiClient.send(message: message) { error in
let responseCode: INSendMessageIntentResponseCode
let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
if let error = error {
userActivity.userInfo = ["error": error]
responseCode = .failure
} else {
responseCode = .success
}
let response = INSendMessageIntentResponse(code: responseCode, userActivity: userActivity)
completion(response)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment