Rust

The Rust crate, veilmesh-bot-client. Blocking, no async runtime: the standard library, serde_json, sha2 and getrandom.

cargo add veilmesh-bot-client

An echo bot

use veilmesh_bot_client::{Client, Command, DEFAULT_URL};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let bot = Client::from_token_file("token.json", DEFAULT_URL)?;
    bot.set_commands(&[Command::new("echo", "Repeat the text", "TEXT")])?;
    bot.skip_old()?;
    for msg in bot.messages() {
        let msg = msg?;
        let text = msg.text.trim();
        if text.eq_ignore_ascii_case("ping") {
            bot.reply(&msg, "pong")?;
        } else if let Some(rest) = text.strip_prefix("/echo ") {
            bot.reply(&msg, rest)?;
        }
    }
    Ok(())
}

messages() is an iterator that reconnects while the daemon is away (1 to 30 seconds between tries) and ends after one error that is not the daemon being away, such as a refused token.

The client

  • Client::new(token, url), Client::from_token_file(path, url) — loopback addresses only; Debug never shows the token.
  • me(), groups() → Vec<Group>.
  • messages() → Messages (.wait(secs) sets the long-poll); items are Result<Message, ApiError>.
  • events(wait), skip_old(), cursor, missed.
  • send, reply, send_file, react, leave.
  • commands(), set_commands(&[Command]).

ApiError { status, message }; status 0 means the daemon is not reachable. The client checks the daemon's proof (/v1/hello) before the token goes out.