From 1892ec5a5f0a7154da83f2b6556387b353147433 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 1 Jul 2025 08:48:18 -0400 Subject: [PATCH] feat: allow sending nostr direct messages --- src/nostr/client.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/nostr/client.py b/src/nostr/client.py index d122a08..d139aef 100644 --- a/src/nostr/client.py +++ b/src/nostr/client.py @@ -88,19 +88,23 @@ class NostrClient: def publish_json_to_nostr( self, encrypted_json: bytes, to_pubkey: str | None = None ) -> bool: - """Builds and publishes a Kind 1 text note to the configured relays.""" + """Builds and publishes a Kind 1 text note or direct message.""" try: content = base64.b64encode(encrypted_json).decode("utf-8") - # Use the EventBuilder to create and sign the event - event = ( - EventBuilder.text_note(content) - .build(self.keys.public_key()) - .sign_with_keys(self.keys) - ) + if to_pubkey: + receiver = PublicKey.parse(to_pubkey) + event_output = self.client.send_private_msg_to( + self.relays, receiver, content + ) + else: + event = ( + EventBuilder.text_note(content) + .build(self.keys.public_key()) + .sign_with_keys(self.keys) + ) + event_output = self.publish_event(event) - # Send the event using the client - event_output = self.publish_event(event) event_id_hex = ( event_output.id.to_hex() if hasattr(event_output, "id")