Meshtastic-Apple/Meshtastic/AppIntents/MessageNodeIntent.swift

46 lines
1.2 KiB
Swift
Raw Normal View History

2024-12-03 22:14:14 -08:00
//
// MessageNodeIntent.swift
// Meshtastic
//
// Created by Benjamin Faershtein on 11/9/24.
//
import Foundation
import AppIntents
struct MessageNodeIntent: AppIntent {
static var title: LocalizedStringResource = "Send a Direct Message"
static var description: IntentDescription = "Send a message to a certain meshtastic node"
@Parameter(title: "Message")
var messageContent: String
@Parameter(title: "Node Number")
var nodeNumber: Int
static var parameterSummary: some ParameterSummary {
Summary("Send \(\.$messageContent) to \(\.$nodeNumber)")
}
func perform() async throws -> some IntentResult {
if !BLEManager.shared.isConnected {
throw AppIntentErrors.AppIntentError.notConnected
}
// Convert messageContent to data and check its length
guard let messageData = messageContent.data(using: .utf8) else {
throw AppIntentErrors.AppIntentError.message("Failed to encode message content")
}
if messageData.count > 200 {
throw $messageContent.needsValueError("Message content exceeds 200 bytes.")
}
if !BLEManager.shared.sendMessage(message: messageContent, toUserNum: Int64(nodeNumber), channel: 0, isEmoji: false, replyID: 0) {
throw AppIntentErrors.AppIntentError.message("Failed to send message")
}
return .result()
}
}