-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathbot.rs
77 lines (64 loc) · 2.83 KB
/
bot.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2025 Rust Nostr Developers
// Distributed under the MIT software license
use nostr_sdk::prelude::*;
#[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(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
}