Performing the desired action

Once Siri knows exactly what the user wants to do and which parameters to use, and once your app has confirmed that everything is in place to handle the user's request, the time has finally come to execute the requested action. Once this time has come, Siri calls the handle method on your intent handler.

Just like the confirm method, every intent has their own version of this method, but they all follow a similar pattern. For sending messages, the method signature is handle(sendMessage:completion:). The parameters for this method are identical to the ones in the confirmation step. The major difference is that you're now expected to handle the intent instead of only confirming that the intent is valid.

Once you're done handling the intent, you must call the completion handler with an INSendMessageIntentResponse. If everything goes well, you're expected to use a success response code. If you're unable to process the intent quickly, you're expected to call the completion handler with an inProgress status code. Using the inProgress informs Siri that you're handling the intent but it's taking a while. An example of a handle method is shown in the following code snippet:

func handle(sendMessage intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
guard let groupName = intent.groupName,
let message = intent.content else {
completion(INSendMessageIntentResponse(code: .failure, userActivity: nil))
}


MessagingApi.sendMessage(message, toGroup: groupName) { success in
if success {
completion(INSendMessageIntentResponse(code: .success, userActivity: nil)
} else {
completion(INSendMessageIntentResponse(code: .failure, userActivity: nil)
}
}
}

Just like before, we're using non-existing dummy classes, so copying and pasting this code won't work in the example app. The purpose of this snippet is to show you what an implementation of sending a message could look like. First, we confirm that we can extract all of the required information to send a message with. If this fails, we tell Siri that we couldn't send the message. Then a MessagingApi class method is used to send the message to the selected group. Finally, we inform Siri about how we handled the intent based on the response from the API.

The final aspect of the Siri experience we need to take into account is the user interface. Siri will provide us with a default interface for every intent, but it's often desirable to customize this interface to match your app's look and feel. Let's see how we can achieve this.