+
+ var messageText = ns.getString("message");
+ if (messageText == null) {
+ try {
+ messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
+ } catch (IOException e) {
+ throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
+ }
+ }
+
+ List<String> attachments = ns.getList("attachment");
+ if (attachments == null) {
+ attachments = List.of();
+ }
+
+ try {
+ var results = m.sendMessage(new Message(messageText, attachments), recipientIdentifiers);
+ outputResult(outputWriter, results.getTimestamp());
+ ErrorUtils.handleSendMessageResults(results.getResults());
+ } catch (AttachmentInvalidException | IOException e) {
+ throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
+ .getSimpleName() + ")");
+ } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
+ throw new UserErrorException(e.getMessage());
+ }
+ }
+
+ @Override
+ public void handleCommand(
+ final Namespace ns, final Signal signal, final OutputWriter outputWriter
+ ) throws CommandException {
+ final var recipients = ns.<String>getList("recipient");
+ final var isEndSession = ns.getBoolean("end-session");
+ final var groupIdStrings = ns.<String>getList("group-id");
+ final var isNoteToSelf = ns.getBoolean("note-to-self");
+
+ final var noRecipients = recipients == null || recipients.isEmpty();
+ final var noGroups = groupIdStrings == null || groupIdStrings.isEmpty();
+ if ((noRecipients && isEndSession) || (noRecipients && noGroups && !isNoteToSelf)) {
+ throw new UserErrorException("No recipients given");
+ }
+ if (!noRecipients && !noGroups) {
+ throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");