Meshtastic-Apple/Meshtastic/AppIntents/MessageChannelIntent.swift
2024-08-09 23:02:55 -07:00

43 lines
1.3 KiB
Swift

//
// MessageChannelIntent.swift
// Meshtastic
//
// Created by Benjamin Faershtein on 8/9/24.
//
import Foundation
import AppIntents
struct MessageChannelIntent: AppIntent {
static var title: LocalizedStringResource = "Send a channel message"
@Parameter(title: "Message")
var messageContent: String
@Parameter(title: "Channel",controlStyle: .stepper, inclusiveRange: (lowerBound: 0, upperBound: 7))
var channelNumber: Int
static var parameterSummary: some ParameterSummary {
Summary("Send \(\.$messageContent) to \(\.$channelNumber)")
}
func perform() async throws -> some IntentResult {
// Check if channel number is between 1 and 7
guard (0...7).contains(channelNumber) else {
throw $channelNumber.needsValueError("Channel number must be between 0 and 7.")
}
// Convert messageContent to data and check its length
guard let messageData = messageContent.data(using: .utf8) else {
throw $messageContent.needsValueError("Failed to encode message content.")
}
if messageData.count > 228 {
throw $messageContent.needsValueError("Message content exceeds 228 bytes.")
}
if (BLEManager.shared.isConnected){
BLEManager.shared.sendMessage(message: messageContent, toUserNum: 0, channel: Int32(channelNumber), isEmoji: false, replyID: 0)
}
return .result()
}
}