Crate nostr_bot

source ·
Expand description

Nostr Bot


Do you want to run your own nostr bot? You’ve come to the right place. This crate makes it easy to implement your own bot that reacts to nostr events, using tokio.

This crate is young and still being developed so there may be hiccups. If you find any issue, don’t hesitate to create PR/issue on GitHub.

Full documentation is available here.


This crate is on and can be used by adding nostr-bot to your dependencies in your project’s Cargo.toml.

nostr-bot = "0.2"

You can also use GitHub repository:

nostr-bot = { git = "", rev = "v0.2.3" }

Or you can clone the repository and use it locally:

nostr-bot = { path = "your/path/to/nostr-bot" }


// Bot that reacts to '!yes', '!no' and '!results' commands.
use nostr_bot::*;

// Your struct that will be passed to the commands responses
struct Votes {
    question: String,
    yes: u64,
    no: u64,

type State = nostr_bot::State<Votes>;

fn format_results(question: &str, votes: &Votes) -> String {
        "{}\n------------------\nyes: {}\nno:  {}",
        question, votes.yes,

// Following functions are command responses, you are getting nostr event
// and shared state as arguments and you are supposed to return non-signed
// event which is then signed using the bot's key and send to relays
async fn yes(event: Event, state: State) -> EventNonSigned {
    let mut votes = state.lock().await;
    votes.yes += 1;

    // Use formatted voting results to create new event
    // that is a reply to the incoming command
    get_reply(event, format_results(&votes.question, &votes))

async fn no(event: Event, state: State) -> EventNonSigned {
    let mut votes = state.lock().await; += 1;
    get_reply(event, format_results(&votes.question, &votes))

async fn results(event: Event, state: State) -> EventNonSigned {
    let votes = state.lock().await;
    get_reply(event, format_results(&votes.question, &votes))

async fn main() {

    let relays = vec![

    let keypair = keypair_from_secret(
        // Your secret goes here, can be hex or bech32 (nsec1...)

    let question = String::from("Do you think Pluto should be a planet?");

    // Wrap your object into Arc<Mutex> so it can be shared among command handlers
    let shared_state = wrap_state(Votes {
        question: question.clone(),
        yes: 0,
        no: 0,

    // And now the Bot
    Bot::new(keypair, relays, shared_state)
        // You don't have to set these but then the bot will have incomplete profile info :(
        .about("Just a bot.")
        // You don't have to specify any command but then what will the bot do? Nothing.
        .command(Command::new("!yes", wrap!(yes)))
        .command(Command::new("!no", wrap!(no)))
        .command(Command::new("!results", wrap!(results)))
        // And finally run it



  • Wraps your functor so it can be passed to the bot.
  • Wraps your functor so it can be passed to the bot.



  • Direct connection to the internet or would you rather like a socks5 proxy?
  • Describes various functor types.


Type Definitions