]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendReceiptCommand.java
Handle send failures as non fatal and return detailed results in json output
[signal-cli] / src / main / java / org / asamk / signal / commands / SendReceiptCommand.java
1 package org.asamk.signal.commands;
2
3 import net.sourceforge.argparse4j.inf.Namespace;
4 import net.sourceforge.argparse4j.inf.Subparser;
5
6 import org.asamk.signal.commands.exceptions.CommandException;
7 import org.asamk.signal.commands.exceptions.UserErrorException;
8 import org.asamk.signal.manager.Manager;
9 import org.asamk.signal.manager.api.SendMessageResults;
10 import org.asamk.signal.output.OutputWriter;
11 import org.asamk.signal.util.CommandUtil;
12
13 import java.io.IOException;
14
15 import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
16
17 public class SendReceiptCommand implements JsonRpcLocalCommand {
18
19 @Override
20 public String getName() {
21 return "sendReceipt";
22 }
23
24 @Override
25 public void attachToSubparser(final Subparser subparser) {
26 subparser.help("Send a read or viewed receipt to a previously received message.");
27 subparser.addArgument("recipient").help("Specify the sender's phone number.").required(true);
28 subparser.addArgument("-t", "--target-timestamp")
29 .type(long.class)
30 .nargs("+")
31 .help("Specify the timestamp of the messages for which a receipt should be sent.");
32 subparser.addArgument("--type")
33 .help("Specify the receipt type (default is read receipt).")
34 .choices("read", "viewed");
35 }
36
37 @Override
38 public void handleCommand(
39 final Namespace ns, final Manager m, final OutputWriter outputWriter
40 ) throws CommandException {
41 final var recipientString = ns.getString("recipient");
42 final var recipient = CommandUtil.getSingleRecipientIdentifier(recipientString, m.getSelfNumber());
43
44 final var targetTimestamps = ns.<Long>getList("target-timestamp");
45 final var type = ns.getString("type");
46
47 try {
48 final SendMessageResults results;
49 if (type == null || "read".equals(type)) {
50 results = m.sendReadReceipt(recipient, targetTimestamps);
51 } else if ("viewed".equals(type)) {
52 results = m.sendViewedReceipt(recipient, targetTimestamps);
53 } else {
54 throw new UserErrorException("Unknown receipt type: " + type);
55 }
56 outputResult(outputWriter, results);
57 } catch (IOException e) {
58 throw new UserErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
59 .getSimpleName() + ")");
60 }
61 }
62 }