nostr-tool icon indicating copy to clipboard operation
nostr-tool copied to clipboard

feat: Add response message

Open gourcetools opened this issue 2 years ago • 5 comments

It would be nice to have response from server when an event is send, usually ["OK","EVENTID",true,""] when note is send succesfully. Using websocat and nostril , that's what i get.

Or i get :

Sent event EVENTID to 'wss://relay.nostr.band'.
Seen EVENTID on 'wss://relay.nostr.band'.

when using noscl.

This let me know that the message was succesfully accepted by the server and the event id so I can look at it later if i need to. I know you already give:

Public key:
Private key:

back . Wich i think is nice.

What do you think?

gourcetools avatar Feb 07 '23 22:02 gourcetools

Should be doable, we could add a flag that lets you listen for the event on the relay you're posting to. I have other features I want to prioritize over this one though but if someone else wants to add it, be my guest. If not, I'll take a look at it when I get time.

0xtrr avatar Feb 14 '23 21:02 0xtrr

I think we should do this as a "--print-response" flag. This should be one of the root flags just like --relay and --private-key and should be passed down to sub command functions.

0xtrr avatar Apr 21 '23 12:04 0xtrr

@yukibtc I'm unsure how I would get the response from posting an event to the relay.

I found this example: https://github.com/rust-nostr/nostr/blob/master/crates/nostr-sdk/examples/blocking.rs#L36

I tried to do this like so:

    // Publish event
    let event_id = client.publish_text_note(sub_command_args.content.clone(), &tags)?;

    let subscription = Filter::new().event(event_id.clone());
    
    client.handle_notifications(|notification| {
        if let RelayPoolNotification::Message(_url, relay_message) = notification {
            if let RelayMessage::Ok { event_id, status, message } = relay_message {
                if status {
                    if !sub_command_args.hex {
                        println!("Published text note with id: {}", event_id.to_bech32()?);
                    } else {
                        println!("Published text note with id: {}", event_id.to_hex());
                    }
                } else {
                    println!("Failed to write event to relay: {}", message);
                }
            }
        }
        println!("TESTING SOMETHING");
        Ok(())
    })?;
    println!("TESTING SOMETHING ELSE");

This kind of works but I never reach the TESTING SOMETHING ELSE which means the command never finishes. I guess the client.handle_notifications function never exits? How would I exit as soon as I receive the event I subscribe to?

0xtrr avatar May 27 '23 22:05 0xtrr

@yukibtc I'm unsure how I would get the response from posting an event to the relay.

I found this example: https://github.com/rust-nostr/nostr/blob/master/crates/nostr-sdk/examples/blocking.rs#L36

I tried to do this like so:

    // Publish event
    let event_id = client.publish_text_note(sub_command_args.content.clone(), &tags)?;

    let subscription = Filter::new().event(event_id.clone());
    
    client.handle_notifications(|notification| {
        if let RelayPoolNotification::Message(_url, relay_message) = notification {
            if let RelayMessage::Ok { event_id, status, message } = relay_message {
                if status {
                    if !sub_command_args.hex {
                        println!("Published text note with id: {}", event_id.to_bech32()?);
                    } else {
                        println!("Published text note with id: {}", event_id.to_hex());
                    }
                } else {
                    println!("Failed to write event to relay: {}", message);
                }
            }
        }
        println!("TESTING SOMETHING");
        Ok(())
    })?;
    println!("TESTING SOMETHING ELSE");

This kind of works but I never reach the TESTING SOMETHING ELSE which means the command never finishes. I guess the client.handle_notifications function never exits? How would I exit as soon as I receive the event I subscribe to?

Try with:

client.handle_notifications(|notification| {
    if let RelayPoolNotification::Message(_url, relay_message) = notification {
        if let RelayMessage::Ok { event_id, status, message } = relay_message {
            if status {
                if !sub_command_args.hex {
                    println!("Published text note with id: {}", event_id.to_bech32()?);
                } else {
                    println!("Published text note with id: {}", event_id.to_hex());
                }
            } else {
                println!("Failed to write event to relay: {}", message);
            }
           return Ok(true);
        }
    }
    println!("TESTING SOMETHING");
    Ok(false)
})?;

You must use nostr-sdk from master branch, at commit eced782b.

Basically if the function pointer output is false the loop will continue, if it's true it will exit.

yukibtc avatar May 28 '23 08:05 yukibtc

@yukibtc I'm unsure how I would get the response from posting an event to the relay. I found this example: https://github.com/rust-nostr/nostr/blob/master/crates/nostr-sdk/examples/blocking.rs#L36 I tried to do this like so:

    // Publish event
    let event_id = client.publish_text_note(sub_command_args.content.clone(), &tags)?;

    let subscription = Filter::new().event(event_id.clone());
    
    client.handle_notifications(|notification| {
        if let RelayPoolNotification::Message(_url, relay_message) = notification {
            if let RelayMessage::Ok { event_id, status, message } = relay_message {
                if status {
                    if !sub_command_args.hex {
                        println!("Published text note with id: {}", event_id.to_bech32()?);
                    } else {
                        println!("Published text note with id: {}", event_id.to_hex());
                    }
                } else {
                    println!("Failed to write event to relay: {}", message);
                }
            }
        }
        println!("TESTING SOMETHING");
        Ok(())
    })?;
    println!("TESTING SOMETHING ELSE");

This kind of works but I never reach the TESTING SOMETHING ELSE which means the command never finishes. I guess the client.handle_notifications function never exits? How would I exit as soon as I receive the event I subscribe to?

Try with:

client.handle_notifications(|notification| {
    if let RelayPoolNotification::Message(_url, relay_message) = notification {
        if let RelayMessage::Ok { event_id, status, message } = relay_message {
            if status {
                if !sub_command_args.hex {
                    println!("Published text note with id: {}", event_id.to_bech32()?);
                } else {
                    println!("Published text note with id: {}", event_id.to_hex());
                }
            } else {
                println!("Failed to write event to relay: {}", message);
            }
           return Ok(true);
        }
    }
    println!("TESTING SOMETHING");
    Ok(false)
})?;

You must use nostr-sdk from master branch, at commit eced782b.

Basically if the function pointer output is false the loop will continue, if it's true it will exit.

Nice, that worked! Appreciate the quick fix on master, sent you some sats to your ln address.

0xtrr avatar May 28 '23 08:05 0xtrr