Song Jong on Nostr: Yuki Kishimoto, I think some imports are missing in the examples, but even after ...
Yuki Kishimoto (nprofile…ehfp), I think some imports are missing in the examples, but even after fixing that, it still throws errors. Can you help me?
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license
use nostr_sdk::prelude::*;
use tracing_subscriber;
use rand;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let keys = Keys::parse("nsec12kcgs78l06p30jz7z7h3n2x2cy99nw2z6zspjdp7qc206887mwvs95lnkx")?;
let client = Client::builder()
.signer(keys.clone())
.opts(Options::new().gossip(true))
.build();
println!("Bot public key: {}", keys.public_key().to_bech32()?);
client.add_relay("wss://nostr.oxtr.dev").await?;
client.add_relay("wss://relay.damus.io").await?;
client.add_relay("wss://nostr.mom").await?;
client.add_relay("wss://nostr.wine").await?;
client.add_relay("wss://relay.nostr.info").await?;
client.add_relay("wss://auth.nostr1.com").await?;
client.connect().await;
let metadata = Metadata::new()
.name("rust-nostr-bot-example")
.display_name("rust-nostr bot example")
.website(Url::parse("https://github.com/rust-nostr/nostr";)?);
client.set_metadata(&metadata).await?;
let subscription = Filter::new()
.pubkey(keys.public_key())
.kind(Kind::GiftWrap)
.limit(0); // Limit set to 0 to get only new events! Timestamp::now() CAN'T be used for gift wrap since the timestamps are tweaked!
client.subscribe(vec![subscription], None).await?;
client
.handle_notifications(|notification| async {
if let RelayPoolNotification::Event { event, .. } = notification {
if event.kind == Kind::GiftWrap {
match client.unwrap_gift_wrap(&event).await {
Ok(UnwrappedGift { rumor, sender }) => {
if rumor.kind == Kind::PrivateDirectMessage {
let content: String = match rumor.content.as_str() {
"/rand" => rand::random::<u16>().to_string(),
"/help" => help(),
_ => String::from(
"Invalid command, send /help to see all commands.",
),
};
// Send private message
client.send_private_msg(sender, content, []).await?;
}
}
Err(e) => tracing::error!("Impossible to decrypt direct message: {e}"),
}
}
}
Ok(false) // Set to true to exit from the loop
})
.await?;
Ok(())
}
fn help() -> String {
let mut output = String::new();
output.push_str("Commands:\n");
output.push_str("/rand - Random number\n");
output.push_str("/help - Help");
output
}
Error log:
user@Debian:~/sjnostr$ cargo run
Compiling sjnostr v0.1.0 (/home/user/sjnostr)
error[E0422]: cannot find struct, variant or union type `UnwrappedGift` in this scope
--> src/main.rs:48:28
|
48 | Ok(UnwrappedGift { rumor, sender }) => {
| ^^^^^^^^^^^^^ not found in this scope
error[E0599]: no method named `unwrap_gift_wrap` found for struct `nostr_sdk::Client` in the current scope
--> src/main.rs:47:34
|
47 | match client.unwrap_gift_wrap(&event).await {
| ^^^^^^^^^^^^^^^^ method not found in `Client`
error[E0599]: no method named `send_private_msg` found for struct `nostr_sdk::Client` in the current scope
--> src/main.rs:59:40
|
59 | ... client.send_private_msg(sender, content, []).await?;
| ^^^^^^^^^^^^^^^^ method not found in `Client`
Some errors have detailed explanations: E0422, E0599.
For more information about an error, try `rustc --explain E0422`.
error: could not compile `sjnostr` (bin "sjnostr") due to 3 previous errors
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license
use nostr_sdk::prelude::*;
use tracing_subscriber;
use rand;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let keys = Keys::parse("nsec12kcgs78l06p30jz7z7h3n2x2cy99nw2z6zspjdp7qc206887mwvs95lnkx")?;
let client = Client::builder()
.signer(keys.clone())
.opts(Options::new().gossip(true))
.build();
println!("Bot public key: {}", keys.public_key().to_bech32()?);
client.add_relay("wss://nostr.oxtr.dev").await?;
client.add_relay("wss://relay.damus.io").await?;
client.add_relay("wss://nostr.mom").await?;
client.add_relay("wss://nostr.wine").await?;
client.add_relay("wss://relay.nostr.info").await?;
client.add_relay("wss://auth.nostr1.com").await?;
client.connect().await;
let metadata = Metadata::new()
.name("rust-nostr-bot-example")
.display_name("rust-nostr bot example")
.website(Url::parse("https://github.com/rust-nostr/nostr";)?);
client.set_metadata(&metadata).await?;
let subscription = Filter::new()
.pubkey(keys.public_key())
.kind(Kind::GiftWrap)
.limit(0); // Limit set to 0 to get only new events! Timestamp::now() CAN'T be used for gift wrap since the timestamps are tweaked!
client.subscribe(vec![subscription], None).await?;
client
.handle_notifications(|notification| async {
if let RelayPoolNotification::Event { event, .. } = notification {
if event.kind == Kind::GiftWrap {
match client.unwrap_gift_wrap(&event).await {
Ok(UnwrappedGift { rumor, sender }) => {
if rumor.kind == Kind::PrivateDirectMessage {
let content: String = match rumor.content.as_str() {
"/rand" => rand::random::<u16>().to_string(),
"/help" => help(),
_ => String::from(
"Invalid command, send /help to see all commands.",
),
};
// Send private message
client.send_private_msg(sender, content, []).await?;
}
}
Err(e) => tracing::error!("Impossible to decrypt direct message: {e}"),
}
}
}
Ok(false) // Set to true to exit from the loop
})
.await?;
Ok(())
}
fn help() -> String {
let mut output = String::new();
output.push_str("Commands:\n");
output.push_str("/rand - Random number\n");
output.push_str("/help - Help");
output
}
Error log:
user@Debian:~/sjnostr$ cargo run
Compiling sjnostr v0.1.0 (/home/user/sjnostr)
error[E0422]: cannot find struct, variant or union type `UnwrappedGift` in this scope
--> src/main.rs:48:28
|
48 | Ok(UnwrappedGift { rumor, sender }) => {
| ^^^^^^^^^^^^^ not found in this scope
error[E0599]: no method named `unwrap_gift_wrap` found for struct `nostr_sdk::Client` in the current scope
--> src/main.rs:47:34
|
47 | match client.unwrap_gift_wrap(&event).await {
| ^^^^^^^^^^^^^^^^ method not found in `Client`
error[E0599]: no method named `send_private_msg` found for struct `nostr_sdk::Client` in the current scope
--> src/main.rs:59:40
|
59 | ... client.send_private_msg(sender, content, []).await?;
| ^^^^^^^^^^^^^^^^ method not found in `Client`
Some errors have detailed explanations: E0422, E0599.
For more information about an error, try `rustc --explain E0422`.
error: could not compile `sjnostr` (bin "sjnostr") due to 3 previous errors