mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
47 lines
1.3 KiB
Swift
47 lines
1.3 KiB
Swift
//
|
|
// 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 await !AccessoryManager.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.")
|
|
}
|
|
|
|
do {
|
|
try await AccessoryManager.shared.sendMessage(message: messageContent, toUserNum: Int64(nodeNumber), channel: 0, isEmoji: false, replyID: 0)
|
|
} catch {
|
|
throw AppIntentErrors.AppIntentError.message("Failed to send message")
|
|
}
|
|
|
|
return .result()
|
|
}
|
|
}
|