|
| 1 | +package org.hiero.base.sample; |
| 2 | + |
| 3 | +import org.hiero.base.TopicClient; |
| 4 | +import org.hiero.base.implementation.ProtocolLayerClientImpl; |
| 5 | +import org.hiero.base.implementation.TopicClientImpl; |
| 6 | +import org.hiero.base.protocol.ProtocolLayerClient; |
| 7 | +import picocli.CommandLine.Command; |
| 8 | +import picocli.CommandLine.Option; |
| 9 | + |
| 10 | +@Command(name = "send-message", description = "Send a message to a Hiero topic", mixinStandardHelpOptions = true) |
| 11 | +public class SendMessageCommand implements Runnable { |
| 12 | + |
| 13 | + @Option(names = {"-n", "--network"}, description = "Hiero network", defaultValue = "hedera-testnet") |
| 14 | + private String network; |
| 15 | + |
| 16 | + @Option(names = {"-a", "--account-id"}, description = "Operator account ID", required = true) |
| 17 | + private String accountId; |
| 18 | + |
| 19 | + @Option(names = {"-k", "--private-key"}, description = "Operator private key", required = true) |
| 20 | + private String privateKey; |
| 21 | + |
| 22 | + @Option(names = {"-t", "--topic-id"}, description = "Topic ID", required = true) |
| 23 | + private String topicId; |
| 24 | + |
| 25 | + @Option(names = {"-msg", "--message"}, description = "Message content", required = true) |
| 26 | + private String message; |
| 27 | + |
| 28 | + @Override |
| 29 | + public void run() { |
| 30 | + try { |
| 31 | + final CliHieroContext context = new CliHieroContext(accountId, privateKey, network); |
| 32 | + final ProtocolLayerClient protocolClient = new ProtocolLayerClientImpl(context); |
| 33 | + final TopicClient topicClient = new TopicClientImpl(protocolClient, context.getOperatorAccount()); |
| 34 | + System.out.println("Sending message to topic " + topicId + "..."); |
| 35 | + topicClient.submitMessage(topicId, message); |
| 36 | + System.out.println("Message sent successfully!"); |
| 37 | + } catch (final Exception e) { |
| 38 | + System.err.println("Error: " + e.getMessage()); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments