0xtrr / nostr-tool

A simple CLI to send nostr events
MIT License
60 stars 18 forks source link

feat: Add response message #22

Open gourcetools opened 1 year ago

gourcetools commented 1 year ago

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?

0xtrr commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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?

yukibtc commented 1 year ago

@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.

0xtrr commented 1 year ago

@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.