Skip to main content

echo_bot/
echo_bot.rs

1//! A minimal echo bot. Replies to every inbound DM with `Echo: <text>`.
2//!
3//! Run with:
4//! ```sh
5//! VECTOR_NSEC=nsec1... cargo run -p vector-sdk --example echo_bot
6//! ```
7
8use vector_sdk::VectorBot;
9
10#[tokio::main]
11async fn main() -> vector_sdk::Result<()> {
12    let nsec = std::env::var("VECTOR_NSEC").expect("set VECTOR_NSEC to your bot's nsec");
13
14    let bot = VectorBot::builder().nsec(nsec).build().await?;
15    println!("Echo bot online as {}", bot.npub());
16
17    // Blocks, processing inbound messages until the client disconnects. The SAME handler and
18    // reply work for DMs and Community channels — the SDK hides the transport difference.
19    bot.on_message(|_bot, msg| async move {
20        if msg.is_mine() {
21            return; // don't echo our own messages
22        }
23        let _ = msg.channel().typing().await;
24        let _ = msg.reply(&format!("Echo: {}", msg.text())).await;
25    })
26    .await?;
27
28    Ok(())
29}