Nostr.Event.PrivateMessage (Nostr Lib v0.2.1) (event) (nip17)

View Source

Private Direct Message (Kind 14)

This is an unsigned event (rumor) for encrypted chat messages per NIP-17. Messages MUST be wrapped in a Seal (kind 13) and GiftWrap (kind 1059) before publishing.

Defined in NIP 17 https://github.com/nostr-protocol/nips/blob/master/17.md

Summary

Types

Quoted event reference

Receiver with optional relay URL

t()

Functions

Create a new private message (unsigned rumor)

Parse a kind 14 event or rumor into a PrivateMessage struct

Types

quote_ref()

@type quote_ref() :: %{id: binary(), relay: binary() | nil, pubkey: binary() | nil}

Quoted event reference

receiver()

@type receiver() :: %{pubkey: binary(), relay: URI.t() | nil}

Receiver with optional relay URL

t()

@type t() :: %Nostr.Event.PrivateMessage{
  content: binary(),
  quotes: [quote_ref()],
  receivers: [receiver()],
  reply_to: binary() | nil,
  rumor: Nostr.Event.Rumor.t(),
  subject: binary() | nil
}

Functions

create(sender_pubkey, receiver_pubkeys, content, opts \\ [])

@spec create(
  sender_pubkey :: binary(),
  receiver_pubkeys :: [binary() | map()],
  content :: binary(),
  opts :: Keyword.t()
) :: t()

Create a new private message (unsigned rumor)

Arguments

  • sender_pubkey - public key of the sender
  • receiver_pubkeys - list of receiver public keys (or maps with pubkey and optional relay)
  • content - plain text message content
  • opts - optional arguments:
    • :reply_to - event ID this message is replying to
    • :subject - conversation title
    • :quotes - list of quoted event references
    • :created_at - timestamp (defaults to now)

Example

iex> msg = Nostr.Event.PrivateMessage.create(
...>   "sender_pubkey",
...>   ["receiver_pubkey"],
...>   "Hello!"
...> )
iex> msg.content
"Hello!"
iex> msg.rumor.kind
14

parse(rumor)

@spec parse(Nostr.Event.Rumor.t() | Nostr.Event.t() | map()) :: t()

Parse a kind 14 event or rumor into a PrivateMessage struct

Example

iex> rumor = %Nostr.Event.Rumor{kind: 14, content: "Hello", tags: [], pubkey: "abc", created_at: DateTime.utc_now()}
iex> msg = Nostr.Event.PrivateMessage.parse(rumor)
iex> msg.content
"Hello"