In November 2023, the code required to get this to compile and run was:
use std::{str::FromStr, time::Duration, collections::{HashSet, HashMap}};
use nostr_sdk::prelude::*;
use tokio::time;
const PRIVATE_KEY: &str = "PRIVATE KEY";
#[tokio::main]
async fn main() -> Result<()> {
let secret_key = SecretKey::from_str(PRIVATE_KEY).unwrap();
let my_keys = Keys::new(secret_key);
let client = Client::new(&my_keys);
client.add_relay("wss://relay.house", None).await?;
client.add_relay("wss://relay.damus.io", None).await?;
client.connect().await;
let message = format!("Hello, nostr! My public key is: {}", my_keys.public_key().to_string());
println!("{}", message);
let event = client.publish_text_note(message, &[]).await?;
println!("{:#?}", event);
// Retrieve only our last event (from both relays)
// let filter = Filter::new().id(event);
// Retrieve all the events that we have posted
let ids = HashSet::from([event]);
let authors = HashSet::from([my_keys.public_key()]);
let kinds = HashSet::new();
let generic_tags: HashMap<Alphabet, HashSet<String>> = HashMap::new();
let filter = Filter {
ids,
authors,
kinds,
generic_tags,
search: None,
since: None,
until: None,
limit: None,
};
time::sleep(Duration::from_secs(1)).await;
let events = client.get_events_of(vec![filter], None).await?;
println!("{:#?}", events);
Ok(())
}