This commit is contained in:
Râu Cao 2024-04-04 12:44:44 +03:00
parent dd218ad951
commit 4d68e92010
Signed by: raucao
GPG Key ID: 37036C356E56CC51
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,48 @@
module NostrManager
class PublishEvent < NostrManagerService
def initialize(event: nil, relay: nil)
# @relay = relay
@relay = Nostr::Relay.new(url: 'ws://nostr-relay:7777', name: 'strfry')
keypair = Nostr::KeyPair.new(
private_key: Nostr::PrivateKey.new(Setting.nostr_private_key),
public_key: Nostr::PublicKey.new(Setting.nostr_public_key)
)
@user = Nostr::User.new(keypair: keypair)
@event = @user.create_event(
kind: Nostr::EventKind::TEXT_NOTE,
content: "The time is #{Time.now.strftime('%H:%M:%S')}"
)
@client = Nostr::Client.new
end
def call
client, relay, event = @client, @relay, @event
client.on :connect do
puts "Connected to #{relay.url}"
puts "Publishing #{event.id}..."
client.publish event
end
client.on :error do |e|
puts "Error: #{e}"
puts "Closing thread..."
Thread.exit
end
client.on :message do |m|
puts "Message: #{m}"
msg = JSON.parse(m) rescue []
if msg[0] == "OK" && msg[1] == event.id
puts "Event published. Closing thread..."
else
puts "Unexpected message from relay. Closing thread..."
end
Thread.exit
end
puts "Connecting to #{relay.url}..."
client.connect relay
end
end
end

View File

@ -0,0 +1,22 @@
{
"kind": 9734,
"created_at": 1712066899,
"content": "",
"tags": [
["p", "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"],
["amount", "21000"],
[
"relays",
"wss://relay.primal.net",
"wss://relay.snort.social",
"wss://nostr.wine",
"wss://relay.damus.io",
"wss://eden.nostr.land",
"wss://atlas.nostr.land",
"wss://nostr.bitcoiner.social",
"wss://relay.mostr.pub",
"wss://relay.mostr.pub/",
"wss://nostr-01.bolt.observer"
]
]
}