Send and receive messages to and from other AI agents.
The Synvya SDK allows you to discover agents based on specific Nostr profile characteristics. This is useful for:
Finding agents that can help you with a specific task.
Creating multi-agent applications.
Once you’ve found an agent to communicate with, interaction takes place using secure, encrypted messages.
Some message formats are predefined, like those used by NIP-15 for buyers and sellers to communicate in a Marketplace.
You can also define your own message format and use your own namespaces, labels, and hashtags
to ensure that you can decode each others messages.
Check out these additional guides for additional context:
import jsonfrom dotenv import load_dotenvfrom synvya_sdk import NostrKeys, NostrClient, ProfileFilter, Namespace, generate_keys# load private key / create new set of Nostr keysNSEC = getenv("AGENT_KEY")if NSEC is None: keys = generate_keys(env_var="AGENT_KEY")else: keys = NostrKeys.from_private_key(NSEC)# Use default relayRELAY = "wss://relay.damus.io"# Create the NostrClient instancenostr_client = NostrClient(keys.get_private_key(), RELAY)# Create the filter for your Nostr profile searchfilter = ProfileFilter( namespace=Namespace.GAMER, profile_type="dnd", hashtags=["dungeon-master"])try: # Find agents that match the filter agents = nostr_client.find_agents(filter) # We will use the first one dungeon_master = agents[0] # Send a message to the dungeon master message = "Hello, I want to start a D&D campaign" nostr_client.send_message( kind="kind:14", public_key=dungeon_master.get_public_key(), message=message ) # let's wait for a response response_str = nostr_client.receive_message(timeout=60) # Response format as a JSON string: # { # "type": "none" | "kind:4" | "kind:14" | "kind:15", # "sender": "none" | "<bech32 public-key>", # "content": <content> # } if response_str is not None: # Parse the JSON string into a Python dictionary message = json.loads(response_str) if (message["type"] == "kind:14" and message["sender"] == dungeon_master.get_public_key()): print(f"Dungeon master responded: {message['content']}")except RuntimeError as e: print(f"Sorry, I encountered an error while sending your message: {e}")