Skip to main content

file_bot/
file_bot.rs

1//! Sends a single file to a recipient, then exits. Demonstrates the send side of
2//! the encrypted file-attachment pipeline; see `save_files_bot` for the receive
3//! side (download + decrypt).
4//!
5//! Run with:
6//! ```sh
7//! VECTOR_NSEC=nsec1... \
8//! VECTOR_TARGET=npub1... \
9//! VECTOR_FILE=/path/to/image.png \
10//! cargo run -p vector-sdk --example file_bot
11//! ```
12
13use std::time::Duration;
14use vector_sdk::VectorBot;
15
16#[tokio::main]
17async fn main() -> vector_sdk::Result<()> {
18    let nsec = std::env::var("VECTOR_NSEC").expect("set VECTOR_NSEC to your bot's nsec");
19    let target = std::env::var("VECTOR_TARGET").expect("set VECTOR_TARGET to a recipient npub");
20    let file = std::env::var("VECTOR_FILE").expect("set VECTOR_FILE to a file path");
21
22    let bot = VectorBot::builder().nsec(nsec).build().await?;
23    println!("Sending {} as {}", file, bot.npub());
24
25    // Give the relay connections a moment to come up before the first send.
26    tokio::time::sleep(Duration::from_secs(2)).await;
27
28    let event_id = bot.channel(&target).send_file(&file).await?;
29    println!("Sent — event id: {}", event_id);
30
31    Ok(())
32}