Meshtastic-Apple/Meshtastic/AppIntents/MessageChannelIntent.swift

51 lines
1.5 KiB
Swift
Raw Normal View History

//
// MessageChannelIntent.swift
// Meshtastic
//
// Created by Benjamin Faershtein on 8/9/24.
//
import Foundation
import AppIntents
struct MessageChannelIntent: AppIntent {
2024-08-27 21:17:35 -07:00
static var title: LocalizedStringResource = "Send a Group Message"
static var description: IntentDescription = "Send a message to a certain meshtastic channel"
@Parameter(title: "Message")
var messageContent: String
2024-10-04 19:36:30 -07:00
@Parameter(title: "Channel", controlStyle: .stepper, inclusiveRange: (lowerBound: 0, upperBound: 7))
var channelNumber: Int
2024-10-04 19:36:30 -07:00
static var parameterSummary: some ParameterSummary {
Summary("Send \(\.$messageContent) to \(\.$channelNumber)")
}
func perform() async throws -> some IntentResult {
2024-10-04 19:36:30 -07:00
if !BLEManager.shared.isConnected {
throw AppIntentErrors.AppIntentError.notConnected
}
2024-10-04 19:36:30 -07:00
// 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.")
}
2024-10-04 19:36:30 -07:00
// 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")
}
2024-10-05 15:50:57 -07:00
if messageData.count > 200 {
throw $messageContent.needsValueError("Message content exceeds 200 bytes.")
}
2024-10-04 19:36:30 -07:00
if !BLEManager.shared.sendMessage(message: messageContent, toUserNum: 0, channel: Int32(channelNumber), isEmoji: false, replyID: 0) {
throw AppIntentErrors.AppIntentError.message("Failed to send message")
}
2024-10-04 19:36:30 -07:00
return .result()
}
}