]> nmode's Git Repositories - signal-cli/commitdiff
Implement sendPayment notification command
authorAsamK <asamk@gmx.de>
Thu, 19 May 2022 18:54:15 +0000 (20:54 +0200)
committerAsamK <asamk@gmx.de>
Sat, 21 May 2022 07:04:23 +0000 (09:04 +0200)
lib/src/main/java/org/asamk/signal/manager/Manager.java
lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
man/signal-cli.1.adoc
src/main/java/org/asamk/signal/commands/Commands.java
src/main/java/org/asamk/signal/commands/SendPaymentNotificationCommand.java [new file with mode: 0644]
src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java

index cc3ce0ce1da3061d0ccef3109b9bbf7a0cbb2a42..e568816fd89ebc43793cd43ce43966fe474f5b75 100644 (file)
@@ -141,6 +141,10 @@ public interface Manager extends Closeable {
             Set<RecipientIdentifier> recipients
     ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException;
 
+    SendMessageResults sendPaymentNotificationMessage(
+            byte[] receipt, String note, RecipientIdentifier.Single recipient
+    ) throws IOException;
+
     SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
 
     void deleteRecipient(RecipientIdentifier.Single recipient);
index f2d080fd91e829bf40adfc27da5e64fdf796bbbb..ed7ae86b09a67a3460185e8b66a73b43f1b38db0 100644 (file)
@@ -638,6 +638,20 @@ class ManagerImpl implements Manager {
         return sendMessage(messageBuilder, recipients);
     }
 
+    @Override
+    public SendMessageResults sendPaymentNotificationMessage(
+            byte[] receipt, String note, RecipientIdentifier.Single recipient
+    ) throws IOException {
+        final var paymentNotification = new SignalServiceDataMessage.PaymentNotification(receipt, note);
+        final var payment = new SignalServiceDataMessage.Payment(paymentNotification);
+        final var messageBuilder = SignalServiceDataMessage.newBuilder().withPayment(payment);
+        try {
+            return sendMessage(messageBuilder, Set.of(recipient));
+        } catch (NotAGroupMemberException | GroupNotFoundException | GroupSendingNotAllowedException e) {
+            throw new AssertionError(e);
+        }
+    }
+
     @Override
     public SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException {
         var messageBuilder = SignalServiceDataMessage.newBuilder().asEndSessionMessage();
index 7d1aa6f1cb3903e7a12658f10217ab5a4863c09d..1cebd044fb0addd3eecf02a8be0508a605868d9c 100644 (file)
@@ -256,6 +256,19 @@ Specify the mentions of the original message (same format as `--mention`).
 *-e*, *--end-session*::
 Clear session state and send end session message.
 
+=== sendPaymentNotification
+
+Send a payment notification.
+
+RECIPIENT::
+Specify the recipient’s phone number.
+
+*--receipt* RECEIPT::
+The base64 encoded receipt blob.
+
+*--note* NOTE::
+Specify a note for the payment notification.
+
 === sendReaction
 
 Send reaction to a previously received or sent message.
index 15ab172482b94387e725802a35442bbcfa2ee107..1c4251da504617c9cd66c629ef53e271eaa8bcb2 100644 (file)
@@ -34,6 +34,7 @@ public class Commands {
         addCommand(new RemoteDeleteCommand());
         addCommand(new SendCommand());
         addCommand(new SendContactsCommand());
+        addCommand(new SendPaymentNotificationCommand());
         addCommand(new SendReactionCommand());
         addCommand(new SendReceiptCommand());
         addCommand(new SendSyncRequestCommand());
diff --git a/src/main/java/org/asamk/signal/commands/SendPaymentNotificationCommand.java b/src/main/java/org/asamk/signal/commands/SendPaymentNotificationCommand.java
new file mode 100644 (file)
index 0000000..77181e3
--- /dev/null
@@ -0,0 +1,51 @@
+package org.asamk.signal.commands;
+
+import net.sourceforge.argparse4j.inf.Namespace;
+import net.sourceforge.argparse4j.inf.Subparser;
+
+import org.asamk.signal.commands.exceptions.CommandException;
+import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
+import org.asamk.signal.manager.Manager;
+import org.asamk.signal.output.OutputWriter;
+import org.asamk.signal.util.CommandUtil;
+
+import java.io.IOException;
+import java.util.Base64;
+
+import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
+
+public class SendPaymentNotificationCommand implements JsonRpcLocalCommand {
+
+    @Override
+    public String getName() {
+        return "sendPaymentNotification";
+    }
+
+    @Override
+    public void attachToSubparser(final Subparser subparser) {
+        subparser.help("Send a payment notification.");
+        subparser.addArgument("recipient").help("Specify the recipient's phone number.");
+        subparser.addArgument("--receipt").required(true).help("The base64 encoded receipt blob.");
+        subparser.addArgument("--note").help("Specify a note for the payment notification.");
+    }
+
+    @Override
+    public void handleCommand(
+            final Namespace ns, final Manager m, final OutputWriter outputWriter
+    ) throws CommandException {
+        final var recipientString = ns.getString("recipient");
+        final var recipientIdentifier = CommandUtil.getSingleRecipientIdentifier(recipientString, m.getSelfNumber());
+
+        final var receiptString = ns.getString("receipt");
+        final var receipt = Base64.getDecoder().decode(receiptString);
+        final var note = ns.getString("note");
+
+        try {
+            final var results = m.sendPaymentNotificationMessage(receipt, note, recipientIdentifier);
+            outputResult(outputWriter, results);
+        } catch (IOException e) {
+            throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
+                    .getSimpleName() + ")", e);
+        }
+    }
+}
index 11800be2a3e0069a40d6e160d52c5b9d5809e288..6bb93677f15e030c5a836aecdcd51821416015a4 100644 (file)
@@ -388,6 +388,13 @@ public class DbusManagerImpl implements Manager {
                         groupId));
     }
 
+    @Override
+    public SendMessageResults sendPaymentNotificationMessage(
+            final byte[] receipt, final String note, final RecipientIdentifier.Single recipient
+    ) throws IOException {
+        throw new UnsupportedOperationException();
+    }
+
     @Override
     public SendMessageResults sendEndSessionMessage(final Set<RecipientIdentifier.Single> recipients) throws IOException {
         signal.sendEndSessionMessage(recipients.stream().map(RecipientIdentifier.Single::getIdentifier).toList());