simploxide-client 0.10.0

SimpleX-Chat API client
Documentation
//! The examples expects that SimpleX-CLI(`simplex-chat` binary) is installed locally on the system
//! and is available via `$PATH` or at the `simploxide/simploxide-client` directory(you can symlink
//! it there)
//!
//! To compile this example pass the --features flag like this:
//! `cargo run --example squaring_bot --features bon,cli`
//!
//!
//! ----
//!
//! A bot that receives a number and sends back its square.

use simploxide_client::{
    prelude::*,
    ws::{self, ClientResult},
};
use std::{error::Error, sync::Arc};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let (bot, events, mut cli) = ws::BotBuilder::new("SimplOxide Examples", 5225)
        .db_prefix("test_db/bot")
        // create a public bot address auto-accepting new users with a welcome message
        .auto_accept_with(
            "Hello, I'm a simple squaring bot. Send me a number and I will calculate its square",
        )
        // Launch CLI, connect the client, and initialise the bot
        .launch()
        .await?;

    let address = bot.address().await?;
    println!("Bot address: {address}");

    events.into_dispatcher(bot).on(new_msgs).dispatch().await?;
    cli.kill().await?;
    Ok(())
}

async fn new_msgs(ev: Arc<NewChatItems>, bot: ws::Bot) -> ClientResult<StreamEvents> {
    // SimpleX sends a lot of utility messages like changed preferences and connection events.
    // These fake messages must be filtered out, we're interested only in real user messages
    for (chat, msg, content) in ev.chat_items.filter_messages() {
        if msg.meta.item_text.trim() == "/die" {
            return Ok(StreamEvents::Break);
        }

        if let Some(num) = content
            .text()
            .and_then(|txt| txt.trim().parse::<i64>().ok())
        {
            let square = num.saturating_mul(num);
            bot.send_msg(chat, format!("Squared: {square}"))
                .reply_to(msg)
                .send()
                .await?;
        } else {
            bot.send_msg(chat, "Me understands only numbers!")
                .reply_to(msg)
                .send()
                .await?;
        }
    }

    Ok(StreamEvents::Continue)
}