+ final var textStyleStrings = ns.<String>getList("text-style");
+ final var textStyles = textStyleStrings == null ? List.<TextStyle>of() : parseTextStyles(textStyleStrings);
+
+ final Message.Quote quote;
+ final var quoteTimestamp = ns.getLong("quote-timestamp");
+ if (quoteTimestamp != null) {
+ final var quoteAuthor = ns.getString("quote-author");
+ final var quoteMessage = ns.getString("quote-message");
+ final var quoteMentionStrings = ns.<String>getList("quote-mention");
+ final var quoteMentions = quoteMentionStrings == null
+ ? List.<Message.Mention>of()
+ : parseMentions(selfNumber, quoteMentionStrings);
+ final var quoteTextStyleStrings = ns.<String>getList("quote-text-style");
+ final var quoteAttachmentStrings = ns.<String>getList("quote-attachment");
+ final var quoteTextStyles = quoteTextStyleStrings == null
+ ? List.<TextStyle>of()
+ : parseTextStyles(quoteTextStyleStrings);
+ final var quoteAttachments = quoteAttachmentStrings == null
+ ? List.<Message.Quote.Attachment>of()
+ : parseQuoteAttachments(quoteAttachmentStrings);
+ quote = new Message.Quote(quoteTimestamp,
+ CommandUtil.getSingleRecipientIdentifier(quoteAuthor, selfNumber),
+ quoteMessage == null ? "" : quoteMessage,
+ quoteMentions,
+ quoteTextStyles,
+ quoteAttachments);
+ } else {
+ quote = null;
+ }
+
+ final List<Message.Preview> previews;
+ final var previewUrl = ns.getString("preview-url");
+ if (previewUrl != null) {
+ final var previewTitle = ns.getString("preview-title");
+ final var previewDescription = ns.getString("preview-description");
+ final var previewImage = ns.getString("preview-image");
+ previews = List.of(new Message.Preview(previewUrl,
+ Optional.ofNullable(previewTitle).orElse(""),
+ Optional.ofNullable(previewDescription).orElse(""),
+ Optional.ofNullable(previewImage)));
+ } else {
+ previews = List.of();
+ }
+
+ final Message.StoryReply storyReply;
+ final var storyReplyTimestamp = ns.getLong("story-timestamp");
+ if (storyReplyTimestamp != null) {
+ final var storyAuthor = ns.getString("story-author");
+ storyReply = new Message.StoryReply(storyReplyTimestamp,
+ CommandUtil.getSingleRecipientIdentifier(storyAuthor, selfNumber));
+ } else {
+ storyReply = null;
+ }
+
+ if (messageText.isEmpty() && attachments.isEmpty() && sticker == null && quote == null) {
+ throw new UserErrorException(
+ "Sending empty message is not allowed, either a message, attachment or sticker must be given.");
+ }
+
+ final var editTimestamp = ns.getLong("edit-timestamp");
+
+ try {
+ final var message = new Message(messageText,
+ attachments,
+ mentions,
+ Optional.ofNullable(quote),
+ Optional.ofNullable(sticker),
+ previews,
+ Optional.ofNullable((storyReply)),
+ textStyles);
+ var results = editTimestamp != null
+ ? m.sendEditMessage(message, recipientIdentifiers, editTimestamp)
+ : m.sendMessage(message, recipientIdentifiers);
+ outputResult(outputWriter, results);
+ } catch (AttachmentInvalidException | IOException e) {
+ throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
+ .getSimpleName() + ")", e);
+ } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
+ throw new UserErrorException(e.getMessage());
+ } catch (UnregisteredRecipientException e) {
+ throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
+ } catch (InvalidStickerException e) {
+ throw new UserErrorException("Failed to send sticker: " + e.getMessage(), e);
+ }
+ }
+
+ private List<Message.Mention> parseMentions(
+ final String selfNumber, final List<String> mentionStrings
+ ) throws UserErrorException {
+ final var mentionPattern = Pattern.compile("(\\d+):(\\d+):(.+)");
+ final var mentions = new ArrayList<Message.Mention>();
+ for (final var mention : mentionStrings) {
+ final var matcher = mentionPattern.matcher(mention);
+ if (!matcher.matches()) {
+ throw new UserErrorException("Invalid mention syntax ("
+ + mention
+ + ") expected 'start:length:recipientNumber'");