jeffthibault/python-nostr

[Question] Basic usage

Closed this issue · 7 comments

I am having a hard time figuring out how the examples work, since i can't get any output. Thi is what i am working with:

import json
import ssl
import time
from nostr.relay_manager import RelayManager
from nostr.filter import Filter, Filters
from nostr.key import PrivateKey
from nostr.event import Event, EventKind
from nostr.message_type import ClientMessageType

private_key = PrivateKey()
public_key = private_key.public_key
print(f"Private key: {private_key.bech32()}")
print(f"Public key: {public_key.bech32()}")

relay_manager = RelayManager()
relay_manager.add_relay("wss://nostr-pub.wellorder.net")
relay_manager.add_relay("wss://relay.damus.io")
relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE}) # NOTE: This disables ssl certificate verification
time.sleep(1.25) # allow the connections to open

while relay_manager.message_pool.has_notices():
  notice_msg = relay_manager.message_pool.get_notice()
  print(notice_msg.content)

# publish message
event = Event(public_key.hex(), "Hello Nostr")
private_key.sign_event(event)

relay_manager.publish_event(event)
time.sleep(1) # allow the messages to send

# read message

filters = Filters([Filter(authors=public_key.hex(), kinds=[EventKind.TEXT_NOTE])])
subscription_id = "test"
request = [ClientMessageType.REQUEST, subscription_id]
request.extend(filters.to_json_array())

message = json.dumps(request)
relay_manager.publish_message(message)
time.sleep(1)

while relay_manager.message_pool.has_events():
  event_msg = relay_manager.message_pool.get_event()
  print(event_msg.event.content)

relay_manager.close_connections()

It doesn't want to return the published message, and i can't figure out why

did a little debugging, and found out i got a Notice back: ERROR: bad req: std::get: wrong index for variant
Still don't know what i'm doing wrong though

When you create the filters, it should be authors=[public_key.hex()]

When you create the filters, it should be authors=[public_key.hex()]

that fixed the sending, but it still doesn't receive the message

import json, ssl, os, time,json

from nostr.relay_manager import RelayManager
from nostr.filter import Filter, Filters
from nostr.key import PrivateKey
from nostr.event import Event, EventKind
from nostr.message_type import ClientMessageType

private_key = PrivateKey()
public_key = private_key.public_key
print(f"Private key: {private_key.bech32()}")
print(f"Public key: {public_key.bech32()}")

relay_manager = RelayManager()
relay_manager.add_relay("wss://nostr-pub.wellorder.net")
relay_manager.add_relay("wss://relay.damus.io")
relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE}) # NOTE: This disables ssl certificate verification
time.sleep(1.25) # allow the connections to open

print("\nConnected")

while relay_manager.message_pool.has_notices():
    notice_msg = relay_manager.message_pool.get_notice()
    print(notice_msg.content)

# publish message
event = Event(public_key.hex(), "Hello Nostr")
private_key.sign_event(event)
print(f"\nMessage: {event.to_message()}")

relay_manager.publish_event(event)
print("\nMessage published")
time.sleep(1) # allow the messages to send

while relay_manager.message_pool.has_notices():
    notice_msg = relay_manager.message_pool.get_notice()
    print(notice_msg.content)

# read message
filters = Filters([Filter(authors=[public_key.hex()], kinds=[EventKind.TEXT_NOTE])])
subscription_id = os.urandom(6).hex()
request = [ClientMessageType.REQUEST, subscription_id]
request.extend(filters.to_json_array())

message = json.dumps(request)
relay_manager.publish_message(message)
print("\nRequest for message sent")
time.sleep(1)

while relay_manager.message_pool.has_notices():
    notice_msg = relay_manager.message_pool.get_notice()
    print(notice_msg.content)

while relay_manager.message_pool.has_events():
    event_msg = relay_manager.message_pool.get_event()
    print(event_msg.event.content)

print("Closing connections")
relay_manager.close_connections()

You need to add the subscription to the relay manager before you publish the request.

After you create subscription_id add this:

relay_manager.add_subscription(subscription_id, filters)

You need to add the subscription to the relay manager before you publish the request.

After you create subscription_id add this:

relay_manager.add_subscription(subscription_id, filters)

either my eyes are going bad, or i'm just really stupid. And neither of those are good

Thanks for helping man!